Pat*_* Li 3 plot r ggplot2 gridextra
我有一个循环生成的12个图,我想在一个页面上绘制3行和2列(总共2页).我知道如何在R中做到这一点
pdf("1.pdf")
par(mfrow = c(3, 2))
for (i in 1:12) {
x <- 1:10
y <- 2*x + rnorm(x)
plot(x, y)
}
dev.off()
Run Code Online (Sandbox Code Playgroud)
但如何使用ggplot?
library(ggplot2)
library(grid)
library(gridExtra)
for (i in 1:12) {
x <- 1:10
y <- 2*x + rnorm(x)
qplot(x, y)
}
Run Code Online (Sandbox Code Playgroud)
我想我必须使用grid.arrange但需要一些帮助.谢谢.
你可能想看一下这个cowplot包,它比只使用裸体更具灵活性grid.arrange.
这有点 - 虽然有点不优雅:
library(ggplot2)
library(grid)
library(gridExtra)
lg <- list()
for (i in 1:12) {
x <- 1:10
y <- 2*x + rnorm(x)
lg[[i]] <- qplot(x, y)
}
grid.arrange(lg[[1]],lg[[2]],lg[[3]],lg[[4]],lg[[5]],lg[[6]],nrow=3,ncol=2)
grid.arrange(lg[[7]],lg[[8]],lg[[9]],lg[[10]],lg[[11]],lg[[12]],nrow=3,ncol=2)
Run Code Online (Sandbox Code Playgroud)
另外一个更优雅但有点迟钝的方法是做到这grid.arrange一点(感谢Axeman和甜菜根 - 请注意评论).
do.call(grid.arrange, c(lg[1:6], nrow = 3))
do.call(grid.arrange, c(lg[7:12], nrow = 3))
Run Code Online (Sandbox Code Playgroud)
或这个:
grid.arrange(grobs = lg[1:6], ncol=2)
grid.arrange(grobs = lg[7:12], ncol=2)
Run Code Online (Sandbox Code Playgroud)
它们都导致了这一点 - (想想其中两个 - 无论如何它们看起来都一样):
| 归档时间: |
|
| 查看次数: |
2603 次 |
| 最近记录: |