在 R 中用 p 值绘制多箱线图

Plotting multi boxplot with pvalues in R

我有一个假设的数据样本如下:

df <- read.table(header = TRUE, text =
"time   goal    book    pen temp1   temp2   weight  hight
18  13  18  15  13  15  18  13
18  13  20  16  16  15  20  19
20  12  20  14  18  20  17  15
15  19  18  16  18  20  15  15
16  17  14  12  17  20  20  15
17  12  18  16  17  19  14  16
20  18  15  18  19  13  15  18
13  14  19  20  14  18  12  15
20  16  18  12  16  16  14  13
12  19  19  15  20  14  20  16
14  16  13  14  15  15  15  17
18  19  17  13  14  13  15  17
16  15  14  17  12  18  14  14
19  20  15  13  12  16  20  15
17  12  18  16  16  17  12  20
20  16  14  19  17  20  17  13
17  13  15  16  15  15  17  17
12  17  15  16  14  16  18  13
15  18  17  20  15  13  14  19
19  12  13  17  12  20  12  13
19  18  14  19  15  20  12  16
20  17  18  15  13  19  19  17
16  18  17  19  16  16  12  16
12  15  19  18  20  15  17  19"
)

请考虑,我的栏目比较多。我想为每个组绘制箱线图并在单个图中查看它们

逻辑是:有目标的时间;用笔记; temp1 与 temp2 和 weight 与 hight。 我想查看每个组的 p 值,例如,有目标的时间、有笔的书...

我努力展示结果,但我希望我的描述确实有意义。

根据您的数据,这可能就是您要查找的内容

library (ggpubr)

#get data into long format
long <- data.table::melt(df)

#get colnames
nms <- colnames(df)

# get pairs of variables in a list to pass to comparison
comps <- split(nms,ceiling(seq_along(nms) / 2)) 

#add group to data for colouring
long$group <- rep(letters[1:4],each = nrow(long)/4) 

#plot
ggboxplot(long,x="variable",y="value",fill ="group") + 
stat_compare_means(comparisons = comps)