You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ggplot2如何移除geom_ribbon生成的图例内部填充色块

移除ggplot2 geom_ribbon图例填充色块的解决方案

以下两种方案均可实现仅保留线型标识、移除图例内部填充色块的需求:

  • 方案一:关闭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"))

# 仅在geom_ribbon中新增show.legend = FALSE参数即可
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)
  • 方案二:通过覆盖图例样式实现,适合需要保留ribbon图层其他映射图例的场景
library(ggplot2)
library(ggeffects)

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

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

# 新增guides行覆盖linetype图例的fill属性
ggplot(data = me,
       mapping = aes(x = x, y = predicted, linetype = group)) +
  geom_line() +
  geom_ribbon(aes(ymin = conf.low,
                  ymax = conf.high),
              alpha = .5) +
  guides(linetype = guide_legend(override.aes = list(fill = NA)))

内容的提问来源于stack exchange,提问作者Brigadeiro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 00:30:00