Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single legend when using group, linetype and colour in ggplot2?

I am creating a very simple plot that groups data and uses the grouping variable to determine linestyle and colour. I then override those using 'scale_linetype_manaul' and 'scale_colour_manual'. So far so good, but when I try to modify legend labels or its title, the legend splits into two parts: one for linetype and one for colour. I just want one legend, but with the custom labels and title.

Following this question, I made sure to name both scale objects the same, but that doesn't appear to help.

Minimal example:

X <- data.frame(TPP=factor(c(1,5,10,1,5,10,1,5,10)), 
                value=c(-0.035819, 0.003356, 0.066091, -0.028039, 0.004333, 0.060292, -0.023115, 0.005661, 0.058821), 
                horizon=c(1,1,1,2,2,2,3,3,3))
ggplot(X, aes(x=horizon, y=value, group=TPP, col=TPP, linetype=TPP))+
  geom_line(size=1)+
  scale_linetype_manual(name="X", values = c("solid","dashed", "dotted")) +
  scale_color_manual(name="X", values = c("black", "red", "blue"), labels=c("Low", "5","High"))

This yields the following figure with two legends. How can I recombine those legends again, with custom labels and a title?

like image 423
Thijs Avatar asked Jul 24 '15 12:07

Thijs


People also ask

How to add legend in R using Ggplot2?

Working with Legends in R using ggplot2 Last Updated : 28 Jul, 2021 A legend in a plot helps us to understand which groups belong to each bar, line, or box based on its type, color, etc. We can add a legend box in R using the legend () function.

How to select colors and line types manually in ggplot2?

Colors and line types of the plot are selected based on the default specifications of the ggplot2 package. The following R programming code illustrates how to select colors and line types manually. We can change the line styles with the scale_linetype_manual function and the color of each line with the scale_color_manual function.

How to change the color of the legend formed from the plot?

For a plot dealing with data that can be compared with one another, an appropriate legend will be generated by default for reference. The legend so generated can be easily customized as per requirements. Here, we will change the color and size of the title of the legend formed from the plot. This can be done using the theme () function.

How do I change the Order of legend guides in ggplot?

Set ggplot legend guides for each aesthetic when you have many legends. R function: guides () Change the legend order in the situation where you have multiple legends (or multiple guides) generated by using multiple aesthetics (shape, color, size, fill, etc) in the plot.


1 Answers

This might help:

 ggplot(X, aes(x=horizon, y=value, group=TPP, col=TPP, linetype=TPP))+geom_line(size=1)+
  scale_linetype_manual(name="X", values = c("solid","dashed", "dotted"),labels=c("Low", "5","High")) +
  scale_color_manual(name ="X", values = c("black", "red", "blue"),labels=c("Low", "5","High"))

enter image description here

If the labels defined in scale_color_manual and in scale_linetype_manual are different, or if they are specified in only one of them, you will obtain two different legends.

like image 174
RHertel Avatar answered Oct 01 '22 08:10

RHertel