匠人府分享 http://blog.sciencenet.cn/u/meiweipingg

博文

[转载]ggplot2-一页多图(不同数据集, 灵活合并)

已有 8970 次阅读 2017-7-10 20:48 |个人分类:R语言|系统分类:科研笔记|关键词:ggplot2,一页多图,不同数据集,R语言| R语言, ggplot2, 一页多图, 不同数据集 |文章来源:转载

本着 “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[[1]])

} 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[[i]], 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



http://blog.sciencenet.cn/blog-651374-1065605.html

上一篇:多个自变量,如何确定哪个更重要?
下一篇:用数据说话系列(5): 非参数检验SteelDwass test和 Dunn test选谁

0

该博文允许注册用户评论 请点击登录 评论 (5 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备14006957 )

GMT+8, 2019-11-23 10:00

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部