2015-06-21 143 views
2

我想從圖例框中刪除灰色(這是因爲來自geom_smooth的SE,因此存在)。儘管如此,我仍想讓SE保持在實際情節中。所以在圖例框中,我只想要線條的顏色,而不是陰影。這裏是一個例子:從ggplot2的圖例中刪除灰色

library(ggplot2) 

x <- rnorm(100) 
y <- rnorm(100) 
g_ <- sample(c("group1", "group2"), 100, replace = TRUE) 

ggplot(data.frame(x, y, g_), aes(x = x, y = y, color = g_)) + geom_smooth() 

回答

3

這是一種方法。首先,畫出可信區間的線條,但沒有傳說。然後,畫出沒有間隔和圖例的線條,最後將圖例的顏色設爲白色。

ggplot(data.frame(x, y, g_), aes(x = x, y = y, color = g_)) + 
    geom_smooth(show_guide=FALSE) + 
    geom_smooth(fill=NA) + 
    theme(legend.key = element_rect(fill = "white")) 

enter image description here

+0

創造性的解決方案! –