开发者

Colorize lines in a grouped and facet_wrap ggplot

开发者 https://www.devze.com 2023-03-27 17:47 出处:网络
I have the following data frame: structure(list(milieu 开发者_C百科= structure(c(3L, 9L, 8L, 6L, 10L, 2L,

I have the following data frame:

structure(list(milieu 开发者_C百科= 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), .Label = c("BUM", 
    "DDR", "ETB", "EXP", "HED", "KON", "MAT", "PER", "PMA", "TRA"
    ), class = "factor"), 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), .Label = c("#00CCFF", 
    "#00FD03", "#3168FF", "#97CB00", "#98CBF8", "#CCFCCC", "#FB02FE", 
    "#FE9900", "#FF0200", "#FFFD00"), class = "factor"), 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("Auslaender", "Umweltbelastung", 
    "OEPNV", "Kriminalitaet", "Einr. f. Kinder", "Mangel an Gruenflaechen", 
    "Gaststaette", "Geschaeft", "Bank", "Park", "Hausarzt", "Sportstaette", 
    "Einr. f. Jugendliche", "Einr. F. Aeltere"), 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("Geschaeft", 
    "Gaststaette", "Bank", "Hausarzt", "Einr. F. Aeltere", "Park", 
    "Sportstaette", "OEPNV", "Mangel an Gruenflaechen", "Kriminalitaet", 
    "Auslaender", "Umweltbelastung", "Einr. f. Kinder", "Einr. f. 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), .Label = c("BUM", 
    "DDR", "ETB", "EXP", "HED", "KON", "MAT", "PER", "PMA", "TRA"
    ), class = "factor")), .Names = c("milieu", "mycols", "variable", 
    "value", "y"), row.names = c(NA, -140L), class = "data.frame")

and want to plot it, according to ggplot2 plot table as lines (Thanks Ramnath) with:

ggplot(mdf, aes(x = variable, y = value)) +
  geom_line(data = transform(mdf, milieu = NULL), aes(group = y), colour = 'grey80')+
  geom_line(aes(group = milieu)) +
  scale_colour_manual(value=mdf$mycols) +
  facet_wrap(~milieu) +
  opts(axis.text.x = theme_text(angle=90, hjust=1))

The plot looks so far ok, but I can't set the colors of the lines (right now black) in each group to the according colors in mdf$mycols...

Colorize lines in a grouped and facet_wrap ggplot

Has someone an idea how I can set the colors in the plot? Thanks for your help! Dominik


Here is how you would do it in this case. You can control the thickness of the dark lines by changing "size" and the lightness of the other lines by changing "alpha". The only purpose of the colors I can see is that you don't need to use a legend and can easily look up the figure to compare different facets.

p1 = ggplot(mdf, aes(x = variable, y = value, colour = mycols)) + 
  geom_line(data = transform(mdf, milieu = NULL), aes(group = y), alpha = 0.5) +   
  geom_line(aes(group = milieu, colour = mycols), size = 1.2) + 
  scale_colour_identity() + 
  facet_wrap(~ milieu) +
  theme_bw() +
  opts(axis.text.x = theme_text(angle=90, hjust=1)) +
  opts(legend.position = 'none')

Colorize lines in a grouped and facet_wrap ggplot

0

精彩评论

暂无评论...
验证码 换一张
取 消