Danby
Danby

Reputation: 108

incorporating facet into a loop

I have the following dataset (dataset too big to use dput so just using head() for now):

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

I wrote a loop that will plot the normalized counts for each amino acid seperately, but I'm struggling to write a loop that will wrap all 22 plots into one big facet wrap. Ideally, I would like to have all my seperate 22 plots, but I would also like to have one plot with all 22 plots within it (for presentation purposes). My loop is below:

 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)
}

Thanks in advance!

Upvotes: 0

Views: 233

Answers (1)

Duck
Duck

Reputation: 39603

I would suggest next approach following the comments of great @AllanCameron and @starja using the data you provided and adding some details to your code about facet_wrap():

The data:

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)) 

In the first case of individual plots, here the code with the slight modifications for wraps:

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)
}

This will create an individual plot for each of your codes. Here the results:

enter image description here

enter image description here

Finally, the global plot would require a code like this:

#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:

enter image description here

You can adjust the number of columns with ncol inside facet_wrap().

Upvotes: 1

Related Questions