R实战 | 山脊图(ridgeline plot)
示例数据
library(ggplot2)
library(reshape2)
# 以基因表达矩阵为例
test = matrix(rnorm(200), 20, 10)
test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3
test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2
test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4
colnames(test) = paste("Test", 1:10, sep = "")
rownames(test) = paste("Gene", 1:20, sep = "")
head(test)
# 长宽转换
test_wide <- melt(test, varnames = c("gene","group"),value.name="exp")
head(test_wide)
> head(test)
Test1 Test2 Test3 Test4 Test5 Test6
Gene1 6.862339 -0.09388774 7.025991 0.32898812 6.711819 -0.63220649
Gene2 6.785854 -0.76082397 4.806890 1.64125716 7.331260 0.02801615
Gene3 7.476389 -0.52560553 6.422600 0.08075241 5.347340 0.51520844
Gene4 6.228567 -0.45859833 6.403849 -0.75171274 6.671903 0.85806855
Gene5 5.969722 0.68304298 6.229048 -1.21759698 5.318038 1.09475362
Gene6 6.229172 0.43753681 8.393141 0.90708380 6.444889 0.74859812
> head(test_wide)
gene group exp
1 Gene1 Test1 6.862339
2 Gene2 Test1 6.785854
3 Gene3 Test1 7.476389
4 Gene4 Test1 6.228567
5 Gene5 Test1 5.969722
6 Gene6 Test1 6.229172
山脊图可视化
基础图形
# install.packages("ggridges")
library(ggridges)
ggplot(test_wide, aes(x = exp, y = gene)) +
geom_density_ridges()
![8461988d78bf33529188add53a9cb12f.png](https://i-blog.csdnimg.cn/blog_migrate/7466fae949ed285a89c070b3ff1b2c47.png)
常用参数
ggplot(test_wide, aes(x = exp, y = gene)) +
geom_density_ridges(rel_min_height = 0.005, # 剪尾
scale = 3, # 山脊比例
fill = "white", # 填充
alpha = 0.8, # 透明度
color = 4, # 脊线条颜色
linetype = 1,# 脊线条类型
lwd = 1 ) # 脊线条粗细
![2f8771f5b02d868fdb63d4b0088228cc.png](https://i-blog.csdnimg.cn/blog_migrate/0c99c0b7f022317a3a583975133a90c1.png)
分组
ggplot(test_wide, aes(x = exp, y = gene, fill = gene )) +
geom_density_ridges()
![3196c41aacb37dee269c9a58b1bf3a1c.png](https://i-blog.csdnimg.cn/blog_migrate/cbd920d8c8114a664a6aa95a02bcf26f.png)
渐变
ggplot(test_wide, aes(x = exp, y = gene, fill = stat(x) )) +
geom_density_ridges_gradient() +
scale_fill_viridis_c(name = "Exp", option = "C")+
coord_cartesian(clip = "off") + # To avoid cut off
theme_minimal()
![198318f4da1774a8d9efd3f73bb03508.png](https://i-blog.csdnimg.cn/blog_migrate/d7a96e5bcc7634cf2088e0858b098df9.png)
添加分位线
# 四分位线
ggplot(test_wide, aes(x = exp, y = gene, fill = stat(quantile))) +
stat_density_ridges(quantile_lines = FALSE,
calc_ecdf = TRUE,
geom = "density_ridges_gradient") +
scale_fill_brewer(name = "")
![22387979445ca94f92031bb8ecea3f18.png](https://i-blog.csdnimg.cn/blog_migrate/b33bedf2cd36285dbd77826039f68905.png)
# 双尾分布线
ggplot(test_wide, aes(x = exp, y = gene, fill = stat(quantile))) +
stat_density_ridges(quantile_lines = TRUE,
calc_ecdf = TRUE,
geom = "density_ridges_gradient",
quantiles = c(0.05, 0.95)) +
scale_fill_manual(name = "Prob.", values = c("#E2FFF2", "white", "#B0E0E6"),
labels = c("(0, 5%]", "(5%, 95%]", "(95%, 1]"))
![c0f6ec50a4cd4fa00627d0c99ba93550.png](https://i-blog.csdnimg.cn/blog_migrate/cc24d75824940e3d5ecd7c88ed4a243a.png)
更多
Ridgeline plot in ggplot2 with ggridges | R CHARTS (r-charts.com)
参考
Ridgeline plot in ggplot2 with ggridges | R CHARTS (r-charts.com)