循环存多张图片

Liang / 2018-11-24


library(ggpubr)
trait_names <- colnames(Het_Ind)[5:18]
plot <- list()
for (i in 1:length(trait_names)){
  trait_data <- Het_Ind[, c("Genotype", "NRate", trait_names[i])]
  colnames(trait_data) <- c("Genotype", "Nitrogen", "Value")
  trait_data$Genotype <- factor(trait_data$Genotype, levels = sort(c("B73xILP1", "B73xPH207", "B73xIHP1", "B73xPHG84", "B73xLH82", "B73xMo17")))
  trait_data$Nitrogen <- factor(trait_data$Nitrogen, levels = c("LN", "HN"))
  plot[[i]] <- ggplot(trait_data, aes(x=Genotype, y=Value, fill = Nitrogen)) + 
    geom_bar(stat = "identity", position= "dodge", width=0.8) + theme_bw() + 
    geom_text(aes(label=round(trait_data$Value, digits = 2)), 
              position = position_dodge(width = 0.8), size =2.5) +
      labs(title= trait_names[i], x = "Genotype", y = "log2(Hybrid / MP)") + 
      theme(axis.text.x = element_text(angle = 45,size = 8, face = "bold", vjust = 1, hjust = 1), 
            axis.text.y = element_text(size = 8), 
            plot.title = element_text(size = 9), 
            axis.title = element_text(size = 8), plot.margin=unit(rep(1,4),'lines')) +
      scale_fill_manual(values=c("#00BFC4", "#F8766D"))
}
do.call(ggarrange ,plot)
library(gridExtra)
ggsave("arrange2x2.pdf", marrangeGrob(grobs = plot, nrow=2, ncol=2))
marrangeGrob(grobs = plot, nrow=2, ncol=2)
multi.page <- ggpubr::ggarrange(plotlist = plot, nrow = 2, ncol = 2) 
ggpubr::ggexport(multi.page, filename = "multi.page.ggplot2.pdf")

ggpubr使用完美,ggsave和gridExtra都有毛病

最后一次修改于 2018-11-24