一般比较简单的boxplot是这样的,2个对比组,一个P值,这种图很简单。
,哈哈哈,这个时候该我出马了!
如果你的数据是宽格式呢,首先需要转成长格式(用reshape2::melt转换),比如:
之后利用ggpubr的compare_means函数去计算出统计检验结果,compare_formula代表的是group~value。
res = compare_means(compare_formula, dataset, method = 't.test')
后面基于ggplot2开始构建绘图主体:
pic <- ggplot(dataset, aes_string(axis_x, axis_y)) +
stat_boxplot(geom = 'errorbar', width = 0.3, color = "#404040", size = line_size) +
geom_boxplot(aes_string(color = axis_x, fill = axis_x),
fatten = 1, alpha = 0.5, color = "#404040",
outlier.shape = NA, size = line_size) +
geom_jitter(aes_string(color = axis_x, fill = axis_x), size = pt_size) +
labs(title = plot_title, y = y_label) + theme_bw() + theme +
guides(color = 'none', fill = 'none') +
scale_color_manual(values = colors) +
scale_fill_manual(values = colors) +
scale_y_continuous(expand = c(0.1, 0))
选择P<0.05的统计检验结果,并添加到绘图主体中。
stat_res_tmp <- res %>% filter(p.signif != 'ns')
pic <- pic + geom_bracket(data = stat_res_tmp, aes(xmin = group1, xmax = group2, label = p.signif),
y.position = stat_res_tmp$y.position, tip.length = 0.01, vjust = 0.5,
step.increase = 0.1, size = 1, label.size = label_size)
最终画出的图像案例如下(实现了把P<0.05的两两比较组加上星号*):
都看到这里了,还不快点赞+关注么!!!
网友评论