2016-02-04 87 views
2

我有一個情節,我想用不同顏色的點,但繪製基於所有點的線性迴歸:如何從ggplot2圖例中刪除線條美學?

library(ggplot2) 

set.seed(1) 

df <- data.frame(x=rnorm(100), 
       y=rnorm(100), 
       group=factor(rep(1:2,each=50))) 

ggplot(df,aes(x=x,y=y,color=group)) + 
    stat_smooth(aes(group=1), method="lm", fill=NA) + 
    geom_point() + theme_bw() 

enter image description here

的問題是,當我使用stat_smooth()來添加回歸線,它添加了我不想要的圖例中的行。我無法重寫顏色以從圖例中刪除線條,因爲我需要點的顏色。我怎樣才能從傳說中刪除線條,但保留點?

回答

5

所有你需要做的就是添加show.legend = FALSEstat_smooth

ggplot(df, aes(x = x, y = y, color = group, group = 1)) + 
    geom_smooth(method = "lm", se = FALSE, show.legend = FALSE) + 
    geom_point() + 
    theme_bw() 

plot with no lines in legend