R可视化学习(5) -- 脊线图

简介: Ridgeline 图(脊线图),(有时称为Joyplot)可以同时显示几个组的数值分布情况,分布可以使用直方图或密度图来表示,它们都与相同的水平尺度对齐,并略有重叠。常常被用来可视化随时间或空间变化的多个分布/直方图变化。

安装包ggridges


# Cran安装
install.packages("ggridges")
# github上安装
library(devtools)
install_github("clauswilke/ggridges")

绘图

基础图形

主要利用geom_density_ridges函数


# library
library(ggridges)
library(ggplot2)
# Diamonds dataset is provided by R natively
#head(diamonds)
# basic example
ggplot(diamonds, aes(x = price, y = cut, fill = cut)) +
  geom_density_ridges(alpha = 0.5) +
  theme_ridges() + 
  theme(legend.position = "none")

d5a1e9d118e74c7a08144da9da2926d.png

基础图

另外,有时候我们也可分面展示不同范围的脊线图


diamonds$label= ifelse(diamonds$price >= 10000,'great','bad')
ggplot(diamonds, aes(x = price, y = cut, fill = cut)) +
  geom_density_ridges() +
  theme_ridges() + 
  theme(legend.position = "none")+
  facet_wrap(~label)

8586d24888ac2f840c794b8be95ed7a.png

不同范围分面展示

增加统计信息

这里利用到stat_density_ridges函数,可以在图形上增加代表统计信息的线段, 比如增加上 、下四分位数线(Q1 Q3)和中位数线 Q2 。


ggplot(diamonds, aes(x = price, y = cut, fill = cut)) +
  stat_density_ridges(quantile_lines = TRUE)+
  theme_ridges() + 
  theme(legend.position = "none")
# 当然,我们也可自定义分位数线 比如2.5% 和 60%的线
ggplot(diamonds, aes(x = price, y = cut, fill = cut)) +
  stat_density_ridges(quantile_lines = TRUE, quantiles = c(0.025, 0.600), alpha = 0.7)+
  theme_ridges() + 
  theme(legend.position = "none")

65a0e62b61f2f75ef9fb469de2f31d7.png

将上述代码中fill的映射值改为factor(stat(quantile)),可以将不同颜色映射在不同的分区,并自定义颜色,如:


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_manual(
    name = "Probability", values = c("#FF0000A0", "#A0A0A0A0", "#0000FFA0",'gold'),
    labels = c("(0, 0.025]", "(0.025, 0.050]","(0.050,0.075]", "(0.075, 1]")
  )

d01a18ee4dbdb9cd37bc82033445f79.png

按照范围分组

散点图显示

参考了,代码中设置jittered_points = TRUE来实现散点的绘制,无论是在stat_density_ridges还是在geom_density_ridges

点可以有以下几种选择方式:

  • position = 'sina',在基线和山脊线之间的山脊线图中随机分布点。 这是默认选项。
  • position= 'jitter', 随机抖动山脊线图中的点。 点随机上下移动和/或左右移动。
  • position = 'raincloud': 在山脊线图下方创建随机抖动点的云。


# 增加散点图
A <- ggplot(iris, aes(x = Sepal.Length, y = Species)) +
  geom_density_ridges(jittered_points = TRUE)
# 控制点位置
# position = "raincloud"
B <- ggplot(iris, aes(x = Sepal.Length, y = Species)) +
  geom_density_ridges(
    jittered_points = TRUE, position = "raincloud",
    alpha = 0.7, scale = 0.9
  )
# position = "points_jitter"
C <- ggplot(iris, aes(x = Sepal.Length, y = Species)) +
  geom_density_ridges(
    jittered_points = TRUE, position = "points_jitter",
    alpha = 0.7, scale = 0.9
  )
# 增加边际线
D <- 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,
  )
library(patchwork)
(A + B)/(C + D)+ plot_annotation(tag_levels = 'A')

69e98b81828318f38c1543d31c4105b.png

自定义散点的样式、颜色


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))

b0b2195d2f1e54c4ba817267be74c79.png

自定义

其它(渐变色)

除了上述比较单一的色彩,还可使用此包中geom_density_ridges_gradient函数添加渐变色,拿Example数据为例,可以通过?geom_density_ridges_gradient进行查看更多的控制参数,


# library
library(ggridges)
library(ggplot2)
library(viridis)
library(hrbrthemes)
# Plot
ggplot(lincoln_weather, aes(x = `Mean Temperature [F]`, y = `Month`, fill = ..x..)) +
  geom_density_ridges_gradient(scale = 3, rel_min_height = 0.01) +
  scale_fill_viridis(name = "Temp. [F]", option = "C") +
  labs(title = 'Temperatures in Lincoln NE in 2016') +
  theme_ipsum() +
  theme(
    legend.position="none",
    panel.spacing = unit(0.1, "lines"),
    strip.text.x = element_text(size = 8)
  )
## geom_density_ridges_gradient参数很多,可以?详细查看
 geom_density_ridges_gradient(
  mapping = NULL,
  data = NULL,
  stat = "density_ridges",
  position = "points_sina",
  panel_scaling = TRUE,
  na.rm = TRUE,
  gradient_lwd = 0.5,
  show.legend = NA,
  inherit.aes = TRUE,
  ...
)

85d26bf1b3eecd0b51ec69af9ca1ef3.png

渐变色

直方图显示

除了上述的密度图,只需添加上stat=binline参数即可。


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

72985016f725045b80783accef6eac6.png

直方图

相关文章
|
对象存储 容器 云计算
标准流程描述语言 WDL 阿里云最佳实践
WDL 作为全球基因组与健康联盟 (Global Alliance for Genomics and Health)支持的工作流描述语言,已经被越来越多的客户所采用。通过阿里云的 Cromwell 方案,用户可以本地开发测试WDL流程,再使用云计算强大的计算能力,来完成基因组学数据分析工作。
11260 3
|
机器学习/深度学习 人工智能 算法
黑盒模型事后归因解析:SHAP 方法
近年来人工智能的浪潮越来越汹涌,以神经网络、集成模型为代表的机器学习模型在数据挖掘领域中发挥着不可替代的作用。在追求模型高精度的道路上,工业界和学术界也十分关注模型的可解释性,期待从复杂模型中得到更直观的理解。
|
数据可视化
R语言多图合成:优雅地在一个画布上展示多个图形
【8月更文挑战第30天】R语言提供了多种方法来实现多图合成,从基础的`par()`函数到高级的`gridExtra`、`ggplot2`和`cowplot`包,每种方法都有其独特的优势和应用场景。通过掌握这些技术,你可以根据实际需求灵活地组合图形,从而更高效地展示和解读数据。希望本文能为你提供一些有益的参考和启示。
|
JavaScript
Vue学习之--------事件的基本使用、事件修饰符、键盘事件(2022/7/7)
这篇文章是关于Vue事件处理的学习指南,包括事件的基本使用、事件修饰符和键盘事件。文中首先介绍了事件的基本使用方法,包括使用`v-on:xxx`或`@xxx`绑定事件,以及在`methods`对象中配置回调函数。然后,文章通过代码实例和测试效果,展示了如何使用事件修饰符来增强事件处理的功能,例如阻止默认行为、阻止事件冒泡等。最后,文章还介绍了Vue中的键盘事件处理,包括常用的按键别名和系统修饰键的使用,并通过代码示例展示了如何检测特定按键的按下。整篇文章通过详细的代码实例和清晰的测试效果截图,帮助读者理解和掌握Vue中事件处理的相关知识点。
Vue学习之--------事件的基本使用、事件修饰符、键盘事件(2022/7/7)
|
人工智能 搜索推荐 数据挖掘
让 AI 回答更精准 ◎ 来学学这些Prompt入门小技巧
这篇文章介绍了如何通过有效的提示词来提升向AI提问的质量,使其回答更加精准,并提供了实用的指导原则和案例分析。
让 AI 回答更精准 ◎ 来学学这些Prompt入门小技巧
|
资源调度 数据挖掘
R语言回归分析:线性回归模型的构建与评估
【8月更文挑战第31天】线性回归模型是统计分析中一种重要且实用的工具,能够帮助我们理解和预测自变量与因变量之间的线性关系。在R语言中,我们可以轻松地构建和评估线性回归模型,从而对数据背后的关系进行深入的探索和分析。
|
人工智能 安全 搜索推荐
1.8B参数,阿里云首个联合DNA、RNA、蛋白质的生物大模型,涵盖16.9W物种
【6月更文挑战第14天】阿里云发布首个集成DNA、RNA和蛋白质数据的生物大模型LucaOne,拥有1.8B参数,涉及16.9万物种。LucaOne通过few-shot learning技术和streamlined downstream architecture实现多生物语言统一处理,提升生物系统理解与分析能力。该模型将加速生物信息学研究,推动生物医学应用,但同时也引发生物数据安全、预测偏差及AI伦理法律等问题的讨论。[论文链接](https://www.biorxiv.org/content/10.1101/2024.05.10.592927v1)
810 3
|
数据可视化
R语言极值推断:广义帕累托分布GPD使用极大似然估计、轮廓似然估计、Delta法
R语言极值推断:广义帕累托分布GPD使用极大似然估计、轮廓似然估计、Delta法
|
机器学习/深度学习 数据可视化 Python
Scikit-Learn 中级教程——学习曲线
Scikit-Learn 中级教程——学习曲线
967 3
|
存储 数据处理 计算机视觉
【小白必看】Python词云生成器详细解析及代码实现
【小白必看】Python词云生成器详细解析及代码实现
915 1