stat_compare_means可以同时展示两种自变量的结果,一种方式是使用“~”符号,如下所示:

library(ggpubr)

# 创建一个数据集
data <- data.frame(
  group1 = rep(c("A", "B"), each = 10),
  group2 = rep(c("X", "Y"), 10),
  value = rnorm(20)
)

# 使用ggboxplot函数绘制箱线图,同时使用stat_compare_means函数展示两个自变量之间的差异
ggboxplot(data, x = "group1", y = "value", color = "group2") +
  stat_compare_means(aes(x = group1, y = value, group = group2),
                     method = "t.test", label = "p.format")

这段代码将会创建一个箱线图,其中x轴是group1,y轴是value,颜色编码为group2。stat_compare_means使用aes函数来指定两个自变量group1和group2,同时使用method参数来指定t.test方法计算两种自变量之间的差异。label参数可以用来指定标签的格式。

另一种方式是使用facet_wrap函数,将两种自变量分别显示在不同的子图中,如下所示:

# 使用ggboxplot函数绘制箱线图,同时使用facet_wrap函数将两个自变量分别显示在不同的子图中
ggboxplot(data, x = "group1", y = "value", color = "group2") +
  stat_compare_means(method = "t.test", label = "p.format") +
  facet_wrap(~group2, scales = "free_x")

这段代码将会创建一个箱线图,其中x轴是group1,y轴是value,颜色编码为group2。stat_compare_means使用method参数来指定t.test方法计算两种自变量之间的差异。facet_wrap函数将会将两种自变量group1和group2分别显示在不同的子图中,scales参数可以用来指定x轴的刻度值是否自适应

stat_compare_means同时展示两种自变量的结果

原文地址: https://www.cveoy.top/t/topic/fzbP 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录