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

修改带点折线图图例:移除图例线条仅保留点形状

问题描述

我绘制的带点折线图中,图例里的点形状被线条覆盖了,希望移除图例中的线条,只显示点形状。我的代码如下:

p <- ggplot(data=cumdf, aes(x=quarters2)) +
  geom_line(aes(y = mean_cumsum, colour='Platform participants'), size = 1.5)+
  geom_point(aes(y = mean_cumsum, colour='Platform participants', shape='Platform participants'), size=3) +
  geom_line(aes(y = mean_interventions, colour='Actions'), size=1.5) +
  geom_point(aes(y = mean_interventions, colour='Actions', shape='Actions'), size=3) +
  geom_line(aes(y = mean_sales, colour="Adopters"), size=1.5) +
  geom_point(aes(y = mean_sales, colour='Adopters', shape='Adopters'), size=3) +
  xlab("Quarters") +
  ylab("Cumulative occurences") +
  scale_shape_manual("", values=c("Platform participants" = 16, "Actions" = 17, "Adopters"=15)) +
  scale_colour_manual("",breaks = c("Platform participants", "Actions", "Adopters"),
                      values = c ("#C80000", "#696969", "#4E33FF")) +
  theme_stata(base_size = 15, base_family = "sans", scheme = "s2color") +
  scale_x_continuous(n.breaks=14) +
  geom_vline(xintercept=3, linetype='dashed', size=1.7)
p

当前图表输出

解决方案

有两种简单方法可以实现需求:

方法一:让线条不生成图例

给每个geom_line添加show.legend = FALSE参数,直接让线条不参与图例的构建,这样图例只会保留点的形状和颜色:

p <- ggplot(data=cumdf, aes(x=quarters2)) +
  geom_line(aes(y = mean_cumsum, colour='Platform participants'), size = 1.5, show.legend = FALSE)+
  geom_point(aes(y = mean_cumsum, colour='Platform participants', shape='Platform participants'), size=3) +
  geom_line(aes(y = mean_interventions, colour='Actions'), size=1.5, show.legend = FALSE) +
  geom_point(aes(y = mean_interventions, colour='Actions', shape='Actions'), size=3) +
  geom_line(aes(y = mean_sales, colour="Adopters"), size=1.5, show.legend = FALSE) +
  geom_point(aes(y = mean_sales, colour='Adopters', shape='Adopters'), size=3) +
  xlab("Quarters") +
  ylab("Cumulative occurences") +
  scale_shape_manual("", values=c("Platform participants" = 16, "Actions" = 17, "Adopters"=15)) +
  scale_colour_manual("",breaks = c("Platform participants", "Actions", "Adopters"),
                      values = c ("#C80000", "#696969", "#4E33FF")) +
  theme_stata(base_size = 15, base_family = "sans", scheme = "s2color") +
  scale_x_continuous(n.breaks=14) +
  geom_vline(xintercept=3, linetype='dashed', size=1.7)
p

方法二:修改图例的显示样式

通过guides()函数覆盖图例的默认样式,把图例中的线条设为无,只保留点:

p <- ggplot(data=cumdf, aes(x=quarters2)) +
  geom_line(aes(y = mean_cumsum, colour='Platform participants'), size = 1.5)+
  geom_point(aes(y = mean_cumsum, colour='Platform participants', shape='Platform participants'), size=3) +
  geom_line(aes(y = mean_interventions, colour='Actions'), size=1.5) +
  geom_point(aes(y = mean_interventions, colour='Actions', shape='Actions'), size=3) +
  geom_line(aes(y = mean_sales, colour="Adopters"), size=1.5) +
  geom_point(aes(y = mean_sales, colour='Adopters', shape='Adopters'), size=3) +
  xlab("Quarters") +
  ylab("Cumulative occurences") +
  scale_shape_manual("", values=c("Platform participants" = 16, "Actions" = 17, "Adopters"=15)) +
  scale_colour_manual("",breaks = c("Platform participants", "Actions", "Adopters"),
                      values = c ("#C80000", "#696969", "#4E33FF")) +
  theme_stata(base_size = 15, base_family = "sans", scheme = "s2color") +
  scale_x_continuous(n.breaks=14) +
  geom_vline(xintercept=3, linetype='dashed', size=1.7) +
  # 覆盖图例样式,隐藏线条
  guides(colour = guide_legend(override.aes = list(linetype = 0)))
p

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 22:01:02