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()
基础图形
常用参数
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 ) # 脊线条粗细
常用参数
分组
ggplot(test_wide, aes(x = exp, y = gene, fill = gene )) + geom_density_ridges()
渐变
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()
添加分位线
# 四分位线 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 = "")
# 双尾分布线 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]"))
更多
Ridgeline plot in ggplot2 with ggridges | R CHARTS (r-charts.com)
参考
Ridgeline plot in ggplot2 with ggridges | R CHARTS (r-charts.com)