sarahw sarahw - 4 years ago 186
R Question

Adding a second legend to a ggplot2 graph - data points and regression lines

I am trying to have two separate legends for two data series. Basically I have a data series and regression lines, and I would like one legend with the regression data, preferably with the adjr2 reported, and another legend with the points from the dataset. I'm new to ggplot2, and I have not been able to figure out guides(guide_legend).

Here's my code. I've managed to get just the points to show on the legend, so I would like to create a second one for the dashed regression lines. I have a column for the adjusted r2 values, which I would like to call for the 2nd legend.

g <- ggplot(acci_bud, aes(x = Elevation, y = DOY, color = Year)) +
geom_errorbar(aes(ymin = DOY -se, ymax = DOY +se), width = .1, show.legend = F)
g <- g + geom_point()
g <- g + geom_abline(data = acci_elev_slope, aes(slope = slope_coeff,
intercept = slope_int, color = year),linetype = 'dashed', show.legend = F)


plot I've managed to make, minus the second legend

I can make this plot with the base package, but I would like to be able to have a basic script that I use for multiple datasets and I think ggplot2 is more conducive to this.

base package plot, goal is something similar to this using ggplot2

EDIT: this should be reproducible:

acci_bud <- data.frame( Elevation = rep((seq(from = 500, to = 1250, by = 50)),7),
DOY = sample(75:180, 112, replace = TRUE),
Year = rep(2009:2015, each = 16),
se = 2)

acci_elev_slope <- data.frame ( year = seq(from = 2009, to = 2015, by = 1),
slope_coeff = c(0.05, 0.03, 0.051, 0.030, 0.025, 0.025, 0.034),
slope_int = c(75.76, 79.52, 81.80, 92.71, 75.76, 72.07, 90.6),
adjr2 = c(0.87, 0.79, 0.65, 0.89, 0.20, 0.57, 0.90))


acci_bud$Year <- as.factor(acci_bud$Year)
acci_elev_slope$year <- as.factor(acci_elev_slope$year)

g <- ggplot(acci_bud, aes(x = Elevation, y = DOY, color = Year)) +
geom_errorbar(aes(ymin = DOY -se, ymax = DOY +se), width = 0.1, show.legend = F)
g <- g + geom_point()
g <- g + geom_abline(data = acci_elev_slope,
aes(slope = slope_coeff, intercept = slope_int, color = year),
linetype = 'dashed', show.legend = F)
g

Answer Source

You could achieve this by adding a linetype legend and then jumping through hoops to set the title/label of the new legend, setting colors to use in both legends, and then overriding how the legends look via override.aes in guide_legend.

ggplot(acci_bud, aes(x = Elevation, y = DOY, color = Year)) + 
    geom_errorbar(aes(ymin = DOY -se, ymax = DOY +se), width = 0.1, show.legend = FALSE) + 
    geom_point() + 
    geom_abline(data = acci_elev_slope, 
                 aes(slope = slope_coeff, intercept = slope_int, 
                     color = year, linetype = year)) +
    scale_linetype_discrete(name = "slopes", labels = acci_elev_slope$slope_coeff) +
    scale_color_manual(values = rainbow(length(unique(acci_bud$Year)))) +
    guides(color = guide_legend(override.aes = list(linetype = 0)),
          linetype = guide_legend(override.aes = list(linetype = 2, 
                                            color = rainbow(length(unique(acci_bud$Year))))))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download