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

直方图

相关文章
|
编解码 算法 计算机视觉
【MATLAB】 小波分解信号分解+FFT傅里叶频谱变换组合算法
【MATLAB】 小波分解信号分解+FFT傅里叶频谱变换组合算法
747 0
|
对象存储 容器 云计算
标准流程描述语言 WDL 阿里云最佳实践
WDL 作为全球基因组与健康联盟 (Global Alliance for Genomics and Health)支持的工作流描述语言,已经被越来越多的客户所采用。通过阿里云的 Cromwell 方案,用户可以本地开发测试WDL流程,再使用云计算强大的计算能力,来完成基因组学数据分析工作。
12271 3
|
安全 关系型数据库 Linux
什么叫网站国产化改造?哪些CMS符合国产化标准?
国产化需求在未来的互联网话题上是只会越来越多、越来越重要的,这个国产化改造不单单针对网站项目上,还会逐步涉及到各大软件和系统上,所以,相关的单位提前了解国产化的改造标准和需求,到传达的时候、不会显得出手无策。
822 5
|
资源调度 数据挖掘
R语言回归分析:线性回归模型的构建与评估
【8月更文挑战第31天】线性回归模型是统计分析中一种重要且实用的工具,能够帮助我们理解和预测自变量与因变量之间的线性关系。在R语言中,我们可以轻松地构建和评估线性回归模型,从而对数据背后的关系进行深入的探索和分析。
|
机器学习/深度学习 数据可视化 Python
Scikit-Learn 中级教程——学习曲线
Scikit-Learn 中级教程——学习曲线
1101 3
1天搞定SpringBoot+Vue全栈开发 (9)JWT跨域认证
1天搞定SpringBoot+Vue全栈开发 (9)JWT跨域认证
|
存储 数据处理 计算机视觉
【小白必看】Python词云生成器详细解析及代码实现
【小白必看】Python词云生成器详细解析及代码实现
1314 1
|
网络协议 Unix Linux
【网络安全 | 信息收集】操作系统判定及端口扫描(全网最详析)
【网络安全 | 信息收集】操作系统判定及端口扫描(全网最详析)
599 0
|
C++
【SPSS】两独立样本T检验分析详细操作教程(附案例实战)
【SPSS】两独立样本T检验分析详细操作教程(附案例实战)
2692 0