删除ggplot2中geom_ribbon图例键周围的填充

Bri*_*iro 5 r ggplot2

我想删除使用 geom_ribbon 创建的图例内的填充。请注意,这些答案并不能解决这个特定问题。

最小工作示例

library(ggplot2)
library(ggeffects)

fit <- lm(mpg ~ hp*disp, data= mtcars)

me <- ggeffect(fit,
               ci.lvl = .95,
               type = "fe",
               terms = c("hp", "disp"))

ggplot(data = me,
       mapping = aes(x = x, y = predicted, linetype = group)) +
  geom_line() +
  geom_ribbon(aes(ymin = conf.low,
                  ymax = conf.high),
              alpha = .5)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我想要这个情节,但我希望图例看起来像这样: 在此输入图像描述

jar*_*rot 4

好问题!一个潜在的解决方案是包含show.legend = FALSE并删除图例键背景theme()(这些操作中的每一个都不会单独“工作”),例如

library(tidyverse)
# install.packages("ggeffects")
library(ggeffects)

fit <- lm(mpg ~ hp*disp, data= mtcars)

me <- ggeffect(fit,
               ci.lvl = .95,
               type = "fe",
               terms = c("hp", "disp"))

ggplot(data = me,
       mapping = aes(x = x, y = predicted, linetype = group)) +
  geom_line() +
  geom_ribbon(aes(ymin = conf.low,
                  ymax = conf.high),
              alpha = .5, show.legend = FALSE) +
  theme(legend.key = element_blank())
Run Code Online (Sandbox Code Playgroud)

由reprex 包于 2021 年 8 月 24 日创建(v2.0.0)