一,布局

R绘图所占的区域,被分成两大部分,一是外围边距,一是绘图区域。

外围边距可使用par()函数中的oma来进行设置。比如oma=c(4,3,2,1),就是指外围边距分别为下边距:4行,左边距3行,上边距2行,右边距1行。很明显这个设置顺序是从x轴开始顺时针方向。这里的行是指可以显示1行普通字体。所以当我们使用mtext中的line参数时,设置的大小就应该是[0,行数)的开区间。当我们使用mtext在外围边距上书写内容时,设置mtext中的outer=TRUE即可。

绘图区域可使用par()函数中的mfrow, mfcol来进行布局。mfrow和mfcol可以使用绘图区域被区分为多个区域。默认值为mfrow(1,1)。

比如mfrow(2,3)就是指将绘图区域分成2行3列,并按行的顺序依次绘图填充;
比如mfcol(3,2)就是指将绘图区域分成3行2列,并按列的顺序依次绘图填充;

我们将每一个细分的绘图区域分为两个部分,一是绘图边距,一是主绘图。

绘图边距需要容纳的内容有坐标轴,坐标轴标签,标题。通常来讲,我们都只需要一个x轴,一个y轴,所以在设置时,一般的下边距和左边距都会大一些。如果多个x轴或者y轴,才考虑将上边距或者右边距放大一些。绘图边距可以使用par()函数中mar来设置。比如mar=c(4,3,2,1),与外围边距的设置类似,是指绘图边距分别为下边距:4行,左边距3行,上边距2行,右边距1行。很明显这个设置顺序是从x轴开始顺时针方向。行的概念与之前的相同。也可以使用mai来设置。mai与mar唯一不同之处在于mai不是以行为单位,而是以inch为单位。

  1. SOUTH<-1; WEST<-2; NORTH<-3; EAST<-4;
  2.  
  3. GenericFigure <- function(ID, size1, size2)
  4. {
  5. plot(0:10, 0:10, type="n", xlab="X", ylab="Y")
  6. text(5,5, ID, col="red", cex=size1)
  7. box("plot", col="red")
  8. mtext(paste("cex",size2,sep=""), SOUTH, line=3, adj=1.0, cex=size2, col="blue")
  9. title(paste("title",ID,sep=""))
  10. }
  11.  
  12. MultipleFigures <- function()
  13. {
  14. GenericFigure("1", 3, 0.5)
  15. box("figure", lty="dotted", col="blue")
  16.  
  17. GenericFigure("2", 3, 1)
  18. box("figure", lty="dotted", col="blue")
  19.  
  20. GenericFigure("3", 3, 1.5)
  21. box("figure", lty="dotted", col="blue")
  22.  
  23. GenericFigure("4", 3, 2)
  24. box("figure", lty="dotted", col="blue")
  25. }
  26.  
  27. par(mfrow=c(2,2),mar=c(6,4,2,1),oma=c(4,3,2,1))
  28.  
  29. MultipleFigures()
  30.  
  31. box("inner", lty="dotted", col="green")
  32. box("outer", lty="solid", col="green")
  33.  
  34. mtext("Outer Margin Area (oma) of South: 6", SOUTH, line=1, cex=1, outer=TRUE)
  35.  
  36. plotline<-function(n,direc){
  37. for(i in 0:n){
  38. mtext(paste("line",i,sep=""), direc, line=i, cex=1, col="black", adj=1, outer=TRUE)
  39. }
  40. }
  41. plotline(4,SOUTH)

R绘图布局

但是,使用mfrow,mfcol只能是矩阵似的布局,如果我们需要简单地实际不规则的布局,那该怎么办呢?还有强大的layout()专门用于布局。

layout(mat, widths = rep(1, ncol(mat)), heights = rep(1,nrow(mat)),respect = FALSE)

其中,mat就是一个距阵,假设我们要画N个图,那么,mat就要描述每个图所在的位置,其中1…N-1都必须至少出现过一次。比如有三个图,我们希望的布局是第一排有一个图,第二排有两个图,那么mat<-matrix(c(1,1,2,3), nrow=2, ncol=2, byrow = TRUE);如果希望第一排有两个图,第二排有一个图,那么就是mat<-matrix(c(1,2,3,3),nrow=2,ncol=2,byrow=TRUE)。很明显,还是将画布分成许多小格矩阵,这里就是2X2的,如果希望第1个图放入第一排的1,2格,那就在矩阵的第1,2的位置写入1,如果是希望第2个图放在第一排的1,2格,那就在矩阵的第1,2的位置写入2。

  1. > attach(mtcars)
  2. > layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE))
  3. > hist(wt)
  4. > hist(mpg)
  5. > hist(disp)

layout不规则布局0

那么参数中的widths和heights又是做什么用的呢?它们就是用来指定每行或者每列的宽度和高度的。我们可以做以下的比较:

  1. > attach(mtcars)
  2. > layout(matrix(c(1,1,1,1,2,2,2,3,2,2,2,3),3,4,byrow=TRUE))
  3. > hist(wt)
  4. > hist(mpg)
  5. > hist(disp)
  6. > layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE),
  7. + widths=c(3,1), heights=c(1,2))
  8. > hist(wt)
  9. > hist(mpg)
  10. > hist(disp)

layout不规则布局1

我们发现,这两种方法得到的是相同的结果,在代码的书写上,第2种方法要更清晰一些。

想象一下,似乎每个图的位置必须是连在一起的小矩阵,如果,不构成连在一起小矩阵,会成为怎么样的一个结果呢?

  1. > attach(mtcars)
  2. > layout(matrix(c(1,1,2,1,1,1),nrow=2,ncol=3,byrow=T))
  3. > hist(wt)
  4. > hist(mpg)

layout内嵌图布局

如果需要大图,小图,内嵌图这layout()虽然很容易实现,但多少有点麻烦,不是那么随心所欲。还是回过头来使用par()吧。使用par()的中fig=在画布任意位置上画图。在使用fig参数时,需要把画布理解成左下角为坐标(0,0),右上角为(1,1)的一个坐标系。fig=c(x1,x2,y1,y2)来设置该参,x1<x2,y1<y2,x1,y1定位绘图区的左下角,x2,y2定位绘图区的右上角。使用new=TRUE参数来确认是否在原画布上继续画,还重新在一张新画布上开始画。

  1. > par(fig=c(0,0.85,0,0.85), new=F)
  2. > plot(mtcars$wt, mtcars$mpg, xlab="Miles Per Gallon",
  3. + ylab="Car Weight")
  4. > par(fig=c(0,0.8,0.55,1), new=TRUE)
  5. > boxplot(mtcars$wt, horizontal=TRUE, axes=FALSE)
  6. > par(fig=c(0.65,1,0,0.8),new=TRUE)
  7. > boxplot(mtcars$mpg, axes=FALSE)
  8. > mtext("Enhanced Scatterplot", side=3, outer=TRUE, line=-3)
  9. > par(fig=c(0.4,0.75,0.4,0.7),new=TRUE,mar=c(2,2,0,0),mgp=c(1,.4,0),cex=1,cex.lab=0.7,cex.axis=0.7)
  10. > hist(mtcars$mpg, main="")

par实现内嵌图

二,颜色

计算机保存及还原颜色时有多种方案,较为常用的是两个,RGB和HSV。R预设了657种颜色,可以通过colors()函数调用(或者英式拼写colours())。比如我们常用的红,绿,蓝,

  1. > colors()[c(552,254,26)]
  2. [1] "red" "green" "blue"

我们可以使用grep来调取我们感兴趣的颜色,

  1. > grep("red",colors())
  2. [1] 100 372 373 374 375 376 476 503 504 505 506 507 524 525 526 527 528 552 553
  3. [20] 554 555 556 641 642 643 644 645
  4.  
  5. > colors()[grep("red",colors())]
  6. [1] "darkred" "indianred" "indianred1" "indianred2"
  7. [5] "indianred3" "indianred4" "mediumvioletred" "orangered"
  8. [9] "orangered1" "orangered2" "orangered3" "orangered4"
  9. [13] "palevioletred" "palevioletred1" "palevioletred2" "palevioletred3"
  10. [17] "palevioletred4" "red" "red1" "red2"
  11. [21] "red3" "red4" "violetred" "violetred1"
  12. [25] "violetred2" "violetred3" "violetred4"
  13.  
  14. > colors()[grep("sky",colors())]
  15. [1] "deepskyblue" "deepskyblue1" "deepskyblue2" "deepskyblue3"
  16. [5] "deepskyblue4" "lightskyblue" "lightskyblue1" "lightskyblue2"
  17. [9] "lightskyblue3" "lightskyblue4" "skyblue" "skyblue1"
  18. [13] "skyblue2" "skyblue3" "skyblue4"
  19.  
  20. > SetTextContrastColor <- function(color)
  21. + {
  22. + ifelse( mean(col2rgb(color)) > 127, "black", "white")
  23. + }
  24. > # Define this array of text contrast colors that correponds to each
  25. > # member of the colors() array.
  26. > TextContrastColor <- unlist( lapply(colors(), SetTextContrastColor) )
  27.  
  28. > # 1a. Plot matrix of R colors, in index order, 25 per row.
  29. > # This example plots each row of rectangles one at a time.
  30. > colCount <- 25 # number per row
  31. > rowCount <- 27
  32. > plot( c(1,colCount), c(0,rowCount), type="n", ylab="", xlab="",
  33. + axes=FALSE, ylim=c(rowCount,0))
  34. > title("R colors")
  35. >
  36. > for (j in 0:(rowCount-1))
  37. + {
  38. + base <- j*colCount
  39. + remaining <- length(colors()) - base
  40. + RowSize <- ifelse(remaining < colCount, remaining, colCount)
  41. + rect((1:RowSize)-0.5,j-0.5, (1:RowSize)+0.5,j+0.5,
  42. + border="black",
  43. + col=colors()[base + (1:RowSize)])
  44. + text((1:RowSize), j, paste(base + (1:RowSize)), cex=0.7,
  45. + col=TextContrastColor[base + (1:RowSize)])
  46. + }

R颜色表

对于大多数理工出身的人来讲,理解颜色并不难,难的是如何选择一种或者多种理想的颜色,让绘图很漂亮。R当中有一个包RColorBrewer就可以为我们解决这个难题,其中预设了很多种颜色组合以供我们使用。

  1. > library(RColorBrewer)
  2. > display.brewer.all()

RColorBrewer中预设的颜色表

我们看到其中Set3和Paired设定了12种颜色,其余的有多有少,以9种居多。在使用其颜色时,使用brewer.pal(n, name)调用即可,其中n最小值为3最大值为每组预设值数组的长度。比如brewer.pal(12,Paired)

在绘图时,有如下颜色参数

参数 描述
col 绘图使用的颜色,许多函数接受一组颜色,并对不同的数据依次使用颜色。
col.axis 坐标轴字符颜色
col.lab x,y坐标标记颜色
col.main 标题颜色
col.sub 副标题颜色
fg 绘图前景色,包括坐标轴,各类boxes
bg 绘图背景色
  1. > require(graphics)
  2. > par(col.axis="green",col.lab="blue",col.main="darkred",fg="white",bg="black")
  3. > plot(cars,main="speed vs dist")

颜色设置示例

三,字体

字体参数如下:

参数 描述
font 字体描述,1正常,2加粗,3斜体,4加粗,斜体,5符号
font.axis 坐标轴字符描述
font.lab 坐标轴标记字体描述
font.main 标题字体描述
font.sub 副标题字体描述
ps 字体点阵大小,大约为1/72英寸。在使用时text size=ps*cex
cex 字体放大或者缩小多少倍
cex.axis 坐标轴字符大小
cex.lab 坐标轴标记字体大小
cex.main 标题字体大小
cex.sub 副标题字体大小
family 绘图字体。标准字体是”serif”,”sans”,”mono”,”symbol”。当然可以指定任何自己已有的字体库。但它是设备依赖的。
  1. > fonts<-names(pdfFonts())
  2. > cnt<-length(fonts)
  3. > x<-cnt+10
  4. > x<-1:x
  5. > y<-x
  6. > pdf("font.pdf")
  7. > plot(x,y,type="n")
  8. > for(i in 1:cnt){text(i,i*1.3,family=fonts[i],paste("font",fonts[i]),adj=0)}
  9. > dev.off()

字体

四,符号与线形

画图中的符号由pch参数来控制。其描边色由col控制,填充色由bg控制。

  1. > pchShow <-
  2. + function(extras = c("*",".", "o","O","0","+","-","|","%","#"),
  3. + cex = 3, ## good for both .Device=="postscript" and "x11"
  4. + col = "red3", bg = "gold", coltext = "brown", cextext = 1.2,
  5. + main = paste("plot symbols : points (... pch = *, cex =",
  6. + cex,")"))
  7. + {
  8. + nex <- length(extras)
  9. + np <- 26 + nex
  10. + ipch <- 0:(np-1)
  11. + k <- floor(sqrt(np))
  12. + dd <- c(-1,1)/2
  13. + rx <- dd + range(ix <- ipch %/% k)
  14. + ry <- dd + range(iy <- 3 + (k-1)- ipch %% k)
  15. + pch <- as.list(ipch) # list with integers & strings
  16. + if(nex > 0) pch[26+ 1:nex] <- as.list(extras)
  17. + plot(rx, ry, type="n", axes = FALSE, xlab = "", ylab = "",
  18. + main = main)
  19. + abline(v = ix, h = iy, col = "lightgray", lty = "dotted")
  20. + for(i in 1:np) {
  21. + pc <- pch[[i]]
  22. + ## 'col' symbols with a 'bg'-colored interior (where available) :
  23. + points(ix[i], iy[i], pch = pc, col = col, bg = bg, cex = cex)
  24. + if(cextext > 0)
  25. + text(ix[i] - 0.3, iy[i], pc, col = coltext, cex = cextext)
  26. + }
  27. + }
  28. >
  29. > pchShow()

符号

线形主要由lty和lwd来控制。lty: line type. lwd: line width.顾名思意,lty控制线的形状,而lwd控制线的粗细,默认值为1。设计成2表示两倍线宽。

线形

五,坐标轴,图例,标记与标题

相关参数来控制,它们有

参数 描述
main 主标题
sub 副标题
xlab x轴标记
ylab y轴标记
xlim x轴上下限(范围)
ylim y轴上下限
mgp 坐标轴标记,坐标字符,坐标刻度线距离坐标轴的行数,默认值为c(3,1,0)

增加一个新的坐标轴使用axis()函数。

参数 描述
side 坐标轴所在的位置,1:下,2:左,3:上,4:右
at 坐标轴具体位置,通常由自动给出。
labels 坐标字符串
pos 坐标轴线所在的行,默认值为重绘所在位置上的原坐标
lty 线型
col 颜色
las 坐标标记与坐标轴方位关系,=0为平等,=2为垂直
lwd.ticks 坐标刻度线宽度
col.ticks 坐标刻度线颜色
(…) 其它par()中可用的参数
  1. > # A Silly Axis Example
  2. >
  3. > # specify the data
  4. > x <- c(1:10); y <- x; z <- 10/x
  5. >
  6. > # create extra margin room on the right for an axis
  7. > par(mar=c(5, 4, 4, 8) + 0.1)
  8. >
  9. > # plot x vs. y
  10. > plot(x, y,type="b", pch=21, col="red",
  11. + yaxt="n", lty=3, xlab="", ylab="")
  12. >
  13. > # add x vs. 1/x
  14. > lines(x, z, type="b", pch=22, col="blue", lty=2)
  15. >
  16. > # draw an axis on the left
  17. > axis(2, at=x,labels=x, col.axis="red", las=2)
  18. >
  19. > # draw an axis on the right, with smaller text and ticks
  20. > axis(4, at=z,labels=round(z,digits=2),
  21. + col.axis="blue", las=2, cex.axis=0.7, tck=-.01)
  22. >
  23. > # add a title for the right axis
  24. > mtext("y=1/x", side=4, line=3, cex.lab=1,las=2, col="blue")
  25. >
  26. > # add a main title and bottom and left axis labels
  27. > title("An Example of Creative Axes", xlab="X values",
  28. + ylab="Y=X")

坐标轴

图例使用legend()函数控制

参数 描述
x,y 图例所在位置,可以使用”bottom”,”bottomleft”,”left”,”topleft”,”top”,”topright”,”right”,”bottomleft”,”center”来指定。
inset 设置在主绘图边距
title 图例的标题
legend 图例的内容
其它par()可用的参数
  1. > attach(mtcars)
  2. > boxplot(mpg~cyl, main="Milage by Car Weight",
  3. + yaxt="n", xlab="Milage", horizontal=TRUE,
  4. + col=terrain.colors(3))
  5. > legend("topright", inset=.05, title="Number of Cylinders",
  6. + c("4","6","8"), fill=terrain.colors(3), horiz=TRUE)

图例

文本框使用text或者mtext函数。text可以在主绘图区内加文本框,mtext在边距或者外边距上加文本框。

text(location, “text to place”, pos, …)
mtext(“text to place”, side, line=n, …)

参数 描述
location 图例所在位置
pos 所在的相对位置,1:下面,2:左边,3:上面,4:右边
side 所在边距的位置,1:下,2:左,3:上,4:右
其它par()可用的参数
  1. > attach(mtcars)
  2. > plot(wt, mpg, main="Milage vs. Car Weight",
  3. + xlab="Weight", ylab="Mileage", pch=18, col="blue")
  4. > text(wt, mpg, row.names(mtcars), cex=0.6, pos=4, col="red")

文本框

六,各类图型

点,线,面

点:points,线:abline,lines,segments,面:box,polygon,polypath,rect,特殊的:arrows,symbols

points不仅仅可以画前文中pch所设定的任意一个符号,还可以以字符为符号。

  1. > ## ------------ test code for various pch specifications -------------
  2. > # Try this in various font families (including Hershey)
  3. > # and locales. Use sign=-1 asserts we want Latin-1.
  4. > # Standard cases in a MBCS locale will not plot the top half.
  5. > TestChars <- function(sign=1, font=1, ...)
  6. + {
  7. + if(font == 5) { sign <- 1; r <- c(32:126, 160:254)
  8. + } else if (l10n_info()$MBCS) r <- 32:126 else r <- 32:255
  9. + if (sign == -1) r <- c(32:126, 160:255)
  10. + par(pty="s")
  11. + plot(c(-1,16), c(-1,16), type="n", xlab="", ylab="",
  12. + xaxs="i", yaxs="i")
  13. + grid(17, 17, lty=1)
  14. + for(i in r) try(points(i%%16, i%/%16, pch=sign*i, font=font,...))
  15. + }
  16. > TestChars()
  17. > try(TestChars(sign=-1))

画点

abline可以由斜率和截距来确定一条直线,lines可以连接两个或者多个点,segments可以按起止位置画线。

  1. > require(stats)
  2. > sale5 <- c(6, 4, 9, 7, 6, 12, 8, 10, 9, 13)
  3. > plot(sale5,new=T)
  4. > abline(lsfit(1:10,sale5))
  5. > abline(lsfit(1:10,sale5, intercept = FALSE), col= 4)
  6. > abline(h=6, v=8, col = "gray60")
  7. > text(8,6, "abline( h = 6 )", col = "gray60", adj = c(0, -.1))
  8. > abline(h = 4:8, v = 6:12, col = "lightgray", lty=3)
  9. > abline(a=1, b=2, col = 2)
  10. > text(5,11, "abline( 1, 2 )", col=2, adj=c(-.1,-.1))
  11. > segments(6,4,9,5,col="green")
  12. > text(6,5,"segments(6,4,9,5)")
  13. > lines(sale5,col="pink")

线段

box画出当前盒子的边界,polygon画多边形,polypath画路径,rect画距形。

  1. > x <- c(1:9,8:1)
  2. > y <- c(1,2*(5:3),2,-1,17,9,8,2:9)
  3. > op <- par(mfcol=c(3,1))
  4. > for(xpd in c(FALSE,TRUE,NA)) {
  5. + plot(1:10, main = paste("xpd =", xpd))
  6. + box("figure", col = "pink", lwd=3)
  7. + polygon(x,y, xpd=xpd, col="orange", lty=2, lwd=2, border="red")
  8. + }
  9. > par(op)
  10. > plotPath <- function(x, y, col = "grey", rule = "winding") {
  11. + plot.new()
  12. + plot.window(range(x, na.rm = TRUE), range(y, na.rm = TRUE))
  13. + polypath(x, y, col = col, rule = rule)
  14. + if (!is.na(col))
  15. + mtext(paste("Rule:", rule), side = 1, line = 0)
  16. + }
  17. >
  18. > plotRules <- function(x, y, title) {
  19. + plotPath(x, y)
  20. + plotPath(x, y, rule = "evenodd")
  21. + mtext(title, side = 3, line = 0)
  22. + plotPath(x, y, col = NA)
  23. + }
  24. >
  25. > op <- par(mfrow = c(5, 3), mar = c(2, 1, 1, 1))
  26. >
  27. > plotRules(c(.1, .1, .9, .9, NA, .2, .2, .8, .8),
  28. + c(.1, .9, .9, .1, NA, .2, .8, .8, .2),
  29. + "Nested rectangles, both clockwise")
  30. > plotRules(c(.1, .1, .9, .9, NA, .2, .8, .8, .2),
  31. + c(.1, .9, .9, .1, NA, .2, .2, .8, .8),
  32. + "Nested rectangles, outer clockwise, inner anti-clockwise")
  33. > plotRules(c(.1, .1, .4, .4, NA, .6, .9, .9, .6),
  34. + c(.1, .4, .4, .1, NA, .6, .6, .9, .9),
  35. + "Disjoint rectangles")
  36. > plotRules(c(.1, .1, .6, .6, NA, .4, .4, .9, .9),
  37. + c(.1, .6, .6, .1, NA, .4, .9, .9, .4),
  38. + "Overlapping rectangles, both clockwise")
  39. > plotRules(c(.1, .1, .6, .6, NA, .4, .9, .9, .4),
  40. + c(.1, .6, .6, .1, NA, .4, .4, .9, .9),
  41. + "Overlapping rectangles, one clockwise, other anti-clockwise")
  42. >
  43. > par(op)
  44. > require(grDevices)
  45. > ## set up the plot region:
  46. > op <- par(bg = "thistle")
  47. > plot(c(100, 250), c(300, 450), type = "n", xlab="", ylab="",
  48. + main = "2 x 11 rectangles; 'rect(100+i,300+i, 150+i,380+i)'")
  49. > i <- 4*(0:10)
  50. > ## draw rectangles with bottom left (100, 300)+i
  51. > ## and top right (150, 380)+i
  52. > rect(100+i, 300+i, 150+i, 380+i, col=rainbow(11, start=.7,end=.1))
  53. > rect(240-i, 320+i, 250-i, 410+i, col=heat.colors(11), lwd=i/5)
  54. > ## Background alternating ( transparent / "bg" ) :
  55. > j <- 10*(0:5)
  56. > rect(125+j, 360+j, 141+j, 405+j/2, col = c(NA,0),
  57. + border = "gold", lwd = 2)
  58. > rect(125+j, 296+j/2, 141+j, 331+j/5, col = c(NA,"midnightblue"))
  59. > mtext("+ 2 x 6 rect(*, col = c(NA,0)) and col = c(NA,\"m..blue\"))")

多边形

路径

矩形

arrows用于画箭头,symbols用于画符号

  1. > ## Note that example(trees) shows more sensible plots!
  2. > N <- nrow(trees)
  3. > with(trees, {
  4. + ## Girth is diameter in inches
  5. + symbols(Height, Volume, circles = Girth/24, inches = FALSE,
  6. + main = "Trees' Girth") # xlab and ylab automatically
  7. + ## Colours too:
  8. + op <- palette(rainbow(N, end = 0.9))
  9. + symbols(Height, Volume, circles = Girth/16, inches = FALSE, bg = 1:N,
  10. + fg = "gray30", main = "symbols(*, circles = Girth/16, bg = 1:N)")
  11. + palette(op)
  12. + })

符号

画圆

  1. > library(plotrix)
  2. > plot(1:5,seq(1,10,length=5),type="n",xlab="",ylab="",main="Test draw.circle")
  3. > draw.circle(2,4,c(1,0.66,0.33),border="purple",
  4. + col=c("#ff00ff","#ff77ff","#ffccff"),lty=1,lwd=1)
  5. > draw.circle(2.5,8,0.6,border="red",lty=3,lwd=3)
  6. > draw.circle(4,3,0.7,border="green",lty=1,lwd=1)
  7. > draw.circle(3.5,7,0.8,border="blue",lty=2,lwd=2)

散点图及趋势线

一维点图使用dotchart函数。

  1. > # Dotplot: Grouped Sorted and Colored
  2. > # Sort by mpg, group and color by cylinder
  3. > x <- mtcars[order(mtcars$mpg),] # sort by mpg
  4. > x$cyl <- factor(x$cyl) # it must be a factor
  5. > x$color[x$cyl==4] <- "red"
  6. > x$color[x$cyl==6] <- "blue"
  7. > x$color[x$cyl==8] <- "darkgreen"
  8. > dotchart(x$mpg,labels=row.names(x),cex=.7,groups= x$cyl,
  9. + main="Gas Milage for Car Models\ngrouped by cylinder",
  10. + xlab="Miles Per Gallon", gcolor="black", color=x$color)

一维点图

二维散点图使用plot函数。直趋势线使用abline函数,拟合曲线在拟合后使用line函数绘制。

  1. > attach(mtcars)
  2. > plot(wt, mpg, main="Scatterplot Example",
  3. + xlab="Car Weight ", ylab="Miles Per Gallon ", pch=19)
  4. > # Add fit lines
  5. > abline(lm(mpg~wt), col="red") # regression line (y~x)
  6. > lines(lowess(wt,mpg), col="blue") # lowess line (x,y)

点阵图

曲线

曲线使用lines函数。其type参数可以使用”p”,”l”,”o”,”b,c”,”s,S”,”h”,”n”等。

  1. > x <- c(1:5); y <- x # create some data
  2. > par(pch=22, col="blue") # plotting symbol and color
  3. > par(mfrow=c(2,4)) # all plots on one page
  4. > opts = c("p","l","o","b","c","s","S","h")
  5. > for(i in 1:length(opts)){
  6. + heading = paste("type=",opts[i])
  7. + plot(x, y, main=heading)
  8. + lines(x, y, type=opts[i])
  9. + }

曲线

柱状图

普通的柱状图使用barplot函数。其参数horiz=TRUE表示水平画图,beside=TRUE表示如果是多组数据的话,在并排画图,否则原位堆叠画图。

  1. > par(mfrow=c(1,2))
  2. > counts <- table(mtcars$vs, mtcars$gear)
  3. > barplot(counts, main="Car Distribution by Gears and VS",
  4. + xlab="Number of Gears", col=c("darkblue","red"),
  5. + legend = rownames(counts),horiz=TRUE)
  6. > barplot(counts, main="Car Distribution by Gears and VS",
  7. + xlab="Number of Gears", col=c("darkblue","red"),
  8. + legend = rownames(counts), beside=TRUE)

bar图

柱状统计图使用hist函数。其breaks参数设置每组的范围。使用density函数可以拟合曲线。

  1. > hist(mtcars$mpg, breaks=12)
  2. > dens<-density(mtcars$mpg)
  3. > lines(dens$x,dens$y*100,col="red")

柱状图

饼图

饼图使用pie函数。

  1. > x<-table(mtcars$gear)
  2. > pie(x,label=paste("gear=",rownames(x),sep=""))

饼图

3维饼图使用plotrix库中的pie3D函数。

  1. > x<-table(mtcars$gear)
  2. > pie3D(x,labels=paste("gear=",rownames(x),sep=""),explode=0.1)

3D饼图

箱线图

箱线图使用boxplot函数。boxplot中参数x为公式。R中的公式如何定义呢?最简单的 y ~ x 就是y是x的一次函数。好了,下面就是相关的符号代表的意思:

符号 示例 意义
+ +x 包括该变量
-x 不包括该变量
: x:z 包括两变量的相互关系
* x*z 包括两变量,以及它们之间的相互关系
/ x/z nesting: include z nested within x
| x|z 条件或分组:包括指定z的x
^ (u+v+w)^3 include these variables and all interactions up to three way
poly poly(x,3) polynomial regression: orthogonal polynomials
Error Error(a/b) specify the error term
I I(x*z) as is: include a new variable consisting of these variables multiplied
1 -1 截距:减去该截距
  1. > boxplot(mpg~cyl,data=mtcars, main="Car Milage Data",
  2. + xlab="Number of Cylinders", ylab="Miles Per Gallon")
  3. > boxplot(len~supp*dose, data=ToothGrowth, notch=TRUE,
  4. + col=(c("gold","darkgreen")),
  5. + main="Tooth Growth", xlab="Suppliment and Dose")

箱线图

如果我想在箱线图上叠加样品点,即所谓的蜂群图,如何做呢?

  1. > source("http://bioconductor.org/biocLite.R")
  2. > biocLite(c("beeswarm","ggplot2"))
  3. > library(beeswarm)
  4. > library(ggplot2)
  5. > data(breast)
  6. > beeswarm <- beeswarm(time_survival ~ event_survival,
  7. + data = breast, method = 'swarm',
  8. + pwcol = ER)[, c(1, 2, 4, 6)]
  9. > colnames(beeswarm) <- c("x", "y", "ER", "event_survival")
  10. >
  11. > beeswarm.plot <- ggplot(beeswarm, aes(x, y)) +
  12. + xlab("") +
  13. + scale_y_continuous(expression("Follow-up time (months)"))
  14. > beeswarm.plot2 <- beeswarm.plot + geom_boxplot(aes(x, y,
  15. + group = round(x)), outlier.shape = NA)
  16. > beeswarm.plot3 <- beeswarm.plot2 + geom_point(aes(colour = ER)) +
  17. + scale_colour_manual(values = c("black", "red")) +
  18. + scale_x_continuous(breaks = c(1:2),
  19. + labels = c("Censored", "Metastasis"), expand = c(0, 0.5))
  20. > print(beeswarm.plot3)

蜂群图

  1. > require(beeswarm)
  2. > data(breast)
  3. >
  4. > beeswarm(time_survival ~ event_survival, data = breast,
  5. + method = 'swarm',
  6. + pch = 16, pwcol = as.numeric(ER),
  7. + xlab = '', ylab = 'Follow-up time (months)',
  8. + labels = c('Censored', 'Metastasis'))
  9. >
  10. > boxplot(time_survival ~ event_survival,
  11. + data = breast, add = T,
  12. + names = c("",""), col="#0000ff22")

蜂群图

分枝树

  1. > require(graphics)
  2. > opar<-par(mfrow=c(2,1),mar=c(4,3,0.5,0.5))
  3. > hc <- hclust(dist(USArrests), "ave")
  4. > plot(hc,main="")
  5. > plot(hc, hang = -1,main="")
  6. > par(opar)

分枝树

文氏图

  1. > library(limma)
  2. > Y <- matrix(rnorm(100*6),100,6)
  3. > Y[1:10,3:4] <- Y[1:10,3:4]+3
  4. > Y[1:20,5:6] <- Y[1:20,5:6]+3
  5. > design <- cbind(1,c(0,0,1,1,0,0),c(0,0,0,0,1,1))
  6. > fit <- eBayes(lmFit(Y,design))
  7. > results <- decideTests(fit)
  8. > a <- vennCounts(results)
  9. > print(a)
  10. x1 x2 x3 Counts
  11. [1,] 0 0 0 89
  12. [2,] 0 0 1 11
  13. [3,] 0 1 0 0
  14. [4,] 0 1 1 0
  15. [5,] 1 0 0 0
  16. [6,] 1 0 1 0
  17. [7,] 1 1 0 0
  18. [8,] 1 1 1 0
  19. attr(,"class")
  20. [1] "VennCounts"
  21. > vennDiagram(a)
  22. > vennDiagram(results,include=c("up","down"),counts.col=c("red","green"))

文氏图

七,综合应用

点阵图加两方向柱状图

  1. > def.par <- par(no.readonly = TRUE) # save default, for resetting...
  2. >
  3. > x <- pmin(3, pmax(-3, rnorm(50)))
  4. > y <- pmin(3, pmax(-3, rnorm(50)))
  5. > xhist <- hist(x, breaks=seq(-3,3,0.5), plot=FALSE)
  6. > yhist <- hist(y, breaks=seq(-3,3,0.5), plot=FALSE)
  7. > top <- max(c(xhist$counts, yhist$counts))
  8. > xrange <- c(-3,3)
  9. > yrange <- c(-3,3)
  10. > nf <- layout(matrix(c(2,0,1,3),2,2,byrow=TRUE), c(3,1), c(1,3), TRUE)
  11. > #layout.show(nf)
  12. >
  13. > par(mar=c(3,3,1,1))
  14. > plot(x, y, xlim=xrange, ylim=yrange, xlab="", ylab="")
  15. > par(mar=c(0,3,1,1))
  16. > barplot(xhist$counts, axes=FALSE, ylim=c(0, top), space=0)
  17. > par(mar=c(3,0,1,1))
  18. > barplot(yhist$counts, axes=FALSE, xlim=c(0, top), space=0, horiz=TRUE)
  19. >
  20. > par(def.par)

点阵+柱状

带误差的点线图

  1. > # Clustered Error Bar for Groups of Cases.
  2. > # Example: Experimental Condition (Stereotype Threat Yes/No) x Gender (Male / Female)
  3. > # The following values would be calculated from data and are set fixed now for
  4. > # code reproduction
  5. >
  6. > means.females <- c(0.08306698, -0.83376319)
  7. > stderr.females <- c(0.13655378, 0.06973371)
  8. >
  9. > names(means.females) <- c("No","Yes")
  10. > names(stderr.females) <- c("No","Yes")
  11. >
  12. > means.males <- c(0.4942997, 0.2845608)
  13. > stderr.males <- c(0.07493673, 0.18479661)
  14. >
  15. > names(means.males) <- c("No","Yes")
  16. > names(stderr.males) <- c("No","Yes")
  17. >
  18. > # Error Bar Plot
  19. >
  20. > library (gplots)
  21. >
  22. > # Draw the error bar for female experiment participants:
  23. > plotCI(x = means.females, uiw = stderr.females, lty = 2, xaxt ="n", xlim = c(0.5,2.5), ylim = c(-1,1), gap = 0, ylab="Microworld Performance (Z Score)", xlab="Stereotype Threat", main = "Microworld performance over experimental conditions")
  24. >
  25. > # Add the males to the existing plot
  26. > plotCI(x = means.males, uiw = stderr.males, lty = 1, xaxt ="n", xlim = c(0.5,2.5), ylim = c(-1,1), gap = 0, add = TRUE)
  27. >
  28. > # Draw the x-axis (omitted above)
  29. > axis(side = 1, at = 1:2, labels = names(stderr.males), cex = 0.7)
  30. >
  31. > # Add legend for male and female participants
  32. > legend(2,1,legend=c("Male","Female"),lty=1:2)

误差图

误差绘图plotCI加强版是plotmeans,具体可使用?plotmeans来试用它。这里就不多讲了。

带误差的柱状图。还是使用gplots包

  1. > library(gplots)
  2. > # Example with confidence intervals and grid
  3. > hh <- t(VADeaths)[, 5:1]
  4. > mybarcol <- "gray20"
  5. > ci.l <- hh * 0.85
  6. > ci.u <- hh * 1.15
  7. > mp <- barplot2(hh, beside = TRUE,
  8. + col = c("lightblue", "mistyrose",
  9. + "lightcyan", "lavender"),
  10. + legend = colnames(VADeaths), ylim = c(0, 100),
  11. + main = "Death Rates in Virginia", font.main = 4,
  12. + sub = "Faked 95 percent error bars", col.sub = mybarcol,
  13. + cex.names = 1.5, plot.ci = TRUE, ci.l = ci.l, ci.u = ci.u,
  14. + plot.grid = TRUE)
  15. > mtext(side = 1, at = colMeans(mp), line = 2,
  16. + text = paste("Mean", formatC(colMeans(hh))), col = "red")
  17. > box()

带误差的柱状图

漂亮的箱线图

  1. > require(gplots) #for smartlegend
  2. >
  3. > data(ToothGrowth)
  4. > boxplot(len ~ dose, data = ToothGrowth,
  5. + boxwex = 0.25, at = 1:3 - 0.2,
  6. + subset= supp == "VC", col="yellow",
  7. + main="Guinea Pigs' Tooth Growth",
  8. + xlab="Vitamin C dose mg",
  9. + ylab="tooth length", ylim=c(0,35))
  10. > boxplot(len ~ dose, data = ToothGrowth, add = TRUE,
  11. + boxwex = 0.25, at = 1:3 + 0.2,
  12. + subset= supp == "OJ", col="orange")
  13. >
  14. > smartlegend(x="left",y="top", inset = 0,
  15. + c("Ascorbic acid", "Orange juice"),
  16. + fill = c("yellow", "orange"))

箱线图

基因芯片热图,具体参考http://www2.warwick.ac.uk/fac/sci/moac/students/peter_cock/r/heatmap/

  1. > library("ALL")
  2. > data("ALL")
  3. > eset <- ALL[, ALL$mol.biol %in% c("BCR/ABL", "ALL1/AF4")]
  4. > library("limma")
  5. > f <- factor(as.character(eset$mol.biol))
  6. > design <- model.matrix(~f)
  7. > fit <- eBayes(lmFit(eset,design))
  8. > selected <- p.adjust(fit$p.value[, 2]) <0.005
  9. > esetSel <- eset [selected, ]
  10. > color.map <- function(mol.biol) { if (mol.biol=="ALL1/AF4") "#FF0000" else "#0000FF" }
  11. > patientcolors <- unlist(lapply(esetSel$mol.bio, color.map))
  12. > library("gplots")
  13. > heatmap.2(exprs(esetSel), col=redgreen(75), scale="row", ColSideColors=patientcolors,
  14. + key=TRUE, symkey=FALSE, density.info="none", trace="none", cexRow=0.5)

点柱图(dothistogram)

英文名称 中文名称
bar 条形图
line 线图
area 面积图
pie 饼图
high-low 高低图
pareto 帕累托图
control 控制图
boxplot 箱线图
error bar 误差条图
scatter 散点图
P-P P-P正态概率图
Q-Q Q-Q正态概率图
sequence 序列图
ROC Curve ROC分类效果曲线图
Time Series 时间序列图

好了,言归正传。那么什么又是点柱图(dot histogram)呢?之前我又称之为蜂群图(beeswarm)。还有称之为抖点图(jitter plots)。总之无论如何,在糗世界里我都称之为点柱图吧。

我们先看点柱图效果:

点柱图

以下是代码

  1. > require(beeswarm)
  2. > data(breast)
  3. > head(breast)
  4. ER ESR1 ERBB2 time_survival event_survival
  5. 100.CEL.gz neg 8.372133 13.085894 39 1
  6. 103.CEL.gz pos 10.559356 9.491683 97 0
  7. 104.CEL.gz pos 12.299905 9.599574 11 1
  8. 105.CEL.gz pos 10.776632 9.681747 99 0
  9. 106.CEL.gz pos 10.505124 9.436763 40 1
  10. 107.CEL.gz neg 10.377741 8.695576 94 0
  11. > require(plotrix)
  12. > cluster<-cluster.overplot(breast$event_survival, breast$time_survival)
  13. > png("dothist.png",width=1000,height=1000)
  14. > opar<-par(mfrow=c(3,3))
  15. > plot (breast$event_survival, breast$time_survival, main="Multiple points on coordinate",col=as.numeric(breast$ER),xaxt="n",xlim=c(-1,2))
  16. > axis(1,at=c(0,1),labels=c("Censored","Metastasis"))
  17. > plot(jitter(breast$event_survival), breast$time_survival, main="Using Jitter on x-axis",col=as.numeric(breast$ER),xaxt="n",xlim=c(-0.5,1.5))
  18. > axis(1,at=c(0,1),labels=c("Censored","Metastasis"))
  19. > plot(jitter(breast$event_survival), jitter(breast$time_survival), main="Using Jitter on x and y-axis",col=as.numeric(breast$ER),xaxt="n",xlim=c(-0.5,1.5))
  20. > axis(1,at=c(0,1),labels=c("Censored","Metastasis"))
  21. > sunflowerplot(breast$event_survival, breast$time_survival, main="Using Sunflowers",xaxt="n",xlim=c(-0.5,1.5))
  22. > axis(1,at=c(0,1),labels=c("Censored","Metastasis"))
  23. > plot(cluster, main="Using cluster.overplot",col=as.numeric(breast$ER),xaxt="n",xlim=c(-0.5,1.5))
  24. > axis(1,at=c(0,1),labels=c("Censored","Metastasis"))
  25. > count.overplot(jitter(breast$event_survival), jitter(breast$time_survival), main="Using cout.overplot",col=as.numeric(breast$ER),xaxt="n")
  26. > axis(1,at=c(0,1),labels=c("Censored","Metastasis"))
  27. > sizeplot(breast$event_survival, breast$time_survival, main="Using sizeplot",col=as.numeric(breast$ER),xaxt="n",xlim=c(-0.5,1.5))
  28. > axis(1,at=c(0,1),labels=c("Censored","Metastasis"))
  29. > beeswarm(time_survival ~ event_survival, data = breast,
  30. + method = 'swarm',
  31. + pch = 16, pwcol = as.numeric(ER),
  32. + xlab = '', ylab = 'Follow-up time (months)',
  33. + labels = c('Censored', 'Metastasis'))
  34. > dev.off()
  35. quartz
  36. 2
  37. > par(opar)

以下是解释

在很多情况下,我们画散点图的时候,有许多点拥有相同的横坐标,如果我们简单的使用plot(x,y)的方式,会显得这些点拥挤在一起,象图中左上角一样,非常的不舒服。我们需要把这些点分散开。

最基本的思路是,把横坐标抖散(jitter),使本来都拥有相同坐标的点的横坐标稍有不同。jitter是基类函数{base},无需调用任何包。

  1. > plot(jitter(breast$event_survival), breast$time_survival, main="Using Jitter on x-axis",col=as.numeric(breast$ER),xaxt="n",xlim=c(-0.5,1.5))
  2. > axis(1,at=c(0,1),labels=c("Censored","Metastasis"))
  3. > plot(jitter(breast$event_survival), jitter(breast$time_survival), main="Using Jitter on x and y-axis",col=as.numeric(breast$ER),xaxt="n",xlim=c(-0.5,1.5))
  4. > axis(1,at=c(0,1),labels=c("Censored","Metastasis"))

我们比较图中上边靠右的两个图,我们发现,如果只抖散x坐标的话,还是有些点会粘在一起,所以同时抖散y坐标会好一些。我们可以使用factor参数来控制jitter抖散的强度。

  1. > plot(rep(c(1,5,10),each=5),c(jitter(rep(100,5),factor=1),jitter(rep(100,5),factor=5),jitter(rep(100,5),factor=10)),col=c("red","blue","green","gray","black"),xlim=c(-2,13),xlab="",ylab="y",xaxt="n",main="jitter(rep(100,5)) with different factor")
  2. > axis(1,at=c(1,5,10),labels=c(paste("factor=",c(1,5,10),sep="")))

不同强度的jitter

在graphics包中提供了一个sunflowerplot的函数。它的目的是用花瓣数目多少来显示落在同一坐标上的点的数目。但是从中左图看来,点多的时候效果并非总是那么好。

在plotrix包中提供了一些有意思的函数来解决点挤在一起的这个问题,它们分别是cluster.overplot, count.overplot, sizeplot。这三个函数的效果如图中及下靠左的两个。cluster.overplot的方法类似抖散,count overplot的方法是使用数字来显示落在同一坐标上的点的数目,sizeplot的方法是使用不同大小是点来显示落在同一坐标上的点的数目。从效果来看,点多的时候效果也并非理想。

而上一次提到过的蜂群图似乎是解决这一问题的最佳方案。

我们得出结论,在点数不同的情况下,使用plotrix包及sunflowerplot是不错的。但点数较多的情况下,还是使用jitter和beeswarm较为稳妥。

我们也可以使用ggplot2包中的geom来绘制点柱图

  1. > require(beeswarm)
  2. > data(breast)
  3. > library(ggplot2)
  4. > p<-ggplot(breast, aes(event_survival,time_survival))
  5. > print(p+geom_jitter(aes(color=ER))+scale_colour_manual(value = c("black", "red"))+scale_x_continuous(breaks = c(0:1),labels = c("Censored", "Metastasis")))

ggplot点柱图

还有一种图,名称为Engelmann-Hecker-Plot, 由plotrix的ehplot来实现。

  1. > data(iris);library(plotrix)
  2. > ehplot(iris$Sepal.Length, iris$Species,
  3. + intervals=20, cex=1.8, pch=20, main="pch=20")
  4. > ehplot(iris$Sepal.Width, iris$Species,
  5. + intervals=20, box=TRUE, median=FALSE, main="box=TRUE")
  6. > ehplot(iris$Petal.Length, iris$Species,
  7. + pch=17, col="red", log=TRUE, main="pch=17")
  8. > ehplot(iris$Petal.Length, iris$Species,
  9. + offset=0.06, pch=as.numeric(iris$Species), main="pch=as.numeric(iris$Species)")
  10. > rnd <- sample(150)
  11. > plen <- iris$Petal.Length[rnd]
  12. > pwid <- abs(rnorm(150, 1.2))
  13. > spec <- iris$Species[rnd]
  14. > ehplot(plen, spec, pch=19, cex=pwid,
  15. + col=rainbow(3, alpha=0.6)[as.numeric(spec)], main="cex and col changes")

坐标中断(axis breaks)

R当中的坐标中断一般都使用plotrix库中的axis.break(), gap.plot(), gap.barplot(), gap.boxplot()等几个函数来实现,例:

gap plot

  1. > library(plotrix)
  2. > opar<-par(mfrow=c(3,2))
  3. > plot(sample(5:7,20,replace=T),main="Axis break test",ylim=c(2,8))
  4. > axis.break(axis=2,breakpos=2.5,style="gap")
  5. > axis.break(axis=2,breakpos=3.5,style="slash")
  6. > axis.break(axis=2,breakpos=4.5,style="zigzag")
  7. > twogrp<-c(rnorm(5)+4,rnorm(5)+20,rnorm(5)+5,rnorm(5)+22)
  8. > gap.plot(twogrp,gap=c(8,16,25,35),
  9. + xlab="X values",ylab="Y values",xlim=c(1,30),ylim=c(0,25),
  10. + main="Test two gap plot with the lot",xtics=seq(0,30,by=5),
  11. + ytics=c(4,6,18,20,22,38,40,42),
  12. + lty=c(rep(1,10),rep(2,10)),
  13. + pch=c(rep(2,10),rep(3,10)),
  14. + col=c(rep(2,10),rep(3,10)),
  15. + type="b")
  16. > gap.plot(21:30,rnorm(10)+40,gap=c(8,16,25,35),add=TRUE,
  17. + lty=rep(3,10),col=rep(4,10),type="l")
  18. > gap.barplot(twogrp,gap=c(8,16),xlab="Index",ytics=c(3,6,17,20),
  19. + ylab="Group values",main="Barplot with gap")
  20. > gap.barplot(twogrp,gap=c(8,16),xlab="Index",ytics=c(3,6,17,20),
  21. + ylab="Group values",horiz=TRUE,main="Horizontal barplot with gap")
  22. > twovec<-list(vec1=c(rnorm(30),-6),vec2=c(sample(1:10,40,TRUE),20))
  23. > gap.boxplot(twovec,gap=list(top=c(12,18),bottom=c(-5,-3)),
  24. + main="Show outliers separately")
  25. > gap.boxplot(twovec,gap=list(top=c(12,18),bottom=c(-5,-3)),range=0,
  26. + main="Include outliers in whiskers")
  27. > par(opar)

从图像效果上来看,这样的坐标中断只能说实现了坐标中断,但效果上是非常一般的。甚至远不如excel, openoffice当中出图效果好。为此,我们需要对plotrix库中的gap.plot做出修改,以达到满意的效果。

最简单的修改办法就是在使用了gap.plot, gap.barplot, gap.boxplot之后重新使用axis.break来修改中断类型,使得看上去美一点。

  1. > axis.break(2,from,breakcol="snow",style="gap")
  2. > axis.break(2,from*(1+0.02),breakcol="black",style="slash")
  3. > axis.break(4,from*(1+0.02),breakcol="black",style="slash")
  4. > axis(2,at=from)

使用上面的办法可以绘制出双反斜线中断,并可以视实际情况加油断点起止位置。

改进的gap.plot

  1. > library(plotrix)
  2. > x<-c(1:5,6.9,7)
  3. > y<-2^x
  4. > from<-33
  5. > to<-110
  6. > opar<-par(mfrow=c(2,2))
  7. > plot(x,y,type="b",main="normal plot")
  8. > gap.plot(x,y,gap=c(from,to),type="b",main="gap plot")
  9. > axis.break(2,from,breakcol="snow",style="gap")
  10. > axis.break(2,from*(1+0.02),breakcol="black",style="slash")
  11. > axis.break(4,from*(1+0.02),breakcol="black",style="slash")
  12. > axis(2,at=from)
  13. > gap.barplot(y,gap=c(from,to),col=as.numeric(x),main="barplot with gap")
  14. > axis.break(2,from,breakcol="snow",style="gap")
  15. > axis.break(2,from*(1+0.02),breakcol="black",style="slash")
  16. > axis.break(4,from*(1+0.02),breakcol="black",style="slash")
  17. > axis(2,at=from)
  18. > gap.barplot(y,gap=c(from,to),col=as.numeric(x),horiz=T,main="Horizontal barplot with gap")
  19. > axis.break(1,from,breakcol="snow",style="gap")
  20. > axis.break(1,from*(1+0.02),breakcol="black",style="slash")
  21. > axis.break(3,from*(1+0.02),breakcol="black",style="slash")
  22. > axis(1,at=from)
  23. > par(opar)

来源:http://blog.qiubio.com:8080/rbioconductor-2

这个总结的也不错,推荐下:  ggplot2绘制Excel所有图

R绘图基础的更多相关文章

  1. 基础R绘图

    前言: 在前面介绍了R的基础入门语法之后,现也将最近整理好的一些R的基础绘图实例提供给需要的朋友参考.(温馨提示:代码慎用!按照本博文实例进行练习的话最好能做到举一反三.代码多敲方为上策,切不可隔岸观 ...

  2. R语言基础画图/绘图/作图

    R语言基础画图/绘图/作图 R语言基础画图 R语言免费且开源,其强大和自由的画图功能,深受广大学生和可视化工作人员喜爱,这篇文章对如何使用R语言作基本的图形,如直方图,点图,饼状图以及箱线图进行简单介 ...

  3. Android中Canvas绘图基础详解(附源码下载) (转)

    Android中Canvas绘图基础详解(附源码下载) 原文链接  http://blog.csdn.net/iispring/article/details/49770651   AndroidCa ...

  4. GDI+(一):GDI+ 绘图基础

    一.GDI+绘图基础 编写图形程序时需要使用GDI(Graphics Device Interface,图形设备接口),从程序设计的角度看,GDI包括两部分:一部分是GDI对象,另一部分是GDI函数. ...

  5. r绘图基本

    R绘图命令分为三种类型: 高级绘图命令在图形设备上产生一个新的图区,它可能包括坐标轴,标签,标题等等. 低级画图命令会在一个已经存在的图上加上更多的图形元素,例如额外的点,线和标签. 交互式图形命令允 ...

  6. R语言基础:数组&列表&向量&矩阵&因子&数据框

    R语言基础:数组和列表 数组(array) 一维数据是向量,二维数据是矩阵,数组是向量和矩阵的直接推广,是由三维或三维以上的数据构成的. 数组函数是array(),语法是:array(dadta, d ...

  7. Direct3D11学习:(七)绘图基础——彩色立方体的绘制

    转载请注明出处:http://www.cnblogs.com/Ray1024 一.概述 在前面的几篇文章中,我们详细介绍了Direct3D渲染所需要的数学基础和渲染管道理论知识.从这篇文章开始,我们就 ...

  8. windows游戏编程 绘图基础

    本系列文章由jadeshu编写,转载请注明出处.http://blog.csdn.net/jadeshu/article/details/22451353 作者:jadeshu   邮箱: jades ...

  9. Python Matplotlib绘图基础

    Matplotlib绘图基础 1.Figure和Subplot import numpy as np import matplotlib.pyplot as plt #创建一个Figure fig = ...

随机推荐

  1. for memory long term update

    xargs是一条Unix和类Unix操作系统的常用命令.它的作用是将参数列表转换成小块分段传递给其他命令,以避免参数列表过长的问题. #例如,下面的命令: rm `find /path -type f ...

  2. php初学第一课

    php:强大的脚本语言 B/S:brower:浏览器 server:服务器 C/S: client:客户端 server:服务器 一.php嵌入页面的标记 1. <?php    ?> # ...

  3. 【转】NumPy-快速处理数据

    2.0 简介 标准安装的Python中用列表(list)保存一组值,可以用来当作数组使用,不过由于列表的元素可以是任何对象,因此列表中所保存的是对象的指针(为了保存各种类型的对象,只能牺牲空间).这样 ...

  4. angularjs - 415 (Unsupported Media Type)

    angularJs+springMVC angular表单提交一个user实体时,报 angularjs - 415 (Unsupported Media Type)错误!! 原因是$http({ u ...

  5. JavaScript中的setTimeout和setInterval

    上一篇博文<浏览器中Javascript单线程分析>中描述了浏览器中Javascript单线程的原理. 在此基础上,这篇文章将主要介绍setTimeout/setInterval是如何模拟 ...

  6. 在VS2013中查看C/C++预处理后的文件

    1.右键工程(例子中是myproject),选择[属性],在弹出的对话框中,选择[配置属性]-->[C/C++]-->[预处理器],将[预处理到文件]该为[是],应用,确认. 2.在VS ...

  7. 关于对于IT我自己的见解以及我踩过的坑(需要认真读文章才能理解我所遇到的坑.)

    终于开始下决心写下这篇文章了. 就在写这篇总结文章的前天还是今天,我度过了我的17岁生日,正式踏入了已成年人的路程.生日那天我在想今夜必定要做件比较有意义的事,于是乎我想到两件比较可以证明自己是成年人 ...

  8. js的实参是按值传递还是按引用传递

    1.如果是基本类型,则是按值传递 var str = 'one';function f(string) {    string = 'two';}f(str);console.log(str); // ...

  9. 基于docker+etcd+confd + haproxy构建高可用、自发现的web服务

    基于docker+etcd+confd + haproxy构建高可用.自发现的web服务 2016-05-16 15:12 595人阅读 评论(0) 收藏 举报 版权声明:本文为博主原创文章,未经博主 ...

  10. ios每日一发--仿侧边抽屉效果

    效果图如下 代码实现以及思路下面分析: 代码创建导航控制器 Appdelegate.m中 #import "AppDelegate.h" #import "ViewCon ...