1

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
Uwe
  • 41,420
  • 11
  • 90
  • 134
sarahw
  • 11
  • 4
  • 2
    Provide a reproducible example with some inbuilt datasets. http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Koundy Feb 04 '17 at 06:31

1 Answers1

0

You could achieve this by adding a fake fill legend to the points layer and using show.legend = TRUE in geom_abline. Then you can jump 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(aes(fill = Year)) + 
    geom_abline(data = acci_elev_slope, 
              aes(slope = slope_coeff, intercept = slope_int, 
                  color = year), linetype = "dashed", show.legend = TRUE) +
    scale_fill_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)),
          fill = guide_legend(override.aes = list(shape = NA, 
                                            color = rainbow(length(unique(acci_bud$Year))))))
aosmith
  • 34,856
  • 9
  • 84
  • 118
  • Is it possible to make the lines all one type? I like that they are dashed in the legend, but I don't seem to understand the override enough to get all of the lines on the plot to just be dashed. – sarahw Feb 13 '17 at 18:30
  • You can leverage things using `fill` instead of `linetype`. – aosmith Feb 13 '17 at 21:09