ggplot2实现多图合并
来源:互联网 发布:丹尼·格兰杰数据 编辑:程序博客网 时间:2024/05/19 18:48
multiple plots in one page
cookbook-R-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)) } }}
library(ggplot2)# This example uses the ChickWeight dataset, which comes with ggplot2# First plotp1 <- ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet, group=Chick)) + geom_line() + ggtitle("Growth curve for individual chicks")# Second plotp2 <- 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")# Third plotp3 <- ggplot(subset(ChickWeight, Time==21), aes(x=weight, colour=Diet)) + geom_density() + ggtitle("Final weight, by diet")# Fourth plotp4 <- 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)
<pre name="code" class="plain">multiplot(p1, p2, p3, p4, cols=2)
layout <- matrix(c(1, 1, 1, 2, 2, rep(3, 5)), nrow = 2, byrow = TRUE)multiplot(plotlist = list(p1, p2, p3), layout = layout)
在ggplot画图中添加table
library(ggplot2)library(gridExtra)set.seed(1)mydata <- data.frame(a=1:50, b=rnorm(50))mytable <- cbind(sites=c("site 1","site 2","site 3","site 4"),mydata[10:13,])ggplot(mydata,aes(x=a,y=b)) + geom_point(colour="blue") + geom_point(data=mydata[10:13, ], aes(x=a, y=b), colour="red", size=5) + annotation_custom(tableGrob(mytable), xmin=35, xmax=50, ymin=-2.5, ymax=-1)
1 0
- ggplot2实现多图合并
- ggplot2 一页多图
- ggplot2 一页多图
- ggplot2 3.0 分面、一页多图
- ggplot2-分面(facet) 一页多图
- R ggplot2 做图
- R -ggplot2 气泡图
- ggplot2线型图
- ggplot2绘制条形图
- R ggplot2条形图
- R ggplot2线性图
- ggplot2
- ggplot2
- ggplot2
- ggplot2
- ggplot2-一页多图(不同来源, 灵活绘制)
- ggplot2-一页多图(不同来源, 灵活绘制)(转载)
- ggplot2-分面(facet) 一页多图(转载)
- iap内购 无法连接到 iTunes Store (拿坑砸自己)
- 数字图像处理,图像的伪彩色处理
- SQL Server 2008附加数据库提示5120错误解决
- c++中对文本的操作(转)
- Androi 无法创建模拟器
- ggplot2实现多图合并
- Linux下正确使用getifaddrs()函数避免内存泄露
- 程序员素养
- Python oracle中编码问题
- HDU-2089-不要62
- fread和fwrite函数的使用
- 第4题
- sparkSQL1.2.1入门之七:ThriftServer和CLI
- 算法练习必看