Error while trying to save ggplot in loop with faceted variable

1,673 views
Skip to first unread message

Chandra

unread,
Feb 23, 2017, 4:08:26 AM2/23/17
to ggplot2
I'm running into an error while trying to save multiple plots in a loop. I get the following error: Error: Faceting variables must have at least one value.
If I run the ggplot line outside of the for loop, I don't get an error.  Just running the for loop by itself seems fine but when I run the print section, I get the error.

Here is my code:

library(ggplot2)
SampleLists <- unique(Mydf$Site)
plot_list = list()
for (i in SampleLists) {
  p = ggplot(subset(Mydf, Site == SampleLists[i]), aes(x = Time, y = Protein)) + stat_summary(fun.data = "mean_se", size = 0.75) + geom_line()+ facet_wrap(~Sample) + ggtitle(as.character(SampleLists[i]))
  plot_list[[i]] = p
 
}

pdf("plots.pdf")
for (i in SampleLists) {
  print(plot_list[[i]])
}
dev.off()

Thanks in advance,
Chandra



dput(Mydf)
structure(list(Sample = structure(c(19L, 19L, 19L, 1L, 1L, 1L,
20L, 20L, 20L, 2L, 2L, 2L, 21L, 21L, 21L, 3L, 3L, 3L, 22L, 22L,
22L, 4L, 4L, 4L, 23L, 23L, 23L, 24L, 24L, 24L, 6L, 6L, 6L, 5L,
5L, 5L, 25L, 25L, 25L, 7L, 7L, 7L, 26L, 26L, 26L, 8L, 8L, 8L,
27L, 27L, 27L, 9L, 9L, 9L, 10L, 10L, 10L, 28L, 28L, 28L, 29L,
29L, 29L, 11L, 11L, 11L, 12L, 12L, 12L, 31L, 31L, 31L, 13L, 13L,
13L, 32L, 32L, 32L, 33L, 33L, 33L, 14L, 14L, 14L, 15L, 15L, 15L,
16L, 16L, 16L, 17L, 17L, 17L, 18L, 18L, 18L, 19L, 19L, 19L, 1L,
1L, 1L, 20L, 20L, 20L, 2L, 2L, 2L, 21L, 21L, 21L, 3L, 3L, 3L,
22L, 22L, 22L, 4L, 4L, 4L, 23L, 23L, 23L, 24L, 24L, 24L, 6L,
6L, 6L, 5L, 5L, 5L, 25L, 25L, 25L, 7L, 7L, 7L, 26L, 26L, 26L,
8L, 8L, 8L, 27L, 27L, 27L, 9L, 9L, 9L, 10L, 10L, 10L, 28L, 28L,
28L, 29L, 29L, 29L, 11L, 11L, 11L, 30L, 30L, 30L, 12L, 12L, 12L,
31L, 31L, 31L, 13L, 13L, 13L, 32L, 32L, 32L, 33L, 33L, 33L, 14L,
14L, 14L, 15L, 15L, 15L, 16L, 16L, 16L, 17L, 17L, 17L, 19L, 19L,
19L, 1L, 1L, 1L, 20L, 20L, 20L, 2L, 2L, 2L, 21L, 21L, 21L, 3L,
3L, 3L, 22L, 22L, 22L, 4L, 4L, 4L, 23L, 23L, 23L, 24L, 24L, 24L,
6L, 6L, 6L, 5L, 5L, 5L, 25L, 25L, 25L, 7L, 7L, 7L, 26L, 26L,
26L, 8L, 8L, 8L, 27L, 27L, 27L, 9L, 9L, 9L, 10L, 10L, 10L, 28L,
28L, 28L, 29L, 29L, 29L, 11L, 11L, 11L, 30L, 30L, 30L, 12L, 12L,
12L, 31L, 31L, 31L, 13L, 13L, 13L, 32L, 32L, 32L, 33L, 33L, 33L,
14L, 14L, 14L, 15L, 15L, 15L, 16L, 16L, 16L, 17L, 17L, 17L), .Label = c("W0076",
"W0077", "W0078", "W0079", "W0080", "W0081", "W0082", "W0083",
"W0084", "W0085", "W0113", "W0114", "W0115", "W0116", "W0117",
"W0118", "W0119", "W0120", "W0419", "W0420", "W0421", "W0422",
"W0423", "W0424", "W0425", "W0426", "W0427", "W0475", "W0476",
"W0477", "W0478", "W0479", "W0480"), class = "factor"), Time = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 24L,
24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L,
24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L,
24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L,
24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L,
24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L,
24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L,
24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L,
24L, 24L, 24L, 24L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L), Protein = c(33025.08787, 32621.05272, 33109.26186,
92823.91351, 99658.65946, 88215.63784, 126607.5691, 126062.4228,
120688.8374, 88465.42775, 85641.56069, 85586.19075, 31044.23049,
29184.06171, 28558.21053, 78722.31319, 76932.82967, 76880.1978,
123940.6471, 126999.4874, 121203.7899, 47333.75932, 46879.16271,
48145.53898, 29838.50758, 29384.95644, 28786.26894, 72208.38614,
71402.23267, 71449.65347, 66585.06926, 65631.31602, 64594.62771,
118007.5143, 116091.7143, 116160.1357, 254667.6327, 255058.6122,
253494.6939, 85345.35374, 82999.47619, 84758.88435, 51222.43983,
51540.41494, 47406.73859, 33860.13002, 34427.91013, 35014.00574,
64349.17269, 62925.78715, 61233.11245, 60682.78879, 60847.94397,
59857.01293, 30588.24766, 30856.81776, 30163.01168, 80698.48864,
81188.32386, 82113.56818, 87193.33129, 84019.92025, 83197.18405,
83732.26857, 82309.10286, 82528.05143, 68003.51464, 61791.19247,
60829.28452, 1574720.333, 1577115.083, 1532413.083, 97710.7713,
94832.77578, 90107.70852, 90790.76667, 90790.76667, 93345.16667,
168899.9043, 160736.9304, 158654.5391, 188730.6118, 193914.5412,
195717.6471, 200694.6147, 191115.6147, 191994.422, 53235.80567,
51257.95547, 51529.4251, 56887.58271, 55555.16541, 53934.65789,
55698.1877, 54923.1877, 54334.1877, 32969.95662, 33198.52278,
33489.42516, 50966.30868, 50196.2926, 50843.10611, 35986.85912,
36584.16397, 36739.02079, 41650.47545, 41798.98708, 41353.4522,
44110.35714, 44247.2, 43864.04, 107545.2778, 108942.2153, 109806.9861,
56843.98201, 56878.43885, 56981.80935, 38194.61391, 38470.26859,
38516.21103, 28470.80364, 28296.64, 27983.14545, 72706.66667,
73561.13146, 73965.87793, 135462.9492, 143174.8559, 133839.3898,
93960.11243, 94583.59763, 94016.7929, 45243.45697, 44589.69733,
45470.85163, 270012.807, 273709.9649, 276230.7544, 55948.10432,
56878.43885, 55948.10432, 61637.91051, 61600.63813, 61675.18288,
155484.3469, 155875.3265, 155973.0714, 52823.27148, 53744.9622,
60953.90034, 50893.1877, 50645.1877, 50459.1877, 54846.09689,
54713.51557, 55840.45675, 46137.38922, 46080.02994, 46051.3503,
61831.132, 61907.764, 61907.764, 45646.33626, 45366.24854, 45702.3538,
70558.90265, 70092.66814, 69838.35841, 25046.01127, 25169.41224,
24984.31079, 36407.18476, 36584.16397, 36340.81755, 39964.90617,
39491.86914, 39822.99506, 32393.51308, 32297.14487, 32894.62777,
49987.41613, 50141.91613, 49894.71613, 42265.14402, 42941.9212,
42941.9212, 64637.53036, 64249.7166, 64210.93522, 337041.3404,
341728.9362, 338671.8085, 67624.84977, 66455.58216, 66905.30047,
71242.26415, 71603.73585, 71242.26415, 983901.0667, 967297.4667,
983901.0667, 91310.81988, 95237.61491, 95297.1118, 64297.54545,
65386.06818, 64994.2, 134532.1509, 135435.8302, 135345.4623,
146531.47, 148830.43, 148159.9, 62908.0249, 69466.26141, 63504.22822,
85282.50595, 84199.16667, 86479.88095, 39143.80654, 39326.51226,
39874.62943, 63959.22174, 63709.33478, 64500.64348, 50018.15615,
50622.81063, 51322.93688, 113316.944, 112703.888, 114619.688,
29633.09205, 29813.44979, 30154.12552, 46884.37582, 46133.0817,
47072.19935, 49189.0034, 50427.10544, 51306.80952, 77574.77222,
80022.73889, 78160.15556, 61443.09524, 62106.57576, 62562.71861,
48613.13201, 48107.31023, 47569.87459, 115727.3403, 105150.5278,
105416.6111, 22594.25, 22958.00949, 22639.71994, 21920.10292,
22273.24578, 22420.38863, 56378.24219, 57313.69141, 55854.39063,
61409.656, 60911.548, 61869.448, 44043.16975, 43924.91049, 43777.08642,
64440.04525, 63529.82353, 65480.29864, 44031.73476, 44031.73476,
44820.25, 91342.69375, 97569.04375, 95174.29375, 54479.3, 54331.93077,
55142.46154, 47511.70684, 47948.5342, 51630.36482, 40513.02192,
39988.14521, 39751.95068, 39758.43514, 42010.79459, 41260.00811
), Site = structure(c(3L, 3L, 3L, 1L, 1L, 1L, 13L, 13L, 13L,
4L, 4L, 4L, 14L, 14L, 14L, 15L, 15L, 15L, 11L, 11L, 11L, 9L,
9L, 9L, 8L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L, 12L, 12L, 12L,
12L, 12L, 12L, 13L, 13L, 13L, 4L, 4L, 4L, 11L, 11L, 11L, 5L,
5L, 5L, 3L, 3L, 3L, 8L, 8L, 8L, 7L, 7L, 7L, 4L, 4L, 4L, 13L,
13L, 13L, 6L, 6L, 6L, 13L, 13L, 13L, 11L, 11L, 11L, 3L, 3L, 3L,
8L, 8L, 8L, 12L, 12L, 12L, 8L, 8L, 8L, 3L, 3L, 3L, 2L, 2L, 2L,
11L, 11L, 11L, 3L, 3L, 3L, 1L, 1L, 1L, 13L, 13L, 13L, 4L, 4L,
4L, 14L, 14L, 14L, 15L, 15L, 15L, 11L, 11L, 11L, 9L, 9L, 9L,
8L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L, 12L, 12L, 12L, 12L, 12L,
12L, 13L, 13L, 13L, 4L, 4L, 4L, 11L, 11L, 11L, 5L, 5L, 5L, 3L,
3L, 3L, 8L, 8L, 8L, 7L, 7L, 7L, 4L, 4L, 4L, 13L, 13L, 13L, 12L,
12L, 12L, 6L, 6L, 6L, 13L, 13L, 13L, 11L, 11L, 11L, 3L, 3L, 3L,
8L, 8L, 8L, 12L, 12L, 12L, 8L, 8L, 8L, 3L, 3L, 3L, 2L, 2L, 2L,
3L, 3L, 3L, 1L, 1L, 1L, 13L, 13L, 13L, 4L, 4L, 4L, 14L, 14L,
14L, 15L, 15L, 15L, 11L, 11L, 11L, 9L, 9L, 9L, 8L, 8L, 8L, 9L,
9L, 9L, 10L, 10L, 10L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L,
13L, 4L, 4L, 4L, 11L, 11L, 11L, 5L, 5L, 5L, 3L, 3L, 3L, 8L, 8L,
8L, 7L, 7L, 7L, 4L, 4L, 4L, 13L, 13L, 13L, 12L, 12L, 12L, 6L,
6L, 6L, 13L, 13L, 13L, 11L, 11L, 11L, 3L, 3L, 3L, 8L, 8L, 8L,
12L, 12L, 12L, 8L, 8L, 8L, 3L, 3L, 3L, 2L, 2L, 2L), .Label = c("ARC",
"Bol", "CDC", "DEB", "Fra", "Fra_cor", "Fra2", "HIG", "LDS",
"Leb", "MUS", "NAR", "SAN", "Tur", "Uru"), class = "factor")), .Names = c("Sample",
"Time", "Protein", "Site"), class = "data.frame", row.names = c(NA,
-288L))

Pedro Aphalo

unread,
Feb 23, 2017, 10:43:08 AM2/23/17
to ggplot2
You are using a factor for indexing the list which is not usually a good idea. 
To start with replace `SampleLists <- unique(Mydf$Site)` with `SampleLists <- levels(Mydf$Site)`.
This does not look to me necessarily as a ggplot2 problem, so do first debug the loops, and provide a simple and reproducible example of the problem using simple data if it persists and it is related to ggplot2.
Be aware that error messages like you describe are only triggered on printing even if they have to do with problems in the plotted data. Do all individual plots trigger the error or only some? 
Reply all
Reply to author
Forward
0 new messages