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

时间:2020-11-07 23:41:59

标签: r ggplot2 ggpubr

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

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以及权重都很高。 我想查看每个组的p值,例如目标时间,用笔预定...

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

1 个答案:

答案 0 :(得分:1)

根据您的数据,这可能就是您要寻找的

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)