利用 R 软件包 ggplot2 绘制 boxplot 图 数据集 : 三个阶段卵母细胞 (NSN,NSN-SN,SN) 的核内组蛋白集落 ( 分大小 ,large foci,small foci) 的数量 。 一般而言 ,ggplot2 需要的数据 , 每个样本为一行 , 样本信息需要有观测值 , 以及样本的属性信息等 。 文件名 :mydata2.txt ***************************** number oocyteType fociType 18 NSN large 18 NSN large 18 NSN large 15 NSN large 25 NSN large 26 NSN large 26 NSN large 11 NSN large 24 NSN large 16 NSN large 21 NSN large 18 NSN large 24 NSN large 19 NSN large 12 NSN large 20 NSN large 11 NSN large 17 NSN large 14 NSN large 18 NSN large 14 NSN large 23 NSN large 22 NSN large 16 NSN large 17 NSN large 15 NSN large 16 NSN large 19 NSN large 23 NSN large 19 NSN large 14 NSN large 3 SN large 6 SN large 9 SN large 0 SN large 11 SN large 1 SN large 3 SN large 1 SN large 2 SN large 2 SN large 5 SN large 2 SN large 4 SN large 3 SN large 4 SN large 7 SN large 7 SN large 5 SN large 15 NSN-SN large 7 NSN-SN large 2 NSN-SN large 13 NSN-SN large 15 NSN-SN large 9 NSN-SN large 8 NSN-SN large 11 NSN-SN large 13 NSN-SN large 9 NSN-SN large 1 NSN-SN large 19 NSN-SN large 8 NSN-SN large 11 NSN-SN large 13 NSN-SN large 35 NSN small 23 NSN small 12 NSN small 15 NSN small 55 NSN small 28 NSN small 21 NSN small 38 NSN small 17 NSN small 18 NSN small 18 NSN small 37 NSN small 19 NSN small 14 NSN small 41 NSN small 21 NSN small 30 NSN small 7 NSN small 9 NSN small 9 NSN small 8 NSN small 29 NSN small 8 NSN small 15 NSN small 8 NSN small 13 NSN small 14 NSN small 6 NSN small 12 NSN small 5 NSN small 23 NSN small 8 SN small 10 SN small 6 SN small 8 SN small 9 SN small 3 SN small 8 SN small 3 SN small 9 SN small 5 SN small 9 SN small 7 SN small 7 SN small 10 SN small 10 SN small 8 SN small 8 SN small 6 SN small 12 NSN-SN small 18 NSN-SN small 16 NSN-SN small 20 NSN-SN small 15 NSN-SN small 10 NSN-SN small 15 NSN-SN small 12 NSN-SN small 11 NSN-SN small 14 NSN-SN small 16 NSN-SN small 13 NSN-SN small 9 NSN-SN small 14 NSN-SN small 11 NSN-SN small ***************************** (1) 读入数据 mydata3-read.table(mydata3.txt,header=T,sep=\\t) (2) 初步绘图 library(ggplot2) ggplot(data=mydata3, aes(x=oocyteType,y=number))+geom_boxplot(aes(fill=fociType)) 得如下之图 : (3) 加横轴纵轴标签 p - ggplot(data=mydata3, aes(x=oocyteType,y=number))+geom_boxplot(aes(fill=fociType)) p1 - p+labs(x = oocyte developmental stage) p2 - p1+labs(y = XXX foci number) p2 (4) 修改坐标轴标签之属性 p3-p2 + theme(axis.title.x=element_text(color=red, size=15, face=bold)) p4-p3 + theme(axis.title.y=element_text(color=red, size=15, face=bold)) p4 (5) 修改背景 p4 + theme_bw() 背景应该在第一步开始设置 , 中途设置背景 , 将上面设置的标签掩盖了 。 背景修改方式 , 可见 ?theme_bw() (6) 更改 Figure Legend 位置 p5 - p4 + theme_bw() p5+theme(legend.position=c(0.8,0.8)) p5 +theme(axis.title.x=element_text(color=red, size=15, face=bold))+ theme(axis.title.y=element_text(color=red, size=15, face=bold)) (7) 修改颜色 p6 - p5 + scale_fill_manual(values=c(red,blue)) p6 (8) 修改坐标轴刻度标签颜色大小 p6-p6+theme(axis.text.x=element_text(size=13,color=black)) p6-p6+theme(axis.text.y=element_text(size=13,color=black)) p6 (9) 修改 legend p6+guides(fill=guide_legend(title=NULL)) (10) 加图题 p7-p6+guides(fill=guide_legend(title=NULL)) p7+annotate(text,label=my boxplot plot,x=2,y=50,color=Green,size=8)
本着 “Basic Knowledge worth spreading ” 的宗旨,偶尔发现了这个叫做 “multiplot()” 的 R function, 经测有效好用,特转载之,感谢 csdn博主藏锋者 的无私分享。 (补充:利用facet作一页多图可以参考 https://plot.ly/ggplot2/facet/ ) ggplot2 的分面(facet) 可以绘制一页多图, 但是, 必须 是来自 同一个数据集 的图形, 局限性很大 . 如果我们 有多个不同来源的 图形,想绘制到一张图上又该如何处理呢? multiplot() 提供了极为强大的函数功能. multiplot() 可以设置行列, 也可以设置一个矩阵进行布局. # Multiple plot function ## ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects) # - cols: Number of columns in layout# - layout: A matrix specifying the layout. If present, 'cols' is ignored. ## If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE), # then plot 1 will go in the upper left, 2 will go in the upper right, and# 3 will go all the way across the bottom.# ### 代码定义从此处正式开始--------------------------------------------------- multiplot-function(..., plotlist=NULL, file, cols=1, layout=NULL){ library(grid) # Make a list from the ... arguments and plotlist plots-c(list(...), plotlist) numPlots=length(plots) # If layout is NULL, then use 'cols' to determine layout if(is.null(layout)){ # Make the panel # ncol: Number of columns of plots# nrow: Number of rows needed, calculated from # of cols layout-matrix(seq(1, cols*ceiling(numPlots/cols)), ncol=cols, nrow=ceiling(numPlots/cols)) } if(numPlots==1){ print(plots ]) } else { # Set up the page grid.newpage() pushViewport(viewport(layout=grid.layout(nrow(layout), ncol(layout)))) # Make each plot, in the correct location for(i in 1:numPlots){ # Get the i,j matrix positions of the regions that contain this subplot matchidx-as.data.frame(which(layout==i, arr.ind=TRUE)) print(plots ], vp=viewport(layout.pos.row=matchidx$row, layout.pos.col=matchidx$col)) } } } # function代码定义结束------------------------------------------------------------ # 范例 ----------------------------------------------------------------------------- library(ggplot2) p1-ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet, group=Chick))+ geom_line()+ ggtitle(Growth curve for individual chicks) p2-ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet))+ geom_point(alpha=.3)+geom_smooth(alpha=.2, size=1)+ ggtitle(Fitted growth curve per diet) p3-ggplot(subset(ChickWeight, Time==21), aes(x=weight, colour=Diet))+ geom_density()+ ggtitle(Final weight, by diet) p4-ggplot(subset(ChickWeight, Time==21), aes(x=weight, fill=Diet))+ geom_histogram(colour=black, binwidth=50)+facet_grid(Diet~ .)+ ggtitle(Final weight, by diet)+ theme(legend.position=none) # No legend (redundant in this graph) # 合并为一张图------------------------------------------------------------------- multiplot(p1, p2, p3, p4, cols=2) # 友情补充:合并为一张图-------------------------------------------------- # 如果是 奇数张图片 ,想在合并后的图片的 右上角留白 的话,代码如下------ multiplot(p1, p2, none , p4, cols=2) #已测有效 # 注:本文转载自 http://blog.csdn.net/tanzuozhev/article/details/51112223
source: http://zevross.com/blog/2014/08/04/beautiful-plotting-in-r-a-ggplot2-cheatsheet-3/ Beautiful plotting in R_ A ggplot2 cheatsheet _ Technical Tidbits From Spatial A.pdf