将带 ** 的显着性水平括号添加到分组箱线图中; ggplot

Posted

技术标签:

【中文标题】将带 ** 的显着性水平括号添加到分组箱线图中; ggplot【英文标题】:Adding significance level brackets with ** to a grouped boxplot; ggplot 【发布时间】:2021-12-04 21:11:24 【问题描述】:

我创建了这个箱线图,我需要在每个方面的“现代”和“罗马”马之间添加显着性水平括号。我尝试了 geom_signif 但我只能在“现代”和“罗马”组之间制作括号,而不是在每两匹马之间。 有人可以帮忙吗?

这是我的数据:

   Element    log_cs species  group
1        A 1.0921481   Horse Modern
2        A 1.0920578   Horse Modern
3        A 1.0990291   Horse Modern
4        A 1.1489135   Horse Modern
5        A 1.1017859   Horse Modern
6        A 1.0845620   Horse Modern
7        A 1.0289420   Horse Modern
8        A 1.1027259   Horse Modern
9        A 1.1091967   Horse Modern
10       A 1.1854781   Horse Modern
11       A 1.0999498   Horse  Roman
12       A 1.0944261   Horse  Roman
13       A 1.0956034   Horse  Roman
14       A 1.0751165   Horse  Roman
15       A 1.0841038   Horse  Roman
16       A 1.1073352   Horse  Roman
17       A 1.0776306   Horse  Roman
18       A 1.1109054   Horse  Roman
19       A 1.1231977   Horse  Roman
20       A 1.1026371   Horse  Roman
21       A 1.0917204   Horse  Roman
22       A 1.0938320  Hybrid Modern
23       A 1.0659731  Hybrid Modern
24       A 1.1226732  Hybrid Modern
25       A 1.1170249  Hybrid Modern
26       A 1.0667190  Hybrid Modern
27       A 1.0944956  Hybrid Modern
28       A 1.0728841  Hybrid Modern
29       A 1.0827132  Hybrid Modern
30       A 1.0676629  Hybrid Modern
31       A 1.1269550  Hybrid Modern
32       A 1.0835485  Hybrid Modern
33       A 1.0874964  Hybrid  Roman
34       A 1.1309169  Hybrid  Roman
35       A 1.1144295  Hybrid  Roman
36       A 1.1065604  Hybrid  Roman
37       A 1.1164547  Hybrid  Roman
38       A 1.1373446  Hybrid  Roman
39       A 1.1069174  Hybrid  Roman
40       A 1.0949513  Hybrid  Roman
41       B 1.0485633   Horse Modern
42       B 1.0238247   Horse Modern
43       B 1.0427080   Horse Modern
44       B 1.1040061   Horse Modern
45       B 1.0325064   Horse Modern
46       B 1.0275794   Horse Modern
47       B 0.9587948   Horse Modern
48       B 1.0328241   Horse Modern
49       B 1.0428645   Horse Modern
50       B 1.1462359   Horse Modern
51       B 0.9758035   Horse  Roman
52       B 1.0268603   Horse  Roman
53       B 1.0564401   Horse  Roman
54       B 1.0345251   Horse  Roman
55       B 1.0230175   Horse  Roman
56       B 1.0013129   Horse  Roman
57       B 0.9865836   Horse  Roman
58       B 1.0191887   Horse  Roman
59       B 1.0413617   Horse  Roman
60       B 1.0264960   Horse  Roman
61       B 1.0342843   Horse  Roman
62       B 0.9960231  Hybrid Modern
63       B 0.9920178  Hybrid Modern
64       B 1.0588670  Hybrid Modern
65       B 1.0337936  Hybrid Modern
66       B 1.0343635  Hybrid Modern
67       B 1.0218588  Hybrid Modern
68       B 0.9024005  Hybrid Modern
69       B 1.0408433  Hybrid Modern
70       B 1.0493235  Hybrid Modern
71       B 1.0251871  Hybrid Modern
72       B 1.0311694  Hybrid Modern
73       B 1.0390529  Hybrid  Roman
74       B 1.0356013  Hybrid  Roman
75       B 1.0751427  Hybrid  Roman
76       B 1.0300296  Hybrid  Roman
77       B 1.0393642  Hybrid  Roman
78       B 1.0240472  Hybrid  Roman
79       B 1.0772711  Hybrid  Roman
80       B 1.0607907  Hybrid  Roman
81       B 1.0651311  Hybrid  Roman

这是我用于箱线图的代码

box_test <- ggplot(test, aes(x = group, y = log_cs)) +
  facet_wrap(~Element) +
  geom_boxplot(aes(fill=species), position=position_dodge(.9),
               outlier.size = 1, outlier.alpha = 0.5, show.legend = TRUE)

【问题讨论】:

【参考方案1】:

您是否正在寻找这样的解决方案?

library(tidyverse)
library(ggsignif)

ggplot(df, aes(x = group, y = log_cs)) +
  facet_wrap(~Element) +
  geom_boxplot(aes(fill=species), position=position_dodge(.9),
               outlier.size = 1, outlier.alpha = 0.5, show.legend = TRUE) +
  geom_signif(comparisons = list(c("Modern", "Roman")),
              map_signif_level=TRUE)
  stat_compare_means(aes(group=species), label = "p.signif", label.y = 1.2) 

【讨论】:

这就是我所做的,但我只想比较每个方面的红色(马/马)而不是“现代”和“罗马”组

以上是关于将带 ** 的显着性水平括号添加到分组箱线图中; ggplot的主要内容,如果未能解决你的问题,请参考以下文章

使用 ggplot2 将显着性级别添加到矩阵相关热图

R中ACF和PACF的显着性水平

有没有办法改变 R 中的显着性水平(alpha)?

r中的星星显着性水平

R中怎么就选定变量,而不是从线性回归的所有变量(F-测试)得到的p值(显着性水平)?

在双边检验中比较显着性水平与 p 值