介绍
单因素多个levels数据在做方差分析后,需要进一步做如t检验等置后检验,判断两两组间差异。
加载R包和准备数据
{r}
# Load required R packages
library(tidyverse)
library(rstatix)
library(ggpubr)
data("PlantGrowth")
set.seed(1234)
PlantGrowth %>% sample_n_by(group, size = 1)
查看统计量
{r}
PlantGrowth %>%
group_by(group) %>%
get_summary_stats(weight, type = "mean_sd")
使用ANOVA test比较多组的平均值
{r}
res.aov <- PlantGrowth %>% anova_test(weight ~ group)
res.aov
组间t检验
{r}
pwc <- PlantGrowth %>%
pairwise_t_test(weight ~ group, p.adjust.method = "bonferroni")
pwc
p值的坐标轴参数
{r}
pwc <- pwc %>% add_xy_position(x = "group")
pwc
可视化
{r}
ggboxplot(PlantGrowth, x = "group", y = "weight") +
stat_pvalue_manual(pwc, label = "p.adj", tip.length = 0, step.increase = 0.1) +
labs(
subtitle = get_test_label(res.aov, detailed = TRUE),
caption = get_pwc_label(pwc))