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")
基礎圖另外,有時候我們也可分面展示不同範圍的脊線圖
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)不同範圍分面展示增加統計信息
這裡利用到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")將上述代碼中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]")
)按照範圍分組散點圖顯示
參考了[1],代碼中設置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')自定義散點的樣式、顏色
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_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,
...
)漸變色直方圖顯示
除了上述的密度圖,只需添加上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")
直方圖參考資料[1]ELEGANT VISUALIZATION OF DENSITY DISTRIBUTION IN R USING RIDGELINE: https://www.datanovia.com/en/blog/elegant-visualization-of-density-distribution-in-r-using-ridgeline/