【问题标题】:How to loop through the columns and create different plots?如何遍历列并创建不同的图?
【发布时间】:2018-12-15 11:57:45
【问题描述】:

我有一个数据框“A”,其中样本作为行,基因作为具有基因表达值 (RPKM) 的列。

        Gene1   Gene2           Gene3        Gene4        Gene5    Gene6
Sample1 0.02   0.038798682  0.1423662   2.778587067 0.471403939 18.93687655
Sample2 0      0.059227225  0.208765213 0.818810739 0.353671882 1.379027685
Sample3 0      0.052116384  0.230437735 2.535040249 0.504061015 9.773089223
Sample4 0.06   0.199264618  0.261100548 2.516963635 0.63659138  11.01441624
Sample5 0      0.123521916  0.273330986 2.751309388 0.623572499 34.0563519
Sample6 0      0.128767634  0.263491811 2.882878373 0.359322715 13.02402045
Sample7 0      0.080097356  0.234511372 3.568192768 0.386217698 9.068928569
Sample8 0      0.017421323  0.247775683 5.109428797 0.068760572 15.7490551
Sample9 0      2.10281137   0.401582013 8.202902242 0.140596724 60.25989178

为了制作显示 Gene1 与 Gene5 和 Gene6 之间相关性的散点图,我使用了以下代码:

library(tidyr)
library(ggplot2)
pdf("Gene1.pdf")
A %>% 
  gather(key = variable, value = values, Gene5:Gene6) %>% 
  ggplot(aes(Gene1, values)) + 
  geom_point() + 
  facet_grid(. ~ variable, scales = "free_x") + 
  geom_smooth(method = "lm", se = FALSE) + 
  scale_y_continuous(trans = "log2", labels = NULL, breaks = NULL) + 
  scale_x_continuous(trans = "log2", labels = NULL, breaks = NULL)
dev.off()

我也想为

Gene2 vs Gene5 and Gene6
Gene3 vs Gene5 and Gene6
Gene4 vs Gene5 and Gene6

当然,我可以将代码中的Gene1 替换为其他基因来绘制。但不是手动替换,我想创建一个循环,以便从 Gene1 到 Gene4 针对 Gene5 和 Gene6 绘制不同的图,每个图都保存在具有各自基因名称的 pdf 中。

根据要求,这是关于dput(A)的更新:

structure(list(Gene1 = c(0.02, 0, 0, 0.06, 0, 0, 0, 0, 0), Gene2 = c(0.038798682, 
0.059227225, 0.052116384, 0.199264618, 0.123521916, 0.128767634, 
0.080097356, 0.017421323, 2.10281137), Gene3 = c(0.1423662, 0.208765213, 
0.230437735, 0.261100548, 0.273330986, 0.263491811, 0.234511372, 
0.247775683, 0.401582013), Gene4 = c(2.778587067, 0.818810739, 
2.535040249, 2.516963635, 2.751309388, 2.882878373, 3.568192768, 
5.109428797, 8.202902242), Gene5 = c(0.471403939, 0.353671882, 
0.504061015, 0.63659138, 0.623572499, 0.359322715, 0.386217698, 
0.068760572, 0.140596724), Gene6 = c(18.93687655, 1.379027685, 
9.773089223, 11.01441624, 34.0563519, 13.02402045, 9.068928569, 
15.7490551, 60.25989178)), class = "data.frame", row.names = c("Sample1", 
"Sample2", "Sample3", "Sample4", "Sample5", "Sample6", "Sample7", 
"Sample8", "Sample9"))

【问题讨论】:

    标签: r for-loop plot graphics scatter-plot


    【解决方案1】:

    循环对你有用吗?我没有测试过(因为你没有数据作为 dput()),所以可能需要一些清理。

    cols <- colnames(A)
    cols <- cols[!cols %in% c("Gene5", "Gene6")]
    for(i in cols){
        name <- paste(i, ".pdf", sep = "")
        id <- which(colnames(A) == i)
        # add a new column - this is the one accepting the "rotating" gene input
        A$Gene <- A[,id]
    
    p <- A %>% 
            select(Gene, Gene5, Gene6) %>%
            gather(variable, values, Gene5:Gene6) %>% 
            ggplot(aes(Gene, values)) + 
            geom_point() + 
            facet_grid(. ~ variable, scales = "free_x") + 
            geom_smooth(method = "lm", se = FALSE) + 
            scale_y_continuous(trans = "log2", labels = NULL, breaks = NULL) + 
            scale_x_continuous(i, trans = "log2", labels = NULL, breaks = NULL)
    
        pdf(name)
        print(p)
    
         dev.off()
            }
    

    【讨论】:

    • 不,这不起作用。在 for 循环中,我不想提及 Gene1 到 Gene4 的名称。
    • 您可以将数据发布为 dput(A) 吗?你不想提名字是什么意思?什么更适合你?
    • 我的意思是在这个数据中我需要 Gene1 到 Gene4 来对抗 Gene5 和 Gene6,正如我在问题中提到的那样。但在我的原始数据中,我需要针对 Gene5 和 Gene6 绘制 50 多个基因
    • 我对答案进行了编辑 - 代码中有错字。再试一次?如果这不起作用 - 请在 R 中运行 dput(A) 并在此处发布输出,以便我可以轻松阅读并测试代码:-)
    • 这50个基因是否都被命名为GeneX?
    【解决方案2】:

    如果你需要一个显式的 for 循环:

    for (gene in paste0("Gene",1:4)){
     a= A %>% 
        gather(key = variable, value = values, Gene5:Gene6) %>% 
        ggplot(aes(get(gene), values)) + 
        geom_point() + 
        facet_grid(. ~ variable, scales = "free_x") + 
        geom_smooth(method = "lm", se = FALSE) + 
        scale_y_continuous(trans = "log2", labels = NULL, breaks = NULL) + 
        scale_x_continuous(trans = "log2", labels = NULL, breaks = NULL)
    
        print(a)
    
    }
    

    【讨论】:

    • 如果列 Gene1 到 Gene4 被命名为不同的名称,如 TGF、DGTH、HJUY、DSER。
    • 把所有你想要的名字放在一个向量中。例如s= c('TGF', 'DGTH', 'HJUY', 'DSER'),现在做for(gene in s){….}
    • 非常感谢!!但你没有展示如何将绘图保存为 pdfs
    • 其他一切都保持不变。我知道你能做到。只需在循环中包含 pdf 部分,即 pdf(gene)
    猜你喜欢
    • 1970-01-01
    • 2021-04-14
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 2023-01-19
    • 2020-03-05
    • 1970-01-01
    相关资源
    最近更新 更多