2015-10-22 45 views
6

我有兩個圖。一個與平滑線:ggplot2:使用stat_smooth時的透明圖例背景

library(splines) 
library(ggplot2) 

ggplot(mtcars, aes(hp, qsec)) + stat_smooth(aes(group = cyl, 
     colour = factor(cyl)), 
     method = "glm", 
     formula = y ~ ns(x, 1), 
     level = 1e-9, 
     size = I(1)) + 
    theme(panel.background=element_rect(fill="transparent",colour=NA), 
     plot.background=element_rect(fill="transparent",colour=NA), 
     legend.key = element_rect(fill = "transparent", colour = "transparent")) 

,一個沒有:

ggplot(mtcars, aes(hp, qsec)) + 
    geom_point(aes(group = cyl, colour = factor(cyl))) + 
    theme(panel.background=element_rect(fill="transparent",colour=NA), 
     plot.background=element_rect(fill="transparent",colour=NA), 
     legend.key = element_rect(fill = "transparent", colour = "transparent")) 

我怎麼能在第一時間拿到地塊白色或透明的傳奇背景是什麼? 爲什麼相同的主題命令會在第二個圖表中完成這項工作?

回答

8

看起來像灰色背景來自stat_smooth(),如解釋here。添加se=FALSE,該停用的置信區間,似乎解決它:

ggplot(mtcars, aes(hp, qsec)) + stat_smooth(aes(group = cyl, 
    colour = factor(cyl)), 
    method = "glm", 
    formula = y ~ ns(x, 1), 
    level = 1e-9, 
    size = I(1), 
    se = FALSE) + 
    theme(panel.background=element_rect(fill="transparent",colour=NA), 
     plot.background=element_rect(fill="transparent",colour=NA), 
     legend.key = element_rect(fill = "transparent", colour = "transparent")) 

enter image description here