数据和代码获取:请查看主页个人信息!!!
大家好,今天我将介绍如何使用R语言ggridges包绘制峰峦图。
近期在做数据分析项目时,再一次遇到了图片组合繁琐的问题;分析主要是构图和布局问题,差异分析图有时太占空间,偶然看到Nature杂志上面的一张图,效果如下:
图片来源:https://doi.org/10.1038/s41586-023-06466-x
该图的优点:占用空间小,直观明了颜值高~今天我们使用微生物组数据进行模仿展示:
Step1:数据载入
rm(list=ls())
pacman::p_load(tidyverse,reshape2,ggridges,ggpubr)
data <- read.csv('data.csv')
Step2:箱线图差异可视化
my_comparisons <- list(c("IW", "CW"), c("IW", "TW"), c('CW', 'TW'))
df <-
data %>%
melt(id.vars = colnames(.)[1:4])
df$Group <- factor(df$Group, levels = c('IW', "CW", "TW"), ordered = T)
ggplot(df, aes(Group, value, fill = Group)) +
geom_boxplot(outlier.size = 0.5, size = 0.6, width = 0.6, color = 'black') +
geom_jitter( position=position_jitter(0.1), size=.5)+
facet_wrap(~ variable, scales = 'free_y', nrow = 2) +
theme_classic(base_size = 10) +
xlab('') +
ylab('') +
ggsci::scale_fill_npg() +
theme(strip.background = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
strip.text.x = element_text(size=6),
strip.text.y = element_text(size=6, face="bold")) +
stat_compare_means(comparisons = my_comparisons,
size = 2.5,
method = "wilcox.test",paired = F,
label = "p.signif",tip.length = 0, vjust = 0.2)
ggsave('pic1.png', width = 7, height = 4)
Step3:ggridges包简单示例
# ggridges
ggplot(iris, aes(x = Sepal.Length, y = Species, fill = Species)) +
geom_density_ridges(quantile_lines=TRUE,
quantile_fun=function(x,...)median(x),
color = 'black')
Step4:实战
ggplot(df, aes(x = log10(value+1), y = Group, fill = Group)) +
geom_density_ridges(quantile_lines=TRUE,
size = 0.2,
quantile_fun=function(x,...)median(x),
color = 'black') +
stat_compare_means(comparisons = my_comparisons,
hide.ns = T,
size = 1.5,
method = "wilcox.test",paired = F,
label = "p.signif",tip.length = 0, vjust = 0.4) +
ylab('') +
ggsci::scale_fill_jama() +
facet_grid(vars(variable), scales = "free") +
theme_classic(base_size = 5) +
theme(axis.ticks.y = element_blank(),
strip.background = element_blank(),
strip.text.x = element_text(size=3),
strip.text.y = element_text(size=3, face="bold"),
legend.position = 'top',
axis.text.y = element_blank(),
legend.key.size = unit(5, "pt"))
ggsave('pic2.png', width = 1, height = 3)