小提琴图+boxplot
ggplot(subtype.meta.dat, aes(Subtype, M2.score, fill=Subtype)) + geom_violin(width=0.8, scale='width') + geom_boxplot(width=0.2) +
# stat_compare_means(comparisons=list(c('W4','Cont'),c('W4','W14'),c('W4','W24')), label.y = c(0.45, 0.6, 0.75),size=5) +
labs(x='',y='M2 module score') +
theme(panel.background=element_blank(), panel.border=element_blank(), axis.line=element_line(size=0.5, colour='black'), axis.ticks=element_line(size=0.5, colour='black'), axis.text=element_text(size=16, colour='black'), axis.title=element_text(size=16, colour='black'), legend.position='none', axis.text.x=element_text(angle=45, hjust=1, vjust=1))
ggplot画组间比较图,并自己添加比较的P值和比较横线
# 导入所需的库
library(ggplot2)
# 创建一个示例数据集
data <- data.frame(
group = rep(c("A", "B", "C"), each = 30),
value = c(rnorm(30, mean = 10, sd = 2),
rnorm(30, mean = 15, sd = 2),
rnorm(30, mean = 12, sd = 2))
)
# 设置组间比较
comparison_data <- data.frame(
group1 = factor(c("A", "A", "B"), levels=c("A", "B", "C")),
group2 = factor(c("B", "C", "C"), levels=c("A", "B", "C")),
y = c(20, 22, 24), # 手动输入您希望绘制横线的高度
p_value = c(0.05, 0.01, 0.001) # 手动输入您希望在图中显示的显著性水平
)
# 创建图形
ggplot() +
geom_boxplot(data = data, aes(x = group, y = value, fill = group)) +
geom_segment(data = comparison_data, aes(x = as.numeric(group1), xend = as.numeric(group2), y = y, yend = y), color = "black", size = 1) +
geom_text(data = comparison_data, aes(x = (as.numeric(group1) + as.numeric(group2)) / 2, y = y + 0.5, label = paste0("p = ", p_value)), vjust = -0.5) +
theme_minimal()