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()
,如 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"))
我有两个地块。一条线条平滑:
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()
,如 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"))