【问题标题】:Multiple ggarrange plots with dataframe list in R with sapplyR中带有数据框列表的多个ggarrange图与sapply
【发布时间】:2020-10-05 03:35:21
【问题描述】:

我正在尝试对数据框列表进行两个 ggplots

我有这个清单:

list.function <-  function() { 
  
  sample1 <- data.frame(gene_biotype= c("protein_coding", "lncRNA", "intronic"), counts = c(1, 1, 1))
  sample2 <- data.frame(gene_biotype= c("protein_coding", "lncRNA", "intronic"), counts = c(2, 2, 2))
  sample3 <- data.frame(gene_biotype= c("protein_coding", "lncRNA", "intronic"), counts = c(3, 3, 3))
  sample4 <- data.frame(gene_biotype= c("protein_coding", "lncRNA", "intronic"), counts = c(4, 4, 4))
  
  sapply(paste('sample', seq(1,4,1), sep=''), get, environment(), simplify = FALSE) 
} 

my.list3 <- list.function()
my.list3

我想做这两个情节


a = ggplot(sampleX, aes(y=count, x = gene_biotype, fill = gene_biotype)) + geom_bar(stat = "identity") +
  xlab("Groups") + 
  ylab("Counts") +
  theme_classic() +
  ggtitle(paste0(samplenumber))

b = ggplot(sampleX, aes(y=count, x = gene_biotype, fill = gene_biotype)) + geom_bar(stat = "identity") +
  xlab("Groups") + 
  ylab("Counts") +
  theme_classic() +
  ggtitle(paste0(samplenumber))

png(samplename, width = 10, height = 5, units = "in", res=200)
ggarrange(a, b)
dev.off()

然后将每个图形打印成 png

【问题讨论】:

    标签: r loops ggplot2 sapply


    【解决方案1】:

    您可以使用purrr::imap 获取地块列表:

    library(ggplot2)
    
    plot_list <- purrr::imap(my.list3, ~{
      ggplot(.x, aes(y=count, x = gene_biotype, fill = gene_biotype)) + 
        geom_bar(stat = "identity") +
        xlab("Groups") + 
        ylab("Counts") +
        theme_classic() +
        ggtitle(.y)
    })
    

    请注意,imap 是访问列表及其名称的便捷方式。您也可以使用基础 R 中的 apply 系列函数来做到这一点。例如,使用 Map

    plot_list <- Map(function(p, q) {
      ggplot(p, aes(y=count, x = gene_biotype, fill = gene_biotype)) + 
          geom_bar(stat = "identity") +
          xlab("Groups") + 
          ylab("Counts") +
          theme_classic() +
          ggtitle(q)
      },my.list3, names(my.list3))
    

    要在 pdf 中编写每个图,我认为您可以使用此处的代码 - Printing multiple ggplots into a single pdf, multiple plots per page

    【讨论】:

      【解决方案2】:

      希望此解决方案可以适应您的需求:

      library(tidyverse)
      library(ggpubr)
      
      for (f in seq_along(my.list3)) {
        a <- ggplot(data = my.list3[[f]], aes(y = counts,
                                     x = gene_biotype,
                                     fill = gene_biotype)) +
          geom_bar(stat = "identity") +
          xlab("Groups") + 
          ylab("Counts") +
          theme_classic() +
          ggtitle(names(my.list3[f]))
        
        b <- ggplot(data = my.list3[[f]], aes(y = counts,
                                              x = gene_biotype,
                                              fill = gene_biotype)) +
          geom_bar(stat = "identity") +
          xlab("Groups") + 
          ylab("Counts") +
          theme_classic() +
          ggtitle(names(my.list3[f]))
          ggsave(filename = paste(names(my.list3[f]), ".png", sep = ""),
                plot = ggarrange(a, b, ncol = 1, nrow = 2, labels = c("A", "B")),
                device = "png", width = 10, height = 5, units = "in")
      }
      

      编辑:每条评论更新 ggsave() 选项

      【讨论】:

      • 我正在尝试使用 png(filename = paste(names(named.list6[f])), width = 10, height = 5, units = "in", res=200) ggarrange (a, b, ncol = 1, nrow = 2, labels = c("A", "B")) dev.off() 但我的代码最终在每个文件的末尾都没有 .png 也没有图像.有什么想法吗?
      • 我已经更新了我的示例以包含这些选项。如果您运行这个“最小可重现示例”,您会得到正确的输出吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-02
      • 1970-01-01
      • 2022-07-07
      • 2021-11-15
      • 1970-01-01
      • 2021-11-27
      相关资源
      最近更新 更多