如何将在一个R脚本中创建的多个图保存到png

时间:2019-06-26 13:21:48

标签: r dataframe ggplot2

在一个R脚本中我想保存到png文件中的图很少。我在终端运行脚本,在这4个图中,只有最后一个被创建为png文件。

如何修复它,以便将所有4个图创建并保存为png文件?

library(data.table)
library("ggplot2")

setwd("C:\\Users\\Desktop\\R\\figure_1A-D")

final_df_older <- fread("C:\\Users\\Desktop\\R\\older.csv"), 
                        select=c(3,4))

png("Figure 1a - older_samples.png", width = 500, height = 400)


figure_1_wo_new <- ggplot(data = final_df_older, aes(x=final_df_older$`quntity`, y=final_df_older$`count`))

dev.off()
list.files(pattern = "png")
getwd()


final_df_all <- fread("C:\\Users\\Desktop\\R\\all.csv"), 
                        select=c(3,4))


png("Figure 1b - all)samples.png", width = 500, height = 400)


figure_1_all <- ggplot(data = final_df_all, aes(x=final_df_all$`quntity`, y=final_df_all$`count`))


dev.off()
list.files(pattern = "png")
getwd()


final_df_new <- fread("C:\\Users\\Desktop\\R\\new.csv"), 
                        select=c(3,4))

png("Figure 1c - new_samples.png", width = 500, height = 400)


figure_1_new_only <- ggplot(data = final_df_new, aes(x=final_df_new$`quntity`, y=final_df_new$`count`))


dev.off()
list.files(pattern = "png")
getwd()

library("gridExtra")
library("grid")

png("Figure 1d - merged_plot.png", width = 1500, height = 500)

merged_plot <- grid.arrange(figure_1_wo_new, figure_1_all,figure_1_new_only, ncol=3,
                        top = textGrob("Merged plot",
                                       gp=gpar(fontsize=30,font=3,cex=1,col="darksalmon"),vjust=0.4))
dev.off()
list.files(pattern = "png")
getwd()

0 个答案:

没有答案