Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing ggplot2 legend title without altering graphical parameters

I have found many topics about the legend title with ggplot2 but after a couple of hours I have not been able to handle my situation.

Here is the dataset:

> dat
  FACTOR1 FACTOR2 lsmean lower.CL upper.CL
1       A      aa   26.2     25.6     26.8
2       B      aa   24.8     23.9     25.7
3       A      bb   26.0     25.2     26.7
4       B      bb   24.9     23.9     25.9
5       A      cc   24.4     23.9     24.8
6       B      cc   23.9     22.9     25.0
7       A      dd   24.9     24.3     25.6
8       B      dd   23.2     22.3     24.0

And the graphic of interest:

gp0 <- ggplot(dat, aes(x=FACTOR2, y=lsmean, group=FACTOR1, colour=FACTOR1)) 
( gp1 <- gp0 +  geom_line(aes(linetype=FACTOR1), size=.6) + 
    geom_point(aes(shape=FACTOR1), size=3) + 
    geom_errorbar(aes(ymax=upper.CL, ymin=lower.CL), width=.1) +
    geom_errorbar(aes(ymax=upper.CL, ymin=lower.CL), width=.1) ) 

gp1

If I use scale_colour_manual() to change the legend title then I get an unexpected additional legend:

gp1 + scale_colour_manual("NEW TITLE",values=c("red","blue"))

gp1+

I suppress this additional legend with scale_"aes"_manual("guide=none", values=...) but I don't understand how to control the parameters (the style of points and lines):

gp1 + scale_colour_manual("NEW TITLE",values=c("red","blue")) +
    scale_shape_manual(guide = 'none', values=c(1,2))   + 
    scale_linetype_manual(guide = 'none', values=c(1,3))  

gp1++

Please how to reproduce the first plot with and only with a new legend title ?

like image 283
Stéphane Laurent Avatar asked Feb 24 '13 19:02

Stéphane Laurent


People also ask

How do I change the title of a legend in ggplot2?

Method 1: Change Legend Title using guides() Function. Now if we want to change Legend Title then we have to add guides and guide_legend functions to the geom_point function. Inside guides() function, we take parameter named 'color' because we use color parameter for legend in ggplot() function.

How do I rename labels in legend?

Click on the legend name you want to change in the Select Data Source dialog box, and click Edit. Note: You can update Legend Entries and Axis Label names from this view, and multiple Edit options might be available. Type a legend name into the Series name text box, and click OK.

How do I add a title to a legend in R?

In case you need to add a title to the legend, in order to add some description of the elements of the legend, you can use the title argument. Note that you can customize the color of the text with the title. col argument and that you can make a horizontal adjustment of the title with the title. adj argument.


1 Answers

You have to set the same title for all aes() attributes you have used, for example, using function labs().

gp1 + scale_colour_manual(values=c("red","blue"))+
    labs(colour="NEW TITLE",linetype="NEW TITLE",shape="NEW TITLE")
like image 178
Didzis Elferts Avatar answered Oct 19 '22 21:10

Didzis Elferts