Skip to content
Prev 276766 / 398500 Next

nesting scale_manual caracteristics in ggplot

Hi:

The problem is that you use different sets of labels in each
scale_*_manual. To get all of the scales to merge into one, you need
to have the same title, same breaks and same labels for the legend.
This gets you a single legend:

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'),
            labels=c('A: Line 1','B: Line 2','C: Line3','D: Line 4',
                     'E: Line 5','F: Line 6','G: Line 7')) +
    scale_shape_manual(breaks=c('A','B','C','D','E','F','G'),
            labels=c('A: Line 1','B: Line 2','C: Line3','D: Line 4',
                     'E: Line 5','F: Line 6','G: Line 7'),
            values = c(0, 1, 2, 3, 4, 5, 6)) +
    theme_bw() +
    geom_abline(aes(intercept = Intercept, slope = Slope,
                    colour = treatment, linetype=treatment),
                data = lines)

In your original, you had different line numbers as labels in
scale_colour_manual() and scale_shape_manual(). If you want to
maintain that, then you'll have to have separate legends for color and
shape.

HTH,
Dennis
On Tue, Nov 8, 2011 at 11:22 AM, Sigrid <s.stenerud at gmail.com> wrote: