【问题标题】:Generating multiple plots though for loops通过 for 循环生成多个图
【发布时间】:2021-02-09 21:39:15
【问题描述】:

TL;DR:我想使用基因列表的标准化计数图生成降价。由于这个列表很长(> 100 个基因),我想在一个图形页面上为前 16 个基因生成一个 4x4 图形的“网格”,然后对基因 17 到 32 等生成一个“网格”……直到最后的名单。目前,我的代码只显示 16 个基因,即使我在循环内运行 grid.arrange 命令(当我在循环内使用“绘图”函数时它工作但它每页只显示一个图形 c)。

我目前正在进行一些 RNAseq 分析,并且正在寻找两个群体之间的差异表达基因 (DEG)。为了更直观地表示 DEG,我想为每个群体绘制一些感兴趣基因 (GoI) 的归一化计数。

但是,这些 GOI 的列表可能很长(例如,如果我专注于编码膜蛋白的 DEG,我有大约 159 个候选者)。我可以使用 for 循环绘制它们,使用以下代码(来自第一次分析):

# top gene contains all of Gene of Interest
# group_origin contains the factor used to discriminate cell population to compare
# dds_g is a dds object from DESeq2 and contained counts number I want to plot

for (i in unique(1:length(top_gene))){
  gene <- top_gene[i]
  d <- plotCounts(dds_g, gene = gene, intgroup = "group_origin", returnData = TRUE)
  b <- ggplot(d, aes(x = group_origin, y = count)) +
    stat_boxplot(geom = 'errorbar', aes(colour = factor(group_origin)), width = 0.2) +
    geom_boxplot(aes(colour = factor(group_origin)), width = 0.2) +
    stat_summary(fun.y=mean, geom="point", shape=17, size=1.5, aes(color=factor(group_origin), fill=factor(group_origin))) +
    labs (title = paste0(resg_cb_db$symbol[gene],' (',gene,')'), x = element_blank()) +
    theme_bw() +
    scale_color_manual(values = mycolors) +
    theme(text= element_text(size=10),
        axis.text.x = element_text(size = 7.5, angle = 45, hjust = 1),
        axis.text.y = element_text(size = 10),
        legend.position = "none")
    
  plot(b)
}

通过使用这种方法,我能够(单独)为我的“top_gene”向量中包含的所有基因 ID 生成图。

但是,降价会在每页创建一个图,当您拥有大量基因时,这很烦人。我想对它们进行分组,例如,列表中的每个基因 1 到 16 都被绘制在一页上,然后是它的 16 到 32,等等......

我已经尝试过(没有成功)par mfrow。我还使用以下代码(来自另一个分析示例)尝试了 Grid.extra:

for (i in unique(1:length(top_gene))){
  gene <- top_gene[i]
  d <- plotCounts(dds, gene = gene, intgroup = "cell_type", returnData = TRUE)
  b <- ggplot(d, aes(x = cell_type, y = count)) +
    stat_boxplot(geom = 'errorbar', aes(colour = factor(cell_type)), width = 0.2) +
    geom_boxplot(aes(colour = factor(cell_type)), width = 0.2) +
    stat_summary(fun.y=mean, geom="point", shape=17, size=1.5, aes(color=factor(cell_type), fill=factor(cell_type))) +
    labs (title = paste0(resg$symbol[gene],' (',gene,')'), x = element_blank()) +
    theme_bw() +
    scale_color_manual(values = mycolors) +
    theme(text= element_text(size=7),
        axis.text.x = element_text(size = 7, angle = 45, hjust = 1),
        axis.text.y = element_text(size = 6),
        legend.position = "none")
    
  plot_list[[gene]] <- b
}

t <- length(plot_list)
x <- seq(1, t, by = 15)
for (i in x){
  z <- i+1
  if (!is.na(x[z])) {
    
    test_list <- plot_list[c(x[i]:x[z])]
    do.call(grid.arrange,test_list)
    }
    else {
    z <- length(plot_list)  
    test_list <- plot_list[c(x[i]:z)]  
    do.call(grid.arrange,test_list)
  }
}

这给了我一个错误“x[i]:z 中的错误:参数 NA / NaN

这里的想法是,对于每 16 个基因,绘制一个 4x4 图形页面。当然,当它到达最后的“16 组”时,剩下的基因少于 16 个(除非你有可以除以 16 的基因总数)。所以这就是为什么 if 循环可以防止错误(但它不起作用)。

另外,我尝试删除最后一部分,并尝试生成列表中的第一个“9 x 16 基因”,丢弃最后一个。它“有效”是因为我可以清楚地看到前 16 个基因以 4 x 4 的形式绘制,但其余的则一无所获。

为什么使用“plot(b)”绘制inside for 循环是有效的,但在 for 循环内创建的 list() 上也不能使用 grid.arrange?

非常抱歉我的代码,我知道它并不完美(我仍在学习所有这些)但我希望它足够清楚,让您理解我的问题......

谢谢!

!编辑! : 通过添加 (i in length(x)) 解决了第一个错误。觉得自己很蠢 :D。无论如何,它仍然只是“打印”了 16 个地块而不是 159 个......

【问题讨论】:

    标签: r ggplot2 plot


    【解决方案1】:

    我认为您的 for 循环有些混乱。当您说 for (i in x) 每次迭代时,您会得到 x (1,16,31,46,...) 中的值,而不是索引号,因此当您设置 z &lt;- i + 1 时,您会得到值 (2,17,31,. ..)。对于iz 的大多数值,这会使您的x[i]x[z] 值不适用。

    下面的 for 循环应该修复索引并防止您在边缘情况下超出 plot_list 的长度。

    for (i in 1:length(x)){ #replaced this line
      z <- i+1
      if (!is.na(x[z])) {
        # changed this to make sure x[z] doesn't show up on two plots
        test_list <- plot_list[x[i]:(x[z]-1)]
        do.call(grid.arrange,test_list)
        }
        else {
        z <- length(plot_list)  
        test_list <- plot_list[x[i]:z]
        do.call(grid.arrange,test_list)
      }
    }
    

    【讨论】:

    • 嗨!谢谢!我还找到了该解决方案(cfr EDIT 部分):)。这是一个非常愚蠢的错误哈哈。所以这里的代码现在“工作”(没有错误)但他没有做我想要的,因为在我的降价输出中,只生成了与最后一个基因对应的图,或者我想要一个“页面”每 16 个基因或兴趣(从我的列表中包含 159 个基因)。
    猜你喜欢
    • 2011-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    • 2017-07-27
    相关资源
    最近更新 更多