[R] nesting scale_manual caracteristics in ggplot

Sigrid s.stenerud at gmail.com
Tue Nov 8 20:22:59 CET 2011


Hi there,
I am having a little problem with combining three scale_manual commands in a
facet plot.  I am not able to combine the three different characteristics,
instead ending up with three different descriptions next to the graph for
the same geom.  I would like to see two separate labels (not three); one
describing lines 1-7 and the other 8-14. For each of the treatments (A-B) I
want a combination of color, line type and symbol.  How do I do this?

Here are my codes (Feel free to modify the example to make it easier to work
with. I was not able to do this while keeping the problem I wanted help
with)
df <-structure(list(year = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 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), treatment =
structure(c(1L, 
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 
6L, 7L, 7L, 7L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 
5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 
3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 1L, 1L, 
1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 
7L, 7L, 7L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 
5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 
3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 1L, 1L, 2L, 
2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 
7L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 
6L, 6L, 6L, 7L, 7L, 7L), .Label = c("A", "B", "C", "D", "E", 
"F", "G"), class = "factor"), total = c(135L, 118L, 121L, 64L, 
53L, 49L, 178L, 123L, 128L, 127L, 62L, 129L, 126L, 99L, 183L, 
45L, 57L, 45L, 72L, 30L, 71L, 123L, 89L, 102L, 60L, 44L, 59L, 
124L, 145L, 126L, 103L, 67L, 97L, 66L, 76L, 108L, 36L, 48L, 41L, 
69L, 47L, 57L, 167L, 136L, 176L, 85L, 36L, 82L, 222L, 149L, 171L, 
145L, 122L, 192L, 136L, 164L, 154L, 46L, 57L, 57L, 70L, 55L, 
102L, 111L, 152L, 204L, 41L, 46L, 103L, 156L, 148L, 155L, 103L, 
124L, 176L, 111L, 142L, 187L, 43L, 52L, 75L, 64L, 91L, 78L, 196L, 
314L, 265L, 44L, 39L, 98L, 197L, 273L, 274L, 89L, 91L, 74L, 91L, 
112L, 98L, 140L, 90L, 121L, 120L, 161L, 83L, 230L, 266L, 282L, 
35L, 53L, 57L, 315L, 332L, 202L, 90L, 79L, 89L, 67L, 116L, 109L, 
44L, 68L, 75L, 29L, 52L, 52L, 253L, 203L, 87L, 105L, 234L, 152L, 
247L, 243L, 144L, 167L, 165L, 95L, 300L, 128L, 125L, 84L, 183L, 
88L, 153L, 185L, 175L, 226L, 216L, 118L, 118L, 94L, 224L, 259L, 
176L, 175L, 147L, 197L, 141L, 176L, 187L, 87L, 92L, 148L, 86L, 
139L, 122L), country = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
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, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 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, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 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, 1L, 1L, 1L, 1L, 1L, 1L, 1L 
), .Label = c("high", "low"), class = "factor")), .Names = c("year", 
"treatment", "total", "country"), class = "data.frame", row.names = c(NA, 
-167L))


lines <- structure(list(`Line #` = 1:14, country = structure(c(2L, 2L, 2L,
2L,2L,2L,2L,1L, 1L,1L,1L,1L,1L,1L), .Label = c("high", "low"), class =
"factor"),treatment = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L,1L, 2L,
3L,4L,5L,6L,7L), .Label = c("A", "B", "C","D", "E", "F","G","H" ), class =
"factor"), Intercept = c(81.47, 31.809,69.892,82.059,106.392,45.059,38.809,
67.024, 17.357, 105.107,79.191,91.357,5.691,24.357), Slope = c(47.267,
20.234,33.717,14.667,13.434,25.817,21.967, 47.267, 20.234,
33.717,14.667,13.434,25.817,21.967)), .Names = c("Line #", "country", 
"treatment", "Intercept", "Slope"), class = "data.frame", row.names = c(NA, 
-14L))

ggplot(data = df, aes(x = year, y = total, colour = treatment,
linetype=treatment)) + 
        geom_point(aes(shape = treatment)) +
      facet_wrap(~country) + 
        scale_colour_manual(breaks=c('A','B','C','D','E','F','G'),
values=c('A'='black','B'='black', 'C'='grey','D'='grey',
'E'='red','F'='grey', 'G'='red'),  labels=c('A: Line 1','B: Line 2','C:
Line3','D: Line 4', 
                        'E: Line 5 ','F:Line 6','G:Line 7'))+
scale_linetype_manual(breaks=c('A','B','C','D','E','F','G'),
values=c('A'='solid','B'='dotted', 'C'='solid','D'='dashed',
'E'='dashed','F'='dotted', 'G'='dotted'))+
        scale_shape_manual(breaks=c('A','B','C','D','E','F','G'), 
                labels=c('A: Line 8','B: Line 9','C: Line 10','D: Line 11', 
                        'E: Line 12 ','F:Line 13','G:Line 14'), 
                values = c(0, 1, 2, 3, 4, 5, 6)) +

        theme_bw()+ geom_abline(aes(intercept = Intercept, slope = Slope,
colour = treatment, linetype=treatment), data = lines) 


Thank you,
Sigrid


--
View this message in context: http://r.789695.n4.nabble.com/nesting-scale-manual-caracteristics-in-ggplot-tp4017171p4017171.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list