【问题标题】:ggplot for-loop failing to subsetggplot for-loop未能子集
【发布时间】:2020-06-22 15:08:30
【问题描述】:

我正在尝试编写一个 ggplot for 循环,但没有成功。本质上,我正在尝试根据氨基酸制作散点图(因此基本上 22 个不同的散点图仅包含所述氨基酸的值)。相反,我在每个输出图中都绘制了每个值。

数据文件如下所示:

dput(head(df_melt_differentials))

structure(list(codon = c("AAA", "AAC", "AAG", "AAT", "ACA", "ACC"
), Fed_differential_cutoff0.5 = c(0.405320284943889, 0.538603465353382, 
0.594679715056111, 0.461396534646618, 0.279723500180007, 0.350047954876902
), Fed_differential_cutoff0 = c(0.400929382467845, 0.541230665098641, 
0.599070617532155, 0.458769334901359, 0.281177150483858, 0.351472083384939
), Fed_differential_cutoff1 = c(0.389691692491739, 0.572371186663778, 
0.610308307508261, 0.427628813336222, 0.258694141571916, 0.371346938275356
), Fed_differential_cutoff2 = c(0.376102000883263, 0.543866386823925, 
0.623897999116737, 0.456133613176075, 0.240118371752021, 0.371624132164088
), Starved_differential_cutoff0.5 = c(0.35341548435504, 0.612764761460883, 
0.64658451564496, 0.387235238539117, 0.241749339598093, 0.401216490580919
), Starved_differential_cutoff0 = c(0.351704818898789, 0.613092767267543, 
0.648295181101211, 0.386907232732457, 0.242028282002779, 0.398227680007641
), Starved_differential_cutoff1 = c(0.351258676092076, 0.616216524001233, 
0.648741323907924, 0.383783475998767, 0.236979413320061, 0.417121137360074
), Starved_differential_cutoff2 = c(0.330195165073707, 0.631859350667716, 
0.669804834926293, 0.368140649332284, 0.226783649173637, 0.440433256347991
), AA = c("K", "N", "K", "N", "T", "T"), full_amino = c("Lysine", 
"Asparagine", "Lysine", "Asparagine", "Threonine", "Threonine"
), aminoacid = c("Lys", "Asn", "Lys", "Asn", "Thr", "Thr"), wobble = c("AT_wobble", 
"GC_wobble", "GC_wobble", "AT_wobble", "AT_wobble", "GC_wobble"
), wobble_single = c("A_wobble", "C_wobble", "G_wobble", "T_wobble", 
"A_wobble", "C_wobble")), row.names = c(NA, 6L), class = "data.frame")

我的循环是:

for (aminoacid in df_melt_differentials$aminoacid) {
  
  cutoff0_gingold_loop <- ggplot(df_melt_differentials, aes(x=Fed_differential_cutoff0, y= Starved_differential_cutoff0)) +
    geom_point(aes(color = wobble)) +
    theme_bw(base_size = 16)+
    labs(title = paste(aminoacid, "RSCU of Differential Genes (Log2FC cutoff = 0)")) +
    geom_abline(slope = 1, intercept = 0, linetype= "dashed")
  
  cutoff0_gingold_loop +
    geom_label_repel(aes(label = codon),
                     box.padding   = 0.35, 
                     point.padding = 0.5,
                     segment.color = 'grey50') +
    theme_classic()
  
    ggsave(filename = paste(aminoacid, "RSCU_FvS_differential_cutoff0_gingold.png", sep = "_"), bg = "white", width = 7, height = 7, dpi = 600)
}

我知道这可能是一个愚蠢的错误,但我似乎无法弄清楚我哪里出错了。

我还有一个次要问题,但如果没有回答我也不会太在意;最后,根据我拥有的 4 个不同的截止值(0、0.5、1 和 2),我通常有 4 个不同的散点图。有没有办法将其合并到循环中?理想情况下,我想要 Fed_diff

提前致谢!

【问题讨论】:

  • 考虑将dput(head(df_melt_differentials)) 添加到您的reprex 共享机器可读的精确数据,这样人们就不必解析文本表来重现您的情节
  • 至于你的第二个问题 - 是的,你可以做一个嵌套循环,或者你可以将你的数据转换为长格式(参见this FAQ)并使用方面 - 这可能会更好,给您将所有四个截止点作为子图。

标签: r for-loop ggplot2


【解决方案1】:

您在任何地方都没有subset。我会改写为:

for (this_aminoacid in unique(df_melt_differentials$aminoacid)) {
  
  cutoff0_gingold_loop <- ggplot(
    data = subset(df_melt_differentials, aminoacid == this_aminoacid),
    aes(x=Fed_differential_cutoff0, y= Starved_differential_cutoff0)
  ) +
    geom_point(aes(color = wobble)) +
    theme_bw(base_size = 16)+
    labs(title = paste(this_aminoacid , "RSCU of Differential Genes (Log2FC cutoff = 0)")) +
    geom_abline(slope = 1, intercept = 0, linetype= "dashed")
  
  cutoff0_gingold_loop +
    geom_label_repel(aes(label = codon),
                     box.padding   = 0.35, 
                     point.padding = 0.5,
                     segment.color = 'grey50') +
    theme_classic()
  
    ggsave(filename = paste(this_aminoacid, "RSCU_FvS_differential_cutoff0_gingold.png", sep = "_"), bg = "white", width = 7, height = 7, dpi = 600)
}

我有

  • 添加了 subset 以告诉 R 每次使用哪些数据
  • 为了清楚起见,将循环变量的名称更改为this_aminoacid
  • 循环遍历 unique(df_melt_differentials$aminoacid),因此每个值仅使用一次,而不是在您的数据中显示多少次

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 2013-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多