小提琴圖+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畫(huà)組間比較圖,并自己添加比較的P值和比較橫線(xiàn)
# 導(dǎo)入所需的庫(kù)
library(ggplot2)
# 創(chuàng)建一個(gè)示例數(shù)據(jù)集
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))
)
# 設(shè)置組間比較
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), # 手動(dòng)輸入您希望繪制橫線(xiàn)的高度
p_value = c(0.05, 0.01, 0.001) # 手動(dòng)輸入您希望在圖中顯示的顯著性水平
)
# 創(chuàng)建圖形
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()