Lun*_*una -8 plot r ggplot2 r-grid
我正在为以下代码摸不着头脑.
我跟着这个例子:
如何使用grid.arrange安排任意数量的ggplots?
我想收集这些图并将它们放在3x9网格上,每个网格都有合适的标签......
但它不起作用.生成的pdf仍然是每页一个图 - 因此生成了27个页面.
我试图使用"grid.arrange",然而,函数"plotFunctionWrittenByOtherPeople"是由其他人编写的,它没有返回绘图的句柄......而且它非常复杂.
如何很好地安排情节?
有人可以对此有所了解吗?
非常感谢!
pdf("mytry1.pdf", width = 11, height = 8.5)
par(mfrow=c(3, 9))
for (a in seq(100, 900, by=100))
for (b in c(1, 3, 6))
{
plotFunctionWrittenByOtherPeople(a, b)
}
dev.off()
Run Code Online (Sandbox Code Playgroud)
bde*_*est 13
我想你想要创建一个由ggplot2创建的一堆图的网格布局.不幸的是,par(mfrow=)基本图形功能不适用于ggplot2.使用grid.arrange在gridExtra包.
library(ggplot2)
library(gridExtra)
# Completely fake plotting function.
makePlot = function(a, b) {
dat = data.frame(x=rnorm(a), y=rnorm(a))
p = ggplot(dat, aes(x=x, y=y)) +
geom_point(size=b, alpha=1/b) +
opts(title=paste("a = ", a, ", b = ", b, sep="")) +
opts(plot.title=theme_text(size=12))
return(p)
}
plot_list = list() # Create an empty list to hold plots.
for (b in c(1, 3, 6)) { # I switched a and b loops
for (a in seq(100, 900, by=100)) { # to make the final layout neater.
p = makePlot(a, b)
plot_list = c(plot_list, list(p)) # Add new plot to list.
}
}
pdf("mytry1.pdf", width = 14, height = 6)
do.call(grid.arrange, c(plot_list, list(nrow=3, ncol=9, main="Grid of Plots")))
dev.off()
Run Code Online (Sandbox Code Playgroud)

该plot_list可以创建和输出为PDF更加紧凑.感谢@baptiste的建议mlply,ggsave和arrangeGrob.
library(plyr)
plot_list = mlply(expand.grid(a=seq(100, 900, by=100), b=c(1, 3, 6)), makePlot)
ggsave(filename="grid_1.pdf", height=6, width=14,
plot=do.call(arrangeGrob, c(plot_list, nrow=3, main="Grid of Plots")))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1006 次 |
| 最近记录: |