Enrico is a colleague of mine in Quantide. Some days ago, he asked me how to get two different legends for several coloured lines.

This is the wanted result:

immagine-1

The first version of his plot sounds like the following one:

immagine-2In this plot, he has six lines of four different colors:

  1. line of values for group 1 (red),
  2. line of values for group 2 (lightblue),
  3. horizontal lines for 95% limits (orange),
  4. horizontal lines for 99% limits (dark red).

When an aesthetic is mapped to data, the legend will be shown automatically. The plot shows the lines for group 1 and group 2.

To show the legend also for the horizontal lines, color of horizontal lines should be mapped to aesthetic.

Pay attention to horizontal lines positions, they have to be included in a data frame:

immagine-3Colours are mapped to “dummy” data. Since the values are shown in the legend, Enrico used labels as “dummy” data.

immagine-4To set colors for horizontal lines my colleague Enrico used scale_color_manual(). This function allows users to choose which colors should be used for each line.

immagine-5Enrico set four values for scale_color_manual: the first and second refers to the first colours mapped, that are the levels of group. The third value refers to the color Enrico called “95% CI”, the fourth value refers to the color Enrico called “99% CI”.

Of course, using scale_color_manual() information about default colors is lost so the colors for group should be declared. This can be annoying for (at least) two reasons: the number of groups may be not known and palettes are a better choice to get a set of colors. A work around consists of creating a function that rebuilds colors created by ggplot by default.

The function is passed to the value argument of the scale_color_manual() function, replacing the color names such as “black” and “blue” in the above plot.

immagine-6At this point, all works and Enrico was satisfied of its work. But the title of the legend, “group”, refers to the first two lines.

immagine-7The title of the legend can be easily changed, as it is the first argument of the scale_color_manual() function. But the best solution for his plot, was two different legends: one for group levels and one for the CI horizontal lines. So Enrico asked me if I know how to do this with ggplot. “Yes, of course” was my reply. “I already did it, in the past”. But this was not true. What I did, was the opposite: I merged several aesthetics in a single legend. Maybe I will write a post about this topic, too. I was pretty sure that ggplot doesn’t implement a solution to have two legends for the same aesthetic by default. This is not coherent with the grammar idea (the GG in ggplot stands for Grammar of Graphics) and the strong link between plot and data behind ggplot2 package. At the same time, I was pretty sure that someone find a workaround to this issue and share its solution on the net. Maybe someone found a solution but she/he did not share this solution with us. I was astonished, the reply to my question was “you can’t do it” (see, for example http://stackoverflow.com/questions/17642190/how-to-set-multiple-legends-for-the-same-aesthetic-in-ggplot2).

During my attempts to get two legends, I tried with two different aesthetics, adding an aesthetic not really useful like size:

immagine-8I got two legends, but the legend about horizontal lines does not help to understand the plot, beacuse it does not show the right color of the lines. This is coherent with the goal of the legend, that is clarify what the size aesthetic means, but does not help my readers to understand which line refers to which confidence interval (95% or 99%).

Fortunately, guide_legend() allows to change some aesthetic, like the colour, to the elements of the legend.

immagine-9This is what I was looking for: multiple legends for a single aesthetic. Finally, I only added the name for the legend of the group level, and all was done. As seen above, “hue” is the default scale for ggplot discrete colours. So the function, is scale_color_hue().

immagine-10That’s all folks!!! Just some notes:

  1. “Dummy” data are useful for this example, and so they are used also to set labels. By the way, labels for the legend can be set using the labels argument of the scale_size_manual()function (and, of course, of any other scale_*_* function).
    immagine-11
  2. As seen above, changing labels will change the order of the legends. To set the order, the guide_legend() function provides an order argument.
    immagine-12Article originally published on Nov 2015 on MilanoR and now modified by Veronica Giro to maintain compatibility with the latest version of ggplot2.