簡體   English   中英

ggplot2:使用stat_smooth時的透明圖例背景

[英]ggplot2: Transparent legend background when stat_smooth is used

我有兩個情節。 一條線條平滑:

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_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"))

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM