Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Misleading geom_smooth colouring in legend when more than 3 colours are used in plot

Tags:

r

ggplot2

Please consider the following

set.seed(28100)
random_data <- data.frame(x=rnorm(100),
                          y=rnorm(100),
                          colour=sample(c("a","b","c"),  100, replace=TRUE),
                          class=sample(c("alpha","beta"),  100, replace=TRUE))
require(ggplot2)
ggplot() +
  geom_smooth(data=random_data, aes(x=x, y=y, colour=colour, linetype=class), se=FALSE)

which returns

enter image description here

The color of the lines in the legend of class is misleading because blue - the default color for geom_smooth - is also used to map the attribute c.

How to set the default colour for geom_smooth to a neutral color - say - gray?

like image 336
CptNemo Avatar asked Oct 19 '22 09:10

CptNemo


1 Answers

You can override using guide in the scale functions like this. Here I am making it grey

 ggplot() +
       geom_smooth(data=random_data, aes(x=x, y=y, colour=colour, linetype=class), se=FALSE) +
       scale_linetype_manual(values=c("solid","dashed")
                             ,guide = guide_legend(override.aes = list(color = "grey")))

enter image description here

like image 115
Koundy Avatar answered Oct 21 '22 04:10

Koundy