Appropriate colour legend when mixing geom_boxplot and geom_step (or geom_line)

Hi,

If you look at the following reprex, you will see that I have a graph with boxplots and an overlaid line.

I am not hapy with the legend produced, as it shows a boxplot to illustrate the colour of the data for which a line was used. Ideally I would like a boxplot in the legend for 'Model', and a black line for 'Obs'.

I can't work out how to do this.

Any suggestions?

Thanks,
Ron.

library(tidyverse)

tbl <- tibble(year = 2000:2005,
              q025 = c(1,2,3,1,2,NA),
              q250 = c(2,3,4,2,3,NA),
              q500 = c(3,4,5,3,4,NA),
              q750 = c(4,5,6,4,5,NA),
              q975 = c(5,6,7,5,6,NA),
              obs  = c(3.5,4.5,4.5,2.5,4.5,4.5))


pp <- ggplot(tbl) +
      geom_boxplot(aes(x = year,
                       ymin = q025,
                       lower = q250,
                       middle = q500,
                       upper = q750,
                       ymax = q975,
                       group = year,
                       colour = 'Model',
                       fill = 'Model'),
                   stat = 'identity') +
      geom_step(aes(x = year-0.5,
                    y = obs,
                    colour = 'Obs', fill = 'Obs')) +
      scale_fill_manual(name = 'legend',
                        values=c(Model = "#ffbfbf",
                                 Obs = NA),
                        drop = FALSE) +
      scale_colour_manual(name = 'legend', values = c(Model = 'red', Obs = 'black'))
#> Warning: Ignoring unknown aesthetics: fill

print(pp)
#> Warning: Removed 1 rows containing missing values (geom_boxplot).

Created on 2020-02-12 by the reprex package (v0.3.0)

A couple kind of hacky workarounds which keep the current "shape" you're using for the data:

Currently, you're manually saying that there will be one key, legend, by specifying the colour and fill with your two scale_*_manual() calls that have the same name (legend). You can specify the key glyph (the shape in the key) using the draw_key_*() family of functions:

Another approach would be to just use the colour scale for the line (Obs), and the fill scale for the boxplot shape. That way, the default glyphs will look to what geoms using the scale (note, I've specified the colour of the line for the boxplot outside of aes(), and given a dummy name for the legend for Obs).

library(tidyverse)

tbl <- tibble(year = 2000:2005,
              q025 = c(1,2,3,1,2,NA),
              q250 = c(2,3,4,2,3,NA),
              q500 = c(3,4,5,3,4,NA),
              q750 = c(4,5,6,4,5,NA),
              q975 = c(5,6,7,5,6,NA),
              obs  = c(3.5,4.5,4.5,2.5,4.5,4.5))

pp <- ggplot(tbl) +
  geom_boxplot(aes(x = year,
                   ymin = q025,
                   lower = q250,
                   middle = q500,
                   upper = q750,
                   ymax = q975,
                   group = year,
                   fill = 'Model'),
               colour = "red",
               stat = 'identity') +
  geom_step(aes(x = year-0.5,
                y = obs,
                colour = 'Obs')) +
  scale_fill_manual(name = 'legend',
                    values=c(Model = "#ffbfbf",
                             Obs = NA),
                    drop = FALSE) +
  scale_colour_manual(name = 'blah', values = c(Model = 'red', Obs = 'black'))

pp
#> Warning: Removed 2 rows containing missing values (geom_segment).
#> Warning: Removed 1 rows containing missing values (geom_segment).

Created on 2020-02-12 by the reprex package (v0.3.0.9001)

1 Like

Thanks @mara.

The key_glyph stuff looks interesting - I hadn't come across the draw_key_* functions before.

I think for now the second solution will achieve what I want.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.