如何移除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
相关产品推荐
相关产品推荐

