Changing the name of the ggplot2 legend without changing the graphic parameters

I found many topics about the name of the legend with ggplot2, but after a couple of hours I could not cope with 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 graphic 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 name of the legend, I get an unexpected additional legend:

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

gp1 +

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

 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 the new name of the legend?

+4
source share
1 answer

You must set the same name for all the aes() attributes that you used, for example, using the labs() function.

 gp1 + scale_colour_manual(values=c("red","blue"))+ labs(colour="NEW TITLE",linetype="NEW TITLE",shape="NEW TITLE") 
+3
source

All Articles