【发布时间】:2017-05-06 14:42:18
【问题描述】:
请假设任何数据集。情况:我正在对所有自变量运行 for 循环以创建与因变量的关系(散点图)图。并希望将绘图保存为 pdf,但保存为 1 个文件的 1 或 2 页 pdf,而不是 1 个单独页面中的每个图表(我已经实现)。我正在使用以下案例
第一次尝试使用开发选项
library(ggplot2)
library(gridExtra)
pdf("one.pdf", onefile = TRUE)
for (i in 1:length(dataset))
{
new_plot[[i]]=print(ggplot(data=dataset, aes(x=dataset[[i]], y=loss))+geom_point(size=1, alpha=0.3)+
geom_smooth(method = lm)+
xlab(paste0(colnames(int[i]), '\n', 'R-Squared: ',round(cor(int[[i]],int$loss, use = 'complete.obs'), 2)))+
ylab("log(loss)") + theme_light())
plot_list = c(new_plot[[i]])
grid.arrange(plot_list)
}
dev.off()
第二次尝试使用 ggsave
for (i in 1:length(dataset))
{
new_plot[[i]]=print(ggplot(data=dataset, aes(x=dataset[[i]], y=loss))+geom_point(size=1, alpha=0.3)+
geom_smooth(method = lm)+
xlab(paste0(colnames(int[i]), '\n', 'R-Squared: ',round(cor(int[[i]],int$loss, use = 'complete.obs'), 2)))+
ylab("log(loss)") + theme_light())
m=marrangeGrob(new_plot[[i]],nrow=2, ncol=2)
}
ggsave("one.pdf",m)
两次我都收到错误
Error in gList(data = list(list(x = c(2213.18, 1283.6, 3005.09, 939.85, :
only 'grobs' allowed in "gList"
如果可能的话,请分享如何在每个页面上以 2*2(示例)的形式发布图表。我非常感谢所有帮助。在此先感谢!
【问题讨论】:
标签: r pdf for-loop ggplot2 gridextra