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

如何修改ggplot2搭配ggridges绘制图表的图例为线型样式?

ggridges密度图修改图例为线型标识

问题描述

我正在尝试修改当前所绘制图表的图例项,所使用的代码如下:

library(ggplot2)
library(HDInterval)
library(ggridges)

df <- data.frame(
  density = c(rgamma(400, 2, 10), rgamma(400, 2.25, 9), rgamma(400, 5, 7)),
  source = rep(c("source_1", "source_2", "source_3"),
               each = 400))

ggplot(df, aes(x = density, color = source, linetype = source,
               fill = after_stat(ifelse(quantile == 2, NA, color)))) +
  geom_density_ridges_gradient(aes(y = 0), quantile_lines = TRUE, size=1.2, 
                               quantile_fun = hdi, vline_linetype = 0, scale = 1) +
  labs(y = "Density", x = "Contribution") + 
  
  scale_linetype_cyclical(name = "Source", values = c("solid", "dotted", "longdash"),
                          labels = c("source1", "source2", "source3"),
                          guide = "legend") +
  
  scale_fill_cyclical(name = "Source", values = c("#31a354", "#2c7fb8", "#d95f0e"),
                      labels = c("source1", "source2", "source3"),
                      guide = "none", na.value = "transparent") +
  
  scale_color_cyclical(name = "Source", values = c("#31a354", "#2c7fb8", "#d95f0e"),
                       labels = c("source1", "source2", "source3"),
                       guide = "none") +
  
  ylim(0, 8) + xlim(0, 1) +
  theme(legend.text = element_text(size=16),
        legend.title=element_text(size=18),
        legend.key.height= unit(1, 'cm'),
        legend.key.width= unit(1, 'cm')) +
  guides(color = guide_legend(override.aes = list(fill = "white")))+
  theme(axis.text.y  = element_text(size = 12, vjust = 0.5),
        axis.text.x  = element_text(size = 12, vjust = 0.5),  
        axis.title.x = element_text(size = 14),
        axis.title.y = element_text(size = 14))

我希望图例中展示对应的线型标识,而非默认的填充方块样式。
示例效果截图
我已尝试深入研究ggridges包的使用逻辑,但发现该包在部分功能设置上较为特殊、调整灵活性不足。

解决方案

问题来自两处设置冲突:

  • 你将color美学的图例全局设置为guide = "none",导致后续针对color图例的override.aes修改完全不生效
  • 仅保留的linetype图例默认会继承geom_density_ridges_gradient的填充属性,自动渲染为填充方块而非线条

只需要做两处调整即可实现需求:

  1. 移除scale_color_cyclical中的guide = "none"参数,让color和linetype的同名图例自动合并
  2. 替换原有无效的guides设置,通过override.aes强制图例项去掉填充、仅保留对应颜色和线型的线条

修改后的完整可运行代码:

library(ggplot2)
library(HDInterval)
library(ggridges)

df <- data.frame(
  density = c(rgamma(400, 2, 10), rgamma(400, 2.25, 9), rgamma(400, 5, 7)),
  source = rep(c("source_1", "source_2", "source_3"),
               each = 400))

ggplot(df, aes(x = density, color = source, linetype = source,
               fill = after_stat(ifelse(quantile == 2, NA, color)))) +
  geom_density_ridges_gradient(aes(y = 0), quantile_lines = TRUE, size=1.2, 
                               quantile_fun = hdi, vline_linetype = 0, scale = 1) +
  labs(y = "Density", x = "Contribution") + 
  
  scale_linetype_cyclical(name = "Source", values = c("solid", "dotted", "longdash"),
                          labels = c("source1", "source2", "source3"),
                          guide = "legend") +
  
  scale_fill_cyclical(name = "Source", values = c("#31a354", "#2c7fb8", "#d95f0e"),
                      labels = c("source1", "source2", "source3"),
                      guide = "none", na.value = "transparent") +
  
  scale_color_cyclical(name = "Source", values = c("#31a354", "#2c7fb8", "#d95f0e"),
                       labels = c("source1", "source2", "source3")) +
  
  ylim(0, 8) + xlim(0, 1) +
  theme(legend.text = element_text(size=16),
        legend.title=element_text(size=18),
        legend.key.height= unit(1, 'cm'),
        legend.key.width= unit(1, 'cm')) +
  guides(
    linetype = guide_legend(
      override.aes = list(
        fill = NA,
        size = 1.2
      )
    )
  ) +
  theme(axis.text.y  = element_text(size = 12, vjust = 0.5),
        axis.text.x  = element_text(size = 12, vjust = 0.5),  
        axis.title.x = element_text(size = 14),
        axis.title.y = element_text(size = 14))

运行后图例就会显示为和图中线条完全一致的、带对应颜色和线型的水平标识,不会再出现填充方块。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 04:57:11