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
相关产品推荐
相关产品推荐

