ggridges包—峰峦图详细介绍

简介: 上次可视化系列说了瀑布图(可跳转)。它可以用于展示拥有相同的X轴变量数据(如相同的时间序列)、不同的Y轴离散型变量(如不同的类别变量)和Z轴数值变量。本节使用的峰峦图也可以很好地展示瀑布图的数据信息。它们对于可视化随时间或空间分布的变化非常有用。本节主要使用ggridges包[1]中的geom_density_ridges()进行绘制峰峦图。

简介


上次可视化系列说了瀑布图(可跳转)。它可以用于展示拥有相同的X轴变量数据(如相同的时间序列)、不同的Y轴离散型变量(如不同的类别变量)和Z轴数值变量。

本节使用的峰峦图也可以很好地展示瀑布图的数据信息。它们对于可视化随时间或空间分布的变化非常有用。本节主要使用ggridges包[1]中的geom_density_ridges()进行绘制峰峦图。详细介绍如下:


1. 数据结构

这里使用base包中的diamonds数据集做例子。

# library
library(ggridges) # Ridgeline Plots in 'ggplot2', CRAN v0.5.2
library(ggplot2) # Create Elegant Data Visualisations Using the Grammar of Graphics, CRAN v3.3.2 
head(diamonds)

TUULVI$R2ZWS5)@311MFG2G.png


2.绘图教程

2.1 基础版本

使用price作为x轴, cut为y轴,fill参数也是设定为cut。geom_density_ridges()内部全部使用默认参数,并使用了gridges包中theme_ridges()主题。

ggplot(diamonds, aes(x = price, y = cut, fill = cut)) +
  geom_density_ridges() +
  theme_ridges() + 
  theme(legend.position = "none")

image.gifJOC[KONEPQH]VLJAV[GR{17.png


2.2 形状变化

如果不想绘制密度图,则可以使用stat="binline", bins=20绘制柱形图,其中bins=20表示每格格子大小。为了防止上下图片重叠,这里使用了透明度参数:alpha=0.7。

ggplot(diamonds, aes(x = price, y = cut, fill = cut)) +
  geom_density_ridges(alpha=0.7, stat="binline", bins=20) +
  theme_ridges() + 
  theme(legend.position = "none")

%H39ISI(UJ)RCPN}FU5~RHO.png


2.3 根据第三变量进行分面

可以使用facet_wrap()进行分面处理。

ggplot(diamonds, aes(x = price, y = cut,fill = cut)) +
  geom_density_ridges(alpha=0.7) +
  facet_wrap(~color) + 
  theme_ridges() + 
  theme(legend.position = "none")

L172}O2[)K0(A9B3ZJP~DEP.png


2.4 加入统计量

设置选项quantile_lines = TRUE,可以使stat_density_ridges计算指示分位数的线的位置。默认情况下,绘制了三行,分别对应于第一,第二和第三四分位数:

ggplot(diamonds, aes(x = price, y = cut,fill = cut)) +
  geom_density_ridges(alpha=0.7,quantile_lines = TRUE) +
  theme_ridges() + 
  theme(legend.position = "none")

image.gif1MEH0FKXFR~S9BW%{FU(7D8.png

注意:quantiles=2意味着在两个分位数之间的边界上有一条线(即中位数)。

我们还可以通过切点而不是数字来指定分位数。例如,我们可以指出2.5%和97.5%(quantiles = c(0.025, 0.975))。

ggplot(diamonds, aes(x = price, y = cut,fill = cut)) +
  geom_density_ridges(alpha=0.7,quantile_lines = TRUE,quantiles = c(0.025, 0.975)) +
  theme_ridges() + 
  theme(legend.position = "none")

image.gif7QH40NC{T@N$SIX{1W[0K62.png


使用stat_density_ridges,计算stat(quantile),通过分位数进行着色。注意,仅当calc_ecdf = TRUE时才能计算。

ggplot(diamonds, aes(x = price, y = cut,fill = factor(stat(quantile)))) +
  stat_density_ridges(
    geom = "density_ridges_gradient", 
    calc_ecdf = TRUE,
    quantiles = 4, quantile_lines = TRUE) +
  theme_ridges() +
  scale_fill_viridis_d(name = "Quartiles")

`AK8MAP(FJ0[1FC[4_IKHKD.png


我们可以使用相同的方法来突出分布的尾部。

ggplot(diamonds, aes(x = price, y = cut,fill = factor(stat(quantile)))) +
  stat_density_ridges(
    geom = "density_ridges_gradient", 
    calc_ecdf = TRUE,
    quantiles = c(0.025, 0.975)) +
  theme_ridges() +
    scale_fill_manual(
    name = "Probability", values = c("#FF0000A0", "#A0A0A0A0", "#0000FFA0"),
    labels = c("(0, 0.025]", "(0.025, 0.975]", "(0.975, 1]")
  )

3}8(RB7L{O[CO64M{%HJC}T.pngimage.gif

最后,当calc_ecdf = TRUE时,我们还可以计算stat(ecdf),它表示该分布的经验累积密度函数。我们将其概率直接映射到颜色上。

ggplot(diamonds, aes(x = price, y = cut,fill = 0.5 - abs(0.5 - stat(ecdf)))) +
  stat_density_ridges(geom = "density_ridges_gradient", calc_ecdf = TRUE) +
  scale_fill_viridis_c(name = "Tail probability", direction = -1)

P64]H[NSN_5FL%(]UJ{OKKL.png


2.5 加入抖动点

stat_density_ridges()还提供了可视化生成分布的原始数据点的选项。可以通过设置jittered_points = TRUE实现。为了简单起见,我们这里使用iris数据集。

ggplot(iris, aes(x = Sepal.Length, y = Species)) +
  geom_density_ridges(jittered_points = TRUE)+
  theme_ridges() + 
  theme(legend.position = "none")

}LO{[X1AUM@UT8YQ)4Q2I[1.png


当然可以将其放在密度函数的下方,通过使用position = "raincloud"参数。

ggplot(iris, aes(x = Sepal.Length, y = Species)) +
  geom_density_ridges(
    jittered_points = TRUE, position = "raincloud",
    alpha = 0.7, scale = 0.9
  )

W39(`VN($0H1GBWVDG9X020.png


我们还可以模拟地毯形式:

ggplot(iris, aes(x = Sepal.Length, y = Species)) +
  geom_density_ridges(
    jittered_points = TRUE,
    position = position_points_jitter(width = 0.05, height = 0),
    point_shape = '|', point_size = 3, point_alpha = 1, alpha = 0.7,
  )

_C@U`EY]G(N%H5[F]ZEFAN8.pngimage.gif


可以使用ggridges提供的特殊比例来设置抖动点的样式。scale_discrete_manual()可用于制作具有任意形状和比例的图形。

ggplot(iris, aes(x = Sepal.Length, y = Species, fill = Species)) +
  geom_density_ridges(
    aes(point_color = Species, point_fill = Species, point_shape = Species),
    alpha = .2, point_alpha = 1, jittered_points = TRUE
  ) +
  scale_point_color_hue(l = 40) +
  scale_discrete_manual(aesthetics = "point_shape", values = c(21, 22, 23))

DY~KM$M@IZTOG237BUMB}7Q.png

如果你还想再加入一个变量进行可视化,可以在geom_density_ridges()加入。

ggplot(iris, aes(x = Sepal.Length, y = Species, fill = Species)) +
  geom_density_ridges(
    aes(point_shape = Species, point_fill = Species, point_size = Petal.Length), 
    alpha = .2, point_alpha = 1, jittered_points = TRUE
  ) +
  scale_point_color_hue(l = 40) + scale_point_size_continuous(range = c(0.5, 4)) +
  scale_discrete_manual(aesthetics = "point_shape", values = c(21, 22, 23))

VP8B}MI6_W[44JYZQ$)`1QI.png


另外一种有趣的可视化是通过vline_xxx构造以下图形。

ggplot(iris, aes(x = Sepal.Length, y = Species)) +
  geom_density_ridges(
    jittered_points = TRUE, quantile_lines = TRUE, scale = 0.9, alpha = 0.7,
    vline_size = 1, vline_color = "red",
    point_size = 0.4, point_alpha = 1,
    position = position_raincloud(adjust_vlines = TRUE)
  )

NZ`U07XGG{MUO4Y6[%]]K6H.png

目录
相关文章
|
存储 数据可视化
使用 plotly 绘制旭日图
使用 plotly 绘制旭日图
1270 0
|
数据挖掘
R实战 | 环状热图(circos)
R实战 | 环状热图(circos)
1333 0
|
9天前
|
域名解析 人工智能 Linux
|
20天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
17774 63
|
17天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
3天前
|
人工智能 安全 网络安全
|
7天前
|
云安全 数据采集 人工智能
|
8天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1412 5
|
15天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2467 8
|
20天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
3994 22

热门文章

最新文章