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

ske*_*tor 8 plot r smoothing ggplot2

我有两个情节.一条线条平滑:

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"))
Run Code Online (Sandbox Code Playgroud)

一个没有:

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"))
Run Code Online (Sandbox Code Playgroud)

如何在第一个图中获得白色或透明的图例背景?为什么相同的主题命令在第二个绘图中完成工作?

coc*_*mas 11

看起来灰色背景来自于此stat_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"))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述