在多页 pdf 中绘制多个 ggplots(每页一个或多个图)
plotting multiple ggplots in a several page pdf (one or several plots per page)
我正在尝试生成一个类似仪表板的 pdf(每个物种一页),其中包含 3 个不同的地块(每个物种),我想将此输出保存为 pdf。我尝试了几个想法,reprex 取自 this link 并且看起来它应该适用于我在长 运行 中需要的东西。但是我无法让它工作
整理一些数据和图表
df = data.frame(x = seq(1:30), y = rnorm(30), z = runif(30, 1, 10))
# Example plots
p.1 = ggplot(df, aes(x = x, y = y)) +
geom_point()
p.2 = ggplot(df, aes(x = x, y = z)) +
geom_point()
p.3 = ggplot(df, aes(x = y, y = z)) +
geom_point()
p.4 = ggplot(df, aes(x = y)) +
geom_histogram()
plots.list = list(p.1, p.2, p.3, p.4) # Make a list of plots
我的愚蠢解决方案工作正常(即遍历每个物种,然后将它们打印成 pdf,然后关闭 pdf)除了 事实上,情节不是' t 使用整个 space 看起来很蠢 ;-).
pdf("myname.pdf", paper="a4r")
for (i in 1:length(plot.list)){print(plot.list[[i]])}
dev.off()
所以这是 reprex 的其余部分
原版
# Generate plots to be saved to pdf, warning the argument to marrangeGrob
# have to be passed using do.call
# nrow (ncol) gives the number of rows (columns) of plots per page
# nrow and ncol have to be specificed inside a list
# Here, we'll obtain 2 plots in rows by page
plots = do.call(marrangeGrob, c(plots.list, list(nrow = 2, ncol = 1)))
# To save to file, here on A4 paper
ggsave("multipage_plot.pdf", plots, width = 21, height = 29.7, units = "cm")
Error in `$<-.data.frame`(`*tmp*`, "wrapvp", value = list(x = 0.5, y = 0.5, :
replacement has 33 rows, data has 30
我错过了什么?
最后解决的办法其实很简单...
### create a layout matrix (nrow and ncol will do the trick too, but you have less options)
layout_mat<-rbind(c(1,1,2),
c(1,1,3))
plots<-marrangeGrob(plot.list, layout_matrix=layout_mat)
ggsave( filename="mypdf.pdf", plots, width=29.7, height=21, units="cm")
这个版本实际上让您可以完全控制绘图大小并使用整个页面!
我正在尝试生成一个类似仪表板的 pdf(每个物种一页),其中包含 3 个不同的地块(每个物种),我想将此输出保存为 pdf。我尝试了几个想法,reprex 取自 this link 并且看起来它应该适用于我在长 运行 中需要的东西。但是我无法让它工作
整理一些数据和图表
df = data.frame(x = seq(1:30), y = rnorm(30), z = runif(30, 1, 10))
# Example plots
p.1 = ggplot(df, aes(x = x, y = y)) +
geom_point()
p.2 = ggplot(df, aes(x = x, y = z)) +
geom_point()
p.3 = ggplot(df, aes(x = y, y = z)) +
geom_point()
p.4 = ggplot(df, aes(x = y)) +
geom_histogram()
plots.list = list(p.1, p.2, p.3, p.4) # Make a list of plots
我的愚蠢解决方案工作正常(即遍历每个物种,然后将它们打印成 pdf,然后关闭 pdf)除了 事实上,情节不是' t 使用整个 space 看起来很蠢 ;-).
pdf("myname.pdf", paper="a4r")
for (i in 1:length(plot.list)){print(plot.list[[i]])}
dev.off()
所以这是 reprex 的其余部分
原版
# Generate plots to be saved to pdf, warning the argument to marrangeGrob
# have to be passed using do.call
# nrow (ncol) gives the number of rows (columns) of plots per page
# nrow and ncol have to be specificed inside a list
# Here, we'll obtain 2 plots in rows by page
plots = do.call(marrangeGrob, c(plots.list, list(nrow = 2, ncol = 1)))
# To save to file, here on A4 paper
ggsave("multipage_plot.pdf", plots, width = 21, height = 29.7, units = "cm")
Error in `$<-.data.frame`(`*tmp*`, "wrapvp", value = list(x = 0.5, y = 0.5, :
replacement has 33 rows, data has 30
我错过了什么?
最后解决的办法其实很简单...
### create a layout matrix (nrow and ncol will do the trick too, but you have less options)
layout_mat<-rbind(c(1,1,2),
c(1,1,3))
plots<-marrangeGrob(plot.list, layout_matrix=layout_mat)
ggsave( filename="mypdf.pdf", plots, width=29.7, height=21, units="cm")
这个版本实际上让您可以完全控制绘图大小并使用整个页面!