facet只是数据集的一个子集

时间:2013-06-01 11:14:06

标签: r ggplot2 facet-wrap

我有以下数据集:

mdf <- structure(list(milieu = structure(c(3L, 9L, 8L, 6L, 10L, 2L, 
1L, 7L, 5L, 4L, 3L, 9L, 8L, 6L, 10L, 2L, 1L, 7L, 5L, 4L, 3L, 
9L, 8L, 6L, 10L, 2L, 1L, 7L, 5L, 4L, 3L, 9L, 8L, 6L, 10L, 2L, 
1L, 7L, 5L, 4L, 3L, 9L, 8L, 6L, 10L, 2L, 1L, 7L, 5L, 4L, 3L, 
9L, 8L, 6L, 10L, 2L, 1L, 7L, 5L, 4L, 3L, 9L, 8L, 6L, 10L, 2L, 
1L, 7L, 5L, 4L, 3L, 9L, 8L, 6L, 10L, 2L, 1L, 7L, 5L, 4L, 3L, 
9L, 8L, 6L, 10L, 2L, 1L, 7L, 5L, 4L, 3L, 9L, 8L, 6L, 10L, 2L, 
1L, 7L, 5L, 4L, 3L, 9L, 8L, 6L, 10L, 2L, 1L, 7L, 5L, 4L, 3L, 
9L, 8L, 6L, 10L, 2L, 1L, 7L, 5L, 4L, 3L, 9L, 8L, 6L, 10L, 2L, 
1L, 7L, 5L, 4L, 3L, 9L, 8L, 6L, 10L, 2L, 1L, 7L, 5L, 4L), class = "factor", .Label = c("BUM", 
"DDR", "ETB", "EXP", "HED", "KON", "MAT", "PER", "PMA", "TRA"
)), mycols = structure(c(3L, 1L, 5L, 6L, 2L, 4L, 10L, 8L, 7L, 
9L, 3L, 1L, 5L, 6L, 2L, 4L, 10L, 8L, 7L, 9L, 3L, 1L, 5L, 6L, 
2L, 4L, 10L, 8L, 7L, 9L, 3L, 1L, 5L, 6L, 2L, 4L, 10L, 8L, 7L, 
9L, 3L, 1L, 5L, 6L, 2L, 4L, 10L, 8L, 7L, 9L, 3L, 1L, 5L, 6L, 
2L, 4L, 10L, 8L, 7L, 9L, 3L, 1L, 5L, 6L, 2L, 4L, 10L, 8L, 7L, 
9L, 3L, 1L, 5L, 6L, 2L, 4L, 10L, 8L, 7L, 9L, 3L, 1L, 5L, 6L, 
2L, 4L, 10L, 8L, 7L, 9L, 3L, 1L, 5L, 6L, 2L, 4L, 10L, 8L, 7L, 
9L, 3L, 1L, 5L, 6L, 2L, 4L, 10L, 8L, 7L, 9L, 3L, 1L, 5L, 6L, 
2L, 4L, 10L, 8L, 7L, 9L, 3L, 1L, 5L, 6L, 2L, 4L, 10L, 8L, 7L, 
9L, 3L, 1L, 5L, 6L, 2L, 4L, 10L, 8L, 7L, 9L), class = "factor", .Label = c("#00CCFF", 
"#00FD03", "#3168FF", "#97CB00", "#98CBF8", "#CCFCCC", "#FB02FE", 
"#FE9900", "#FF0200", "#FFFD00")), variable = structure(c(8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 14L, 14L, 14L, 14L, 14L, 
14L, 14L, 14L, 14L, 14L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 13L, 13L, 13L, 13L, 
13L, 13L, 13L, 13L, 13L, 13L), .Label = c("Ausländer", "Keine Umweltbelastung", 
"ÖPNV", "Keine Kriminalität", "Einrichtungen für Kinder", 
"Kein Mangel an Grünflaechen", "Gaststätte", "Geschäft", "Bank", 
"Park", "Hausarzt", "Sportstätte", "Einrichtungen für Jugendliche", 
"Einrichtung für Ältere"), class = "factor", scores = structure(c(0.0718023287061849, 
0.0693420423225302, 0.0753384763664876, 0.0827043835101492, 0.109631516692048, 
0.0765927537218141, 0.0870322381232645, 0.0515014684350035, 0.0683398169561522, 
0.0554744519820495, 0.0363337127130046, 0.0463575341160886, 0.0671060291182815, 
0.102443247236942), .Dim = 14L, .Dimnames = list(c("Geschäft", 
"Gaststätte", "Bank", "Hausarzt", "Einrichtung für Ältere", 
"Park", "Sportstätte", "ÖPNV", "Kein Mangel an Grünflaechen", 
"Keine Kriminalität", "Ausländer", "Keine Umweltbelastung", 
"Einrichtungen für Kinder", "Einrichtungen für Jugendliche"
)))), value = c(0.0468431771894094, 0.0916666666666667, 0.0654761904761905, 
0.0905432595573441, 0.0761904761904762, 0.0672097759674134, 0.0869565217391304, 
0.0650887573964497, 0.0762250453720508, 0.0518234165067179, 0.0855397148676171, 
0.0604166666666667, 0.0555555555555556, 0.0764587525150905, 0.0895238095238095, 
0.0712830957230143, 0.075098814229249, 0.0631163708086785, 0.0780399274047187, 
0.0383877159309021, 0.065173116089613, 0.0854166666666667, 0.0972222222222222, 
0.0824949698189135, 0.060952380952381, 0.0529531568228106, 0.0731225296442688, 
0.0828402366863905, 0.0725952813067151, 0.0806142034548944, 0.0712830957230143, 
0.0833333333333333, 0.0912698412698413, 0.0704225352112676, 0.0628571428571429, 
0.0672097759674134, 0.106719367588933, 0.0710059171597633, 0.108892921960073, 
0.0940499040307102, 0.10183299389002, 0.104166666666667, 0.107142857142857, 
0.100603621730382, 0.12, 0.116089613034623, 0.112648221343874, 
0.112426035502959, 0.121597096188748, 0.0998080614203455, 0.0855397148676171, 
0.0666666666666667, 0.0912698412698413, 0.0804828973843058, 0.0704761904761905, 
0.0672097759674134, 0.0731225296442688, 0.0670611439842209, 0.0834845735027223, 
0.0806142034548944, 0.0855397148676171, 0.0791666666666667, 0.0952380952380952, 
0.0824949698189135, 0.0933333333333333, 0.114052953156823, 0.0810276679841897, 
0.0788954635108481, 0.0780399274047187, 0.0825335892514395, 0.0529531568228106, 
0.05625, 0.0456349206349206, 0.0583501006036217, 0.0666666666666667, 
0.0366598778004073, 0.0434782608695652, 0.0571992110453649, 0.0344827586206897, 
0.0633397312859885, 0.0692464358452139, 0.0645833333333333, 0.0694444444444444, 
0.0422535211267606, 0.0666666666666667, 0.0692464358452139, 0.0711462450592885, 
0.0749506903353057, 0.0598911070780399, 0.0959692898272553, 0.0672097759674134, 
0.0541666666666667, 0.0476190476190476, 0.0422535211267606, 0.0628571428571429, 
0.0509164969450102, 0.0454545454545455, 0.0532544378698225, 0.058076225045372, 
0.072936660268714, 0.0244399185336049, 0.04375, 0.0416666666666667, 
0.0663983903420523, 0.0228571428571429, 0.0509164969450102, 0.0237154150197628, 
0.0236686390532544, 0.0217785843920145, 0.0441458733205374, 0.0468431771894094, 
0.0479166666666667, 0.0476190476190476, 0.0402414486921529, 0.0438095238095238, 
0.0468431771894094, 0.0454545454545455, 0.0512820512820513, 0.0417422867513612, 
0.0518234165067179, 0.0753564154786151, 0.075, 0.0555555555555556, 
0.0724346076458753, 0.0533333333333333, 0.0794297352342159, 0.075098814229249, 
0.0788954635108481, 0.0598911070780399, 0.0460652591170825, 0.122199592668024, 
0.0875, 0.0892857142857143, 0.0945674044265594, 0.11047619047619, 
0.109979633401222, 0.0869565217391304, 0.120315581854043, 0.105263157894737, 
0.0978886756238004), y = structure(c(3L, 9L, 8L, 6L, 10L, 2L, 
1L, 7L, 5L, 4L, 3L, 9L, 8L, 6L, 10L, 2L, 1L, 7L, 5L, 4L, 3L, 
9L, 8L, 6L, 10L, 2L, 1L, 7L, 5L, 4L, 3L, 9L, 8L, 6L, 10L, 2L, 
1L, 7L, 5L, 4L, 3L, 9L, 8L, 6L, 10L, 2L, 1L, 7L, 5L, 4L, 3L, 
9L, 8L, 6L, 10L, 2L, 1L, 7L, 5L, 4L, 3L, 9L, 8L, 6L, 10L, 2L, 
1L, 7L, 5L, 4L, 3L, 9L, 8L, 6L, 10L, 2L, 1L, 7L, 5L, 4L, 3L, 
9L, 8L, 6L, 10L, 2L, 1L, 7L, 5L, 4L, 3L, 9L, 8L, 6L, 10L, 2L, 
1L, 7L, 5L, 4L, 3L, 9L, 8L, 6L, 10L, 2L, 1L, 7L, 5L, 4L, 3L, 
9L, 8L, 6L, 10L, 2L, 1L, 7L, 5L, 4L, 3L, 9L, 8L, 6L, 10L, 2L, 
1L, 7L, 5L, 4L, 3L, 9L, 8L, 6L, 10L, 2L, 1L, 7L, 5L, 4L), class = "factor", .Label = c("BUM", 
"DDR", "ETB", "EXP", "HED", "KON", "MAT", "PER", "PMA", "TRA"
))), .Names = c("milieu", "mycols", "variable", "value", "y"), row.names = c(NA, 
-140L), class = "data.frame")

另一个总和:

meandf <- structure(list(variable = structure(c(7L, 6L, 2L, 8L, 3L, 13L, 
14L, 12L, 9L, 10L, 1L, 11L, 5L, 4L, 7L, 6L, 2L, 8L, 3L, 13L, 
14L, 12L, 9L, 10L, 1L, 11L, 5L, 4L, 7L, 6L, 2L, 8L, 3L, 13L, 
14L, 12L, 9L, 10L, 1L, 11L, 5L, 4L, 7L, 6L, 2L, 8L, 3L, 13L, 
14L, 12L, 9L, 10L, 1L, 11L, 5L, 4L, 7L, 6L, 2L, 8L, 3L, 13L, 
14L, 12L, 9L, 10L, 1L, 11L, 5L, 4L, 7L, 6L, 2L, 8L, 3L, 13L, 
14L, 12L, 9L, 10L, 1L, 11L, 5L, 4L, 7L, 6L, 2L, 8L, 3L, 13L, 
14L, 12L, 9L, 10L, 1L, 11L, 5L, 4L, 7L, 6L, 2L, 8L, 3L, 13L, 
14L, 12L, 9L, 10L, 1L, 11L, 5L, 4L, 7L, 6L, 2L, 8L, 3L, 13L, 
14L, 12L, 9L, 10L, 1L, 11L, 5L, 4L, 7L, 6L, 2L, 8L, 3L, 13L, 
14L, 12L, 9L, 10L, 1L, 11L, 5L, 4L), .Label = c("Ausländer", 
"Bank", "Einrichtung für Ältere", "Einrichtungen für Jugendliche", 
"Einrichtungen für Kinder", "Gaststätte", "Geschäft", "Hausarzt", 
"Kein Mangel an Grünflaechen", "Keine Kriminalität", "Keine Umweltbelastung", 
"ÖPNV", "Park", "Sportstätte"), class = "factor"), value = c(0.0718023287061849, 
0.0693420423225302, 0.0753384763664876, 0.0827043835101492, 0.109631516692048, 
0.0765927537218141, 0.0870322381232645, 0.0515014684350035, 0.0683398169561522, 
0.0554744519820495, 0.0363337127130046, 0.0463575341160886, 0.0671060291182815, 
0.102443247236942, 0.0718023287061849, 0.0693420423225302, 0.0753384763664876, 
0.0827043835101492, 0.109631516692048, 0.0765927537218141, 0.0870322381232645, 
0.0515014684350035, 0.0683398169561522, 0.0554744519820495, 0.0363337127130046, 
0.0463575341160886, 0.0671060291182815, 0.102443247236942, 0.0718023287061849, 
0.0693420423225302, 0.0753384763664876, 0.0827043835101492, 0.109631516692048, 
0.0765927537218141, 0.0870322381232645, 0.0515014684350035, 0.0683398169561522, 
0.0554744519820495, 0.0363337127130046, 0.0463575341160886, 0.0671060291182815, 
0.102443247236942, 0.0718023287061849, 0.0693420423225302, 0.0753384763664876, 
0.0827043835101492, 0.109631516692048, 0.0765927537218141, 0.0870322381232645, 
0.0515014684350035, 0.0683398169561522, 0.0554744519820495, 0.0363337127130046, 
0.0463575341160886, 0.0671060291182815, 0.102443247236942, 0.0718023287061849, 
0.0693420423225302, 0.0753384763664876, 0.0827043835101492, 0.109631516692048, 
0.0765927537218141, 0.0870322381232645, 0.0515014684350035, 0.0683398169561522, 
0.0554744519820495, 0.0363337127130046, 0.0463575341160886, 0.0671060291182815, 
0.102443247236942, 0.0718023287061849, 0.0693420423225302, 0.0753384763664876, 
0.0827043835101492, 0.109631516692048, 0.0765927537218141, 0.0870322381232645, 
0.0515014684350035, 0.0683398169561522, 0.0554744519820495, 0.0363337127130046, 
0.0463575341160886, 0.0671060291182815, 0.102443247236942, 0.0718023287061849, 
0.0693420423225302, 0.0753384763664876, 0.0827043835101492, 0.109631516692048, 
0.0765927537218141, 0.0870322381232645, 0.0515014684350035, 0.0683398169561522, 
0.0554744519820495, 0.0363337127130046, 0.0463575341160886, 0.0671060291182815, 
0.102443247236942, 0.0718023287061849, 0.0693420423225302, 0.0753384763664876, 
0.0827043835101492, 0.109631516692048, 0.0765927537218141, 0.0870322381232645, 
0.0515014684350035, 0.0683398169561522, 0.0554744519820495, 0.0363337127130046, 
0.0463575341160886, 0.0671060291182815, 0.102443247236942, 0.0718023287061849, 
0.0693420423225302, 0.0753384763664876, 0.0827043835101492, 0.109631516692048, 
0.0765927537218141, 0.0870322381232645, 0.0515014684350035, 0.0683398169561522, 
0.0554744519820495, 0.0363337127130046, 0.0463575341160886, 0.0671060291182815, 
0.102443247236942, 0.0718023287061849, 0.0693420423225302, 0.0753384763664876, 
0.0827043835101492, 0.109631516692048, 0.0765927537218141, 0.0870322381232645, 
0.0515014684350035, 0.0683398169561522, 0.0554744519820495, 0.0363337127130046, 
0.0463575341160886, 0.0671060291182815, 0.102443247236942), milieu = structure(c(3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("BUM", 
"DDR", "ETB", "EXP", "HED", "KON", "MAT", "PER", "PMA", "TRA"
), class = "factor")), .Names = c("variable", "value", "milieu"
), row.names = c(NA, -140L), class = "data.frame")

我通常用这个命令来绘制它:

p <- ggplot(mdf, aes(x = variable, y = value)) +
  geom_line(data = transform(mdf, milieu = NULL), aes(group = y), colour = 'grey80') +
  geom_line(data = meandf, aes(x = variable, y = value, group = milieu),
            colour = 'grey50') +
  geom_line(aes(group = milieu, colour=mycols), size=1) +
  scale_colour_identity() +
  facet_wrap(~milieu, ncol = 2) +
  theme(axis.text.x = element_text(angle=90, hjust=1)) +
  theme(legend.position = 'none') +
  scale_y_continuous('') +
  xlab('')

生成此图片: enter image description here

到目前为止一切都很好。但是现在我必须把它放在一个文件中,但它对它来说很重要。因此,我必须将cols拆分为单独的图。但我无法弄清楚如何做到这一点。我认为命令facet_wrap(as.formula(paste("~", subset(milieu, milieu == LIST_OF_ONE_PLOT))), ncol=2)可以解决问题,但事实并非如此。我刚收到一条错误消息:

Error in layout_base(data, vars, drop = drop) : 
  At least one layer must contain all variables used for facetting

我只需要将列分隔到不同的图中。有人有解决方案吗?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

阐述我的评论:

p <- ggplot(mdf[as.numeric(mdf$milieu) < 6,], aes(x = variable, y = value)) +
  geom_line(data = transform(mdf, milieu = NULL), aes(group = y), 
            colour = 'grey80') +
  geom_line(data = meandf[as.numeric(meandf$milieu) < 6,], 
            aes(x = variable, y = value, group = milieu),
            colour = 'grey50') +
  geom_line(aes(group = milieu, colour=mycols), size=1) +
  scale_colour_identity() +
  facet_grid(milieu~.) +
  theme(axis.text.x = element_text(angle=90, hjust=1)) +
  theme(legend.position = 'none') +
  scale_y_continuous('') +
  xlab('')

print(p)