4 回答
TA贡献1871条经验 获得超8个赞
{Ben Bolker指出了一个更好的选择- grid.arrange从gridExtra包装中。ggplot2但是,如果您是用户,R Cookbook网站仍然值得点击。}
R Cookbook的此页上有一个很好的multiplot功能代码(绝对值得一游),它对于这种事情很有用。直接从该站点报价:
multiplot <- function(..., plotlist=NULL, cols) {
require(grid)
# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)
numPlots = length(plots)
# Make the panel
plotCols = cols # Number of columns of plots
plotRows = ceiling(numPlots/plotCols) # Number of rows needed, calculated from # of cols
# Set up the page
grid.newpage()
pushViewport(viewport(layout = grid.layout(plotRows, plotCols)))
vplayout <- function(x, y)
viewport(layout.pos.row = x, layout.pos.col = y)
# Make each plot, in the correct location
for (i in 1:numPlots) {
curRow = ceiling(i/plotCols)
curCol = (i-1) %% plotCols + 1
print(plots[[i]], vp = vplayout(curRow, curCol ))
}
}
在3×2布局中尝试6个地块(JD Long的四个地块,以及两个奖励地块!):
set.seed(2)
q1 <- ggplot(data.frame(x=rnorm(50)), aes(x)) + geom_density()
q2 <- ggplot(data.frame(x=rnorm(50)), aes(x)) + geom_density()
q3 <- ggplot(data.frame(x=rnorm(50)), aes(x)) + geom_density()
q4 <- ggplot(data.frame(x=rnorm(50)), aes(x)) + geom_density()
q5 <- ggplot(data.frame(x=rnorm(50)), aes(x)) + geom_density()
q6 <- ggplot(data.frame(x=rnorm(50)), aes(x)) + geom_density()
multiplot(q1, q2, q3, q4, q5, q6, cols=2)
给出这个数字:
如果该功能不能完全满足您的需求,至少可以为您提供一个不错的起点!
TA贡献1821条经验 获得超4个赞
感谢Andrie的评论和Harlan对我之前的问题(!)的回答,我提出了此解决方案,该解决方案实现了我所追求的目标:
set.seed(2)
q1 <- ggplot(data.frame(x=rnorm(50)), aes(x)) + geom_density()
q2 <- ggplot(data.frame(x=rnorm(50)), aes(x)) + geom_density()
q3 <- ggplot(data.frame(x=rnorm(50)), aes(x)) + geom_density()
q4 <- ggplot(data.frame(x=rnorm(50)), aes(x)) + geom_density()
grid.newpage()
pushViewport(viewport(layout=grid.layout(2,2)))
vplayout <- function(x,y) viewport(layout.pos.row=x,layout.pos.col=y)
print(q1,vp=vplayout(1,1))
print(q2,vp=vplayout(1,2))
print(q3,vp=vplayout(2,1))
print(q4,vp=vplayout(2,2))
- 4 回答
- 0 关注
- 817 浏览
添加回答
举报