R ggplot2:使用stat_smooth时的透明图例背景

R ggplot2:使用stat_smooth时的透明图例背景,r,plot,ggplot2,smoothing,R,Plot,Ggplot2,Smoothing,我有两个情节。具有平滑线条的一个: 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(p

我有两个情节。具有平滑线条的一个:

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"))
如何在第一个情节中获得白色或透明的图例背景?
为什么相同的主题命令在第二个绘图中起作用呢?

如前所述,灰色背景似乎来自
stat\u smooth()
。添加
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"))