change Geom_smooth default legend color

2019-06-08 07:45发布

I am using geom_smooth and it gives me default legend line color blue that I cannot use in the plot. Is there a way of changing the default color for geom_smooth legend line?

for example, I wand to change the legend colour to black for line type in the following plot

      library(reshape2)
    library(ggplot2)
d1<-melt(mtcars,id=c("mpg","cyl"))
        p<-ggplot(d1,aes(x=mpg,y=value,factor=variable,color=cyl))
        p1<-p+geom_smooth(aes(linetype = as.factor(cyl)),se = F,stat = "smooth",method = "glm",size=.5,  inherit.aes = T)+
          scale_linetype_manual("line type",labels = rp, values=c(1,5,4,3))

enter image description here

2条回答
戒情不戒烟
2楼-- · 2019-06-08 08:00

if you add + guides(linetype = guide_legend(override.aes= list(color = "black"))) to your last plot do

This solves thanks @Mike

查看更多
祖国的老花朵
3楼-- · 2019-06-08 08:09

Per my comment, if you add an override to your legend you can change how the color displays. This should work:

p1<-p+geom_smooth(aes(linetype = as.factor(cyl)),se = F,stat = "smooth",method = "glm",size=.5,  inherit.aes = T)+
  scale_linetype_manual("line type",labels = c("100 yr", "50 yr", "25 yr"), values=c(1,5,4,3)) + 
  guides(linetype = guide_legend(override.aes= list(color = "black")))

And the resulting plot is:

enter image description here

查看更多
登录 后发表回答