更改Geom_smooth默认图例颜色

时间:2017-07-08 17:29:55

标签: r plot ggplot2 dplyr

我正在使用geom_smooth,它给我默认的图例线颜色为蓝色,我无法在图中使用。有没有办法更改geom_smooth图例行的默认颜色?

例如,我想在下图中将线条类型的图例颜色更改为黑色

      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 个答案:

答案 0 :(得分:2)

根据我的评论,如果您为图例添加覆盖,则可以更改颜色的显示方式。这应该有效:

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")))

结果图是:

enter image description here

答案 1 :(得分:0)

如果您在上一个情节中添加+ guides(linetype = guide_legend(override.aes= list(color = "black")))

这解决了感谢@Mike

相关问题