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

如何移除ggline绘图图例中的线条?仅保留supp分组的点图例

解决ggline图例保留点、移除线条的问题

要移除ggline绘制图表图例中的线条,仅保留supp分组对应的点图例,可以通过修改图例的几何属性覆盖来实现,具体方案如下:

方法:使用guides()覆盖图例元素

ggline默认会把mean_se的线条和jitter的点都加入图例,我们可以通过guide_legend()的override.aes参数,把图例中的线条设为空白,只保留点。

修改后的完整代码

library(ggpubr)    

# 创建图A(隐藏图例)
a <- ggline(ToothGrowth, x = "dose", y = "len", 
            add = c("mean_se", "jitter"),
            color = "supp", palette = "jco") +
  theme(legend.position = "none")

# 创建图B(修改图例,仅保留点)
b <- ggline(ToothGrowth, x = "dose", y = "len", 
            add = c("mean_se", "jitter"),
            color = "supp", palette = "jco") +
  guides(color = guide_legend(override.aes = list(linetype = "blank")))

# 合并图表
ggarrange(a, b, common.legend = TRUE, labels = c("A", "B"), legend = "right")

原理说明

  • guides(color = guide_legend(override.aes = list(linetype = "blank"))):强制将图例中color分组对应的线条类型设为空白,只显示点标记。
  • 无需嵌套plot_grid(),直接将a和b传入ggarrange即可实现分图布局。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 19:25:28