繁体   English   中英

将方面合并到一个循环中

[英]incorporating facet into a loop

我有以下数据集(数据集太大而无法使用 dput 所以现在只使用 head() ):

head(df3_melt_mean)
  Isodecoder Anticodon HEK293_rep1_smallRNAseq HEK293_rep2_smallRNAseq HEK293_rep3_smallRNAseq HEK293_rep1_hydroseq HEK293_rep2_hydroseq
1        Ala       AGC            24442.262951            22452.241238            22390.900942           9971.50351          11438.44236
2        Ala       CGC              743.402305              785.417884              785.435272           3493.42947           3757.57860
3        Ala       GGC                1.058978                2.380054                1.498922             20.98667             13.54833
4        Ala       TGC             3507.333950             3755.725518             3613.901605           7386.74006           7681.38485
5        Arg       GCG                0.000000                0.000000                0.000000              0.00000              0.00000
6        Arg       ACG             2821.116438             2731.112187             2856.945856          10353.23399           9125.32470
  HEK293_rep3_hydroseq Pan_tRNA1 Pan_tRNA2     HEK1      HEK2     HEK3     HEK4   Label variable        value          Condition
1          11213.43885 35665.006 32012.831 22247.59  29870.42 27672.74 35719.69 Ala-AGC ARM_mean 23095.135043 ARM-seq - Mean CPM
2           3557.33569  9930.173  7260.746 14897.03  14413.76 14970.47 12177.89 Ala-CGC ARM_mean   771.418487 ARM-seq - Mean CPM
3             18.69229     0.000     0.000     0.00      0.00     0.00     0.00 Ala-GGC ARM_mean     1.645985 ARM-seq - Mean CPM
4           7567.79822 10365.656  8080.036 35228.45  42094.44 34821.68 29162.91 Ala-TGC ARM_mean  3625.653691 ARM-seq - Mean CPM
5              0.00000     0.000     0.000     0.00      0.00     0.00     0.00 Arg-GCG ARM_mean     0.000000 ARM-seq - Mean CPM
6          10847.97240 11070.480 12520.278 81727.21 108113.40 62025.37 63240.17 Arg-ACG ARM_mean  2803.058160 ARM-seq - Mean CPM

我写了一个循环,它将 plot 分别归一化每个氨基酸的计数,但我正在努力编写一个循环,将所有 22 个图包装成一个大平面包装。 理想情况下,我想拥有所有单独的 22 个地块,但我也想拥有一个 plot,其中包含所有 22 个地块(用于演示目的)。 我的循环如下:

 for (iso in c("Ala", "Arg", "Asn", "Asp", "Cys", "Gln", "Glu", "Gly", "His", "Ile", "Leu", "Lys", "Met", "Phe", "Pro", "SeC", "Ser", "Thr", "Trp", "Tyr", "Val", "iMet")) {
  
  df4 <- subset(df3_melt_mean, Isodecoder == iso) 
  
  normalized_counts_graphs <- ggplot(data = df4, aes(x = Label, y = value, colour = Condition, group = Condition))+
    geom_point(position = position_dodge(width = 0.2), aes(size = 2))+
    xlab("Isodecoder")+
    ylab("Normalized Counts (CPM)")+
    theme_bw()+
    theme(axis.text.x = element_text(size = 12, angle = 45, hjust = 1), plot.margin = margin(0.5,0.5,0.5,2, "cm"), axis.title = element_text(size = 12))+
    ggtitle(iso, "Comaprison of CPM's for different tRNA-seq methodologies")+
    ggsave(filename = paste(iso, "_methodsComparison_normalizedcounts.png", sep = "_"), bg = "white", width = 15, height = 7, dpi = 700)
}

提前致谢!

我建议使用您提供的数据并在您的代码中添加一些关于facet_wrap()的详细信息,遵循伟大的 @AllanCameron 和 @starja 的评论的下一种方法:

数据:

df <- structure(list(Isodecoder = c("Ala", "Ala", "Ala", "Ala", "Arg", 
"Arg"), Anticodon = c("AGC", "CGC", "GGC", "TGC", "GCG", "ACG"
), HEK293_rep1_smallRNAseq = c(24442.26295, 743.402305, 1.058978, 
3507.33395, 0, 2821.116438), HEK293_rep2_smallRNAseq = c(22452.24124, 
785.417884, 2.380054, 3755.725518, 0, 2731.112187), HEK293_rep3_smallRNAseq = c(22390.90094, 
785.435272, 1.498922, 3613.901605, 0, 2856.945856), HEK293_rep1_hydroseq = c(9971.50351, 
3493.42947, 20.98667, 7386.74006, 0, 10353.23399), HEK293_rep2_hydroseq = c(11438.44236, 
3757.5786, 13.54833, 7681.38485, 0, 9125.3247), HEK293_rep3_hydroseq = c(11213.43885, 
3557.33569, 18.69229, 7567.79822, 0, 10847.9724), Pan_tRNA1 = c(35665.006, 
9930.173, 0, 10365.656, 0, 11070.48), Pan_tRNA2 = c(32012.831, 
7260.746, 0, 8080.036, 0, 12520.278), HEK1 = c(22247.59, 14897.03, 
0, 35228.45, 0, 81727.21), HEK2 = c(29870.42, 14413.76, 0, 42094.44, 
0, 108113.4), HEK3 = c(27672.74, 14970.47, 0, 34821.68, 0, 62025.37
), HEK4 = c(35719.69, 12177.89, 0, 29162.91, 0, 63240.17), Label = c("Ala-AGC", 
"Ala-CGC", "Ala-GGC", "Ala-TGC", "Arg-GCG", "Arg-ACG"), variable = c("ARM_mean", 
"ARM_mean", "ARM_mean", "ARM_mean", "ARM_mean", "ARM_mean"), 
    value = c(23095.13504, 771.418487, 1.645985, 3625.653691, 
    0, 2803.05816), Condition = c("ARM-seq-MeanCPM", "ARM-seq-MeanCPM", 
    "ARM-seq-MeanCPM", "ARM-seq-MeanCPM", "ARM-seq-MeanCPM", 
    "ARM-seq-MeanCPM")), class = "data.frame", row.names = c(NA, 
-6L)) 

在单个图的第一种情况下,这里的代码对换行进行了轻微修改:

library(ggplot2)
#Loop for individuals plots
for (iso in unique(df$Isodecoder)) {

df4 <- subset(df, Isodecoder == iso) 

normalized_counts_graphs <- ggplot(data = df4, aes(x = Label, y = value, colour = Condition, group = Condition))+
  geom_point(position = position_dodge(width = 0.2), aes(size = 2))+
  facet_wrap(.~Isodecoder)+
  xlab("Isodecoder")+
  ylab("Normalized Counts (CPM)")+
  theme_bw()+
  theme(axis.text.x = element_text(size = 12, angle = 45, hjust = 1), plot.margin = margin(0.5,0.5,0.5,2, "cm"), axis.title = element_text(size = 12))+
  ggtitle(iso, "Comaprison of CPM's for different tRNA-seq methodologies")+
  ggsave(filename = paste(iso, "_methodsComparison_normalizedcounts.png", sep = "_"), bg = "white", width = 15, height = 7, dpi = 700)
}

这将为您的每个代码创建一个单独的 plot。 结果如下:

在此处输入图像描述

在此处输入图像描述

最后,全局 plot 需要这样的代码:

#General plot
ggplot(data = df, aes(x = Label, y = value, colour = Condition, group = Condition))+
  geom_point(position = position_dodge(width = 0.2), aes(size = 2))+
  facet_wrap(.~Isodecoder,ncol = 2)+
  xlab("Isodecoder")+
  ylab("Normalized Counts (CPM)")+
  theme_bw()+
  theme(axis.text.x = element_text(size = 12, angle = 45, hjust = 1),
        plot.margin = margin(0.5,0.5,0.5,2, "cm"),
        axis.title = element_text(size = 12))+
  ggtitle("Comaprison of CPM's for different tRNA-seq methodologies")+
  ggsave(filename ="methodsComparison_normalizedcounts.png",
         bg = "white", width = 15, height = 7, dpi = 700)

Output:

在此处输入图像描述

您可以在facet_wrap()中使用ncol调整列数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM