ggridges包—峰峦图详细介绍

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

本节使用的峰峦图也可以很好地展示瀑布图的数据信息。它们对于可视化随时间或空间分布的变化非常有用。本节主要使用ggridges包中的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)

在这里插入图片描述

2.绘图教程

2.1基础版本

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

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

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

注意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")

在这里插入图片描述

使用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")

在这里插入图片描述

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

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

在这里插入图片描述

最后,当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)

在这里插入图片描述

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

在这里插入图片描述

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

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

在这里插入图片描述

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

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

在这里插入图片描述

可以使用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))

在这里插入图片描述

如果你还想再加入一个变量进行可视化,可以在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))

在这里插入图片描述

另外一种有趣的可视化是通过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)
  )

在这里插入图片描述

其他资料

对于该包的其他有趣函数与可视化可参考以下资料:

  • 4
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
使用d3实现可视化峰峦相比使用Matplotlib库,需要更多的代码和工作量,但是可以实现更加灵活的可视化效果。以下是一个使用d3绘制峰峦的示例代码: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Mountain Range</title> <script src="https://d3js.org/d3.v5.min.js"></script> <style> path { fill: none; stroke: #000; stroke-width: 1.5px; } </style> </head> <body> <svg width="960" height="500"></svg> <script> // 生成随机高程数据 var data = d3.range(100).map(function(d) { return [Math.random() * 10 - 5, Math.random() * 10 - 5, Math.random() * 3]; }); // 创建投影 var projection = d3.geoIdentity() .reflectY(true) .fitSize([960, 500], { type: "Sphere" }); // 创建路径生成器 var path = d3.geoPath() .projection(projection); // 创建颜色插值器 var color = d3.scaleLinear() .domain([0, 2]) .range(["#fff", "#69b3a2"]); // 创建SVG元素 var svg = d3.select("svg"); // 绘制山峦 svg.selectAll("path") .data(data) .enter().append("path") .attr("d", function(d) { var x = d[0]; var y = d[1]; var h = d[2]; return path({ type: "LineString", coordinates: [[x, y], [x, y - h]] }); }) .attr("stroke", function(d) { return color(d[2]); }); </script> </body> </html> ``` 以上代码通过d3生成随机高程数据,然后使用投影和路径生成器绘制山峦,并使用颜色插值器给不同高度的山峰着色。可以在浏览器中查看结果。如果需要更复杂的定制,可以在以上示例代码的基础上进行修改。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值