Enrico 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 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 fill. Moreover, in the case of lines, fill is also meaningless.

1

2

3

4

5

6

7

8

9

10

11

12

ggplot(data=dfr,mapping=aes(x=id,y=value))+

geom_line(mapping=aes(colour=group),show_guide=TRUE)+

geom_hline(

mapping=aes(yintercept=c(-1,1)*qnorm(0.95),fill="95% CI"),

color="orange"

)+

geom_hline(

mapping=aes(yintercept=c(-1,1)*qnorm(0.99),fill="99% CI"),

color="darkred"

)

I got two legends, but the legend about horizontal lines does not help to understand the plot, beacuse it does not show the color of the line. This is coherent with the goal of the legend, that is clarify what the fill aesthetic (and not the color) 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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

ggplot(data=dfr,mapping=aes(x=id,y=value))+

geom_line(mapping=aes(colour=group),show_guide=TRUE)+

geom_hline(

mapping=aes(yintercept=c(-1,1)*qnorm(0.95),fill="95% CI"),

color="orange"

)+

geom_hline(

mapping=aes(yintercept=c(-1,1)*qnorm(0.99),fill="99% CI"),

color="darkred"

)+

scale_fill_manual(

"CI horizontal line",values=rep(1,4),

guide=guide_legend(override.aes=list(colour=c("orange","darkred")))

)

This 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().

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

ggplot(data=dfr,mapping=aes(x=id,y=value))+

geom_line(mapping=aes(colour=group),show_guide=TRUE)+

geom_hline(

mapping=aes(yintercept=c(-1,1)*qnorm(0.95),fill="95% CI"),

color="orange"

)+

geom_hline(

mapping=aes(yintercept=c(-1,1)*qnorm(0.99),fill="99% CI"),

color="darkred"

)+

scale_color_hue("Group")+

scale_fill_manual(

"CI horizontal line",values=rep(1,4),

guide=guide_legend(override.aes=list(colour=c("orange","darkred")))

)

That's all folks!!! Just some notes:

“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_fill_manual() function (and, of course, of any other scale_*_* function).

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

ggplot(data=dfr,mapping=aes(x=id,y=value))+

geom_line(mapping=aes(colour=group),show_guide=TRUE)+

geom_hline(

mapping=aes(yintercept=c(-1,1)*qnorm(0.95),fill="95% CI"),

color="orange"

)+

geom_hline(

mapping=aes(yintercept=c(-1,1)*qnorm(0.99),fill="99% CI"),

color="darkred"

)+

scale_color_hue("Group")+

scale_fill_manual(

"CI horizontal line",values=rep(1,4),

guide=guide_legend(override.aes=list(colour=c("orange","darkred"))),

labels=c("CI of 95%","CI of 99%")

)

As seen above, changing labels will change the order of the legends. To set the order, the guide_legend() function provides an order argument.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

ggplot(data=dfr,mapping=aes(x=id,y=value))+

geom_line(mapping=aes(colour=group),show_guide=TRUE)+

geom_hline(

mapping=aes(yintercept=c(-1,1)*qnorm(0.95),fill="95% CI"),

color="orange"

)+

geom_hline(

mapping=aes(yintercept=c(-1,1)*qnorm(0.99),fill="99% CI"),

color="darkred"

)+

scale_color_hue("Group",guide=guide_legend(order=1))+

scale_fill_manual("CI horizontal line",values=rep(1,4),

guide=guide_legend(

override.aes=list(colour=c("orange","darkred")),

order=2

),

labels=c("CI of 95%","CI of 99%")

)

At the moment, I have no ideas why I need show_guide = TRUE in the geom_line. Removing the show_guide = TRUE in the geom_line or adding it in the geom_hline will produce different legends. Any suggestion will be accepted.