0

I would like to loop to ggplot2 to create multiple plots. I either want to save the plots individuals as png files or put them in a file of some sort.

Here is my data:

graphs<-structure(list(unqid = c(1, 1, 1, 2, 2, 2), Time = c("0", 
"2 Years", "6 years", "0", "2 Years", "6 years"), GlcCer..d18.1.24.0. = c(392.053548533786, 
173.290788496984, 3705.77443834855, 465.331103793138, 967.749312174098, 
747.630597259739), GlcCer..d18.1.22.0. = c(482.088375817056, 
254.854448566389, 4988.80485026693, 571.063743322225, 939.391472143236, 
768.77991521974), SM.d18.1.20.0. = c(28672.2895018695, 13196.8589515893, 
56167.0664748886, 27332.707983709, 24921.9953042692, 36520.1408526379
), SM.d18.1.24.0. = c(26083.782634532, 11475.1218476637, 88585.8234565341, 
19668.2512106659, 22312.971305909, 23989.9271962257), Cer.d18.0.24.0. = c(203.041537286274, 
87.8984583301296, 345.026948321681, 113.63798428895, 91.6431430559489, 
79.6682688542247), GlcCer..d18.1.16.0. = c(119.586951218119, 
84.2784695405782, 430.631169371862, 132.359124161101, 143.596202137092, 
164.891337507055), Cer.d18.1.17.0. = c(5.65513705275648, 10.2926963643725, 
25.7531945158303, 6.8836736534046, 14.2351524354933, 10.4387851992517
), Cer.d18.1.24.0. = c(1671.33460737205, 1461.26838249666, 3365.11466246641, 
1539.70084125062, 2449.53479116483, 1960.50305100894), SM.d18.1.16.0. = c(41211.4398529659, 
33548.0124917944, 105828.065018909, 44295.6016009965, 52582.1187541458, 
43823.0428789309), SM.d18.0.20.0. = c(1253.50334022353, 846.575047211137, 
3031.85531410421, 1399.12291673709, 1074.45154117799, 1196.4626072147
), PC.36.1. = c(29684.9312200963, 25700.9298747554, 65837.4174903136, 
98427.2699693964, 79510.6660875297, 47843.3560210278), SM.d18.1.22.0. = c(47795.7949831908, 
30990.1744028016, 127729.716872227, 47799.0946882277, 46721.7978249562, 
63520.9775652175), error_GlcCer..d18.1.24.0. = c(936.074372976329, 
907.663397420009, 817.919045409943, 936.074372976329, 907.663397420009, 
817.919045409943), error_GlcCer..d18.1.22.0. = c(1429.70389874767, 
1363.38065481112, 1020.70230576293, 1429.70389874767, 1363.38065481112, 
1020.70230576293), error_SM.d18.1.20.0. = c(29777.7387648987, 
31045.3029950327, 25052.1146877277, 29777.7387648987, 31045.3029950327, 
25052.1146877277), error_SM.d18.1.24.0. = c(54561.0927529135, 
58196.3248533819, 36632.993324447, 54561.0927529135, 58196.3248533819, 
36632.993324447), error_Cer.d18.0.24.0. = c(141.78335403169, 
136.519842007374, 117.870286266133, 141.78335403169, 136.519842007374, 
117.870286266133), error_GlcCer..d18.1.16.0. = c(81.086154560981, 
78.5631969448643, 82.7830831042636, 81.086154560981, 78.5631969448643, 
82.7830831042636), error_Cer.d18.1.17.0. = c(11.4223412739451, 
12.4595367519436, 8.37753359127409, 11.4223412739451, 12.4595367519436, 
8.37753359127409), error_Cer.d18.1.24.0. = c(1299.71767331814, 
1351.19164564217, 1172.54994122983, 1299.71767331814, 1351.19164564217, 
1172.54994122983), error_SM.d18.1.16.0. = c(24837.9873076409, 
22995.6727700877, 20459.682202334, 24837.9873076409, 22995.6727700877, 
20459.682202334), error_SM.d18.0.20.0. = c(2510.11807880196, 
2638.13669739695, 1574.50698807042, 2510.11807880196, 2638.13669739695, 
1574.50698807042), error_PC.36.1. = c(71563.8361377675, 68243.1628999811, 
57429.4826536701, 71563.8361377675, 68243.1628999811, 57429.4826536701
), error_SM.d18.1.22.0. = c(86304.1639109951, 86447.2218496344, 
64606.0303862625, 86304.1639109951, 86447.2218496344, 64606.0303862625
)), class = "data.frame", row.names = c(NA, -6L))

Here is the object I loop through:

min.sig1<-c("GlcCer..d18.1.24.0.", "GlcCer..d18.1.22.0.", "SM.d18.1.20.0.", "SM.d18.1.24.0.",
           "Cer.d18.0.24.0.", "GlcCer..d18.1.16.0.", "Cer.d18.1.17.0.", "Cer.d18.1.24.0.",
           "SM.d18.1.16.0.",    "SM.d18.0.20.0.", "PC.36.1.",   "SM.d18.1.22.0.")

Here is the code I have so far. But it doesn't seem to produce any plot that I can see (although it also doesn't produce any errors and the code appears to run).

for (variable in min.sig1) {
  ggplot(graphs, aes(x = Time, y = variable)) +
    geom_line(color = "blue") +  # Line plot
    geom_ribbon(aes(
      ymin = paste0(variable, "- error_", variable), 
      ymax = paste0(variable, "+ error_", variable)), 
                fill = "blue", alpha = 0.3) +  # Shaded error bars
  labs(x = "Time Point", y = variable, 
       title = "Line graph with shaded error bars") +  # Labels
  theme_minimal()  # Optional: Apply a minimal theme
}
2
  • (1) ggplotr2 plots are rendered when they are printed explicitly (print(...)) or implicitly (ggplot()+... interactively on the console), so you need to either explicitly print them each in the loop or capture them in a list for printing later. (2) This will also fail in a new way: ggplot renders lazily, which means that variable will be realized by ggplot only when you print afterward, at which point it will be the last value within min.sig1, so this should likely be done in lapply. Commented Feb 17, 2024 at 0:19
  • Replace for (variable in min.sig1) {...} with myplots <- lapply(min.sig1, function(variable){...}), and then the list will contain all correct plots. Commented Feb 17, 2024 at 0:19

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.