【问题标题】:ggplot 2: generate one separate box plot per columnggplot2:每列生成一个单独的箱线图
【发布时间】:2017-01-26 11:19:12
【问题描述】:

我正在尝试生成许多单独的图,每个图都绘制每个细胞(=行)的单个基因(=列)的水平。在代码中,我还有两个“单元格”子集,这取决于每个单元格的gene1 是否具有> 0 的值(这由dplyr 处理)。

我在下面的尝试将所有基因/列的值绘制在一个 pdf 图中,一次。关于如何更改代码以为每个基因/列生成一个图的任何建议?

数据集:

          gene1      gene2      gene3      gene4      gene5
cell_1   0.0000   0.279204  25.995400  46.171700  94.234100
cell_2   0.0000  23.456000  77.339800 194.241000 301.234000
cell_3   2.0000  13.100000  45.309200   0.776565   0.000000
cell_4   0.0000  10.500000 107.508000   3.032500   0.000000
cell_5   3.0000   0.000000   0.266139   0.762981 123.371000

代码:

library(ggplot2)
library(dplyr)
library(tidyr)

#Loop making many single box plots

  df3 <- df2 %>% as.data.frame %>% mutate(Cell= rownames(.), positive = df2$gene1>0) %>% 
    gather(., key= gene, value="value", -Cell,-positive) %>% 
    mutate( absolute= abs(value), logabs= log(absolute+1))

  for (i in unique(df3$gene)) {
    geneplot <- df3 %>% ggplot(., aes(x=gene, y=logabs, fill=positive)) +
          geom_boxplot() +
          xlab("Gene") + ylab("Expression level (TPM log)") + 
          theme_classic(base_size = 14, base_family = "Helvetica") +
          theme(axis.text.y=element_text(size=14)) + 
          theme(axis.title.y=element_text(size=14, face="bold")) + 
          theme(axis.text.x=element_text(size=14)) +
          theme(axis.title.x=element_text(size=14, face="bold")) + 
          scale_fill_brewer(palette="Pastel1")
   print(geneplot)
   ggsave(sprintf("%s.png", df3$gene))

   dev.off()

  }

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:
    gene1<-c(0.0000, 0.0000, 2.0000, 0.0000, 3.0000)
    gene2<-c(0.279204, 23.456000, 13.100000 , 10.500000, 3.0000)
    gene3<-c(25.995400, 77.339800, 45.309200, 107.508000, 0.266139)
    gene4<-c(46.171700, 194.241000, 0.776565, 3.032500, 0.762981)
    gene5<-c(94.234100, 301.234000, 0.000000, 0.000000, 3.0000)
    df<-data.frame(gene1, gene2, gene3,gene4,gene5)
    
    df <- df %>% 
        as.data.frame %>% 
        mutate(Cell= rownames(.), positive = df$gene1>0) %>% 
        gather(., key= gene, value="value", -Cell,-positive) %>% 
        mutate( absolute= abs(value), logabs= log(absolute+1))
    
    ggplot(data= df , aes(x=gene, y=logabs, fill=positive))+
        geom_boxplot()+facet_wrap(~ gene)
    

    更新

    我不确定发帖人在问什么,但这里有几种解释:

    实际数据中有额外的基因,其值为 1,因此,使用 facet_wrap(~ gene) 会创建一个额外的不必要的图,如下所示:

    gene1<-c(0.0000, 0.0000, 2.0000, 0.0000, 3.0000)
    gene2<-c(0.279204, 23.456000, 13.100000 , 10.500000, 3.0000)
    gene3<-c(25.995400, 77.339800, 45.309200, 107.508000, 0.266139)
    gene4<-c(46.171700, 194.241000, 0.776565, 3.032500, 0.762981)
    gene5<-c(94.234100, 301.234000, 0.000000, 0.000000, 3.0000)
    gene6<-c(0.0000, 0.0000, 0.0000, 0.0000, 0.0000)
    df<-data.frame(gene1, gene2, gene3,gene4,gene5, gene6)
    
    df <- df %>% 
      as.data.frame %>% 
      mutate(Cell= rownames(.), positive = df$gene1>0) %>% 
      gather(., key= gene, value="value", -Cell,-positive) %>% 
      mutate( absolute= abs(value), logabs= log(absolute+1))
    
    ggplot(data= df , aes(x=gene, y=logabs, fill=positive))+
      geom_boxplot()+facet_wrap(~ gene) 
    

    为避免这种情况,只需简单地运行

    df<-filter(df, value>0)
    
    ggplot(data= df , aes(x=gene, y=logabs, fill=positive))+
      geom_boxplot()+facet_wrap(~ gene) 
    

    获得:

    如果这不是您关心的问题,我深表歉意。也许是您想摆脱没有值的单个基因的中断,就像@Huub Hoofs 指出的那样。为了实现这一点,正如 Huub Hoofs 建议的那样,请尝试以下方法:

    ggplot(data= df , aes(x=gene, y=logabs, fill=positive))+
        geom_boxplot()+facet_grid(~ gene, scales = "free")
    

    ggplot(data= df , aes(x=gene, y=logabs, fill=positive))+
        geom_boxplot(aes(1))+facet_wrap(~ gene)
    

    【讨论】:

    • 谢谢!但是:如果我有大量基因(我在我的清晰数据集中这样做),这个解决方案将不起作用,因为每个箱线图会变得非常瘦,或者每个图表会变得非常宽,因为箱线图没有显示在每个图表仍然有它们的空槽。有没有办法解决这个问题?
    • 秤=“免费”?或 aes(x=1)
    • 这最后两个选项是我想要实现的!不幸的是,当我放入完整的数据集(包含大约 1000 个“基因”列)时,整个数据集不适合一页。我最终只看到图表的“标题”部分。 ggplot 中是否有任何方法可以将 facet_wraps 拆分到各个页面上,还是我需要回到我的(不工作的)循环解决方案?
    • 我现在实际上设法解决了我的额外问题!我停止使用 facet_wrap 函数,而是加载了 ggplus 包并使用了 facet_multiple 函数。我添加了这一行代码: facet_multiple(plot = p, facets = 'gene', ncol = 4, nrow = 4)
    • @Cyrus Mohammadian,根据我的阅读,我认为重点是能够打印多页 pdf。由于我在一次运行中生成了大约 1000-2500 个图表,因此我无法将它们保留在一个页面上。
    猜你喜欢
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    • 1970-01-01
    • 2022-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多