r – Error adding significance levels in ggplot boxplot after changing the stats table

I am trying to add significance levels to a geom_boxplot by using stats_pvalue_manual. Since I have a 12-level factor I ordered the data by mean size. I only keep the significant comparisons that are closest to each other. If I use the stats table everything works (but looks horrible).

Here is what works:

stat_pvalue <- pl %>% 
 rstatix::wilcox_test(rating ~ lang2) %>%
 filter(p < 0.001) %>% 
 rstatix::add_significance("p") %>% 
 rstatix::add_y_position(fun="mean") %>% 
 mutate(y.position = seq(min(y.position), max(y.position),length.out = n()))

A tibble: 53 × 12

1 rating KLIN NAVI 751 751 218406. 9.95e- 15 3.18e- 13 **** **** 1.40 <chr [2]>
2 rating KLIN DOTH 751 753 161834. 1.85e- 48 9.62e- 47 **** **** 1.75 <chr [2]>
3 rating KLIN KHUZ 751 750 142862. 1.34e- 63 7.64e- 62 **** **** 2.10 <chr [2]>
4 rating KLIN ATLA 751 747 143668. 3.14e- 62 1.73e- 60 **** **** 2.45 <chr [2]>
5 rating KLIN ADUN 751 751 135155 2.22e- 70 1.29e- 68 **** **** 2.80 <chr [2]>
6 rating KLIN KESH 751 754 119372 1.3 e- 86 7.8 e- 85 **** **** 3.15 <chr [2]>
7 rating KLIN ORCI 751 749 117751 4.57e- 87 2.79e- 85 **** **** 3.51 <chr [2]>
8 rating KLIN UIUI 751 755 106752 2.94e-100 1.85e- 98 **** **** 3.86 <chr [2]>
9 rating KLIN VULC 751 749 95561 1.11e-111 7.1 e-110 **** **** 4.21 <chr [2]>
10 rating KLIN SIND 751 751 87589 2.64e-121 1.72e-119 **** **** 4.56 <chr [2]>

ggplot(pl, aes(x=lang2, y=rating)) + geom_boxplot() +
  ggpubr::stat_pvalue_manual(stat_pvalue, label = "p.signif") +
  theme_bw(base_size = 16)

Boxplot with significant differences

Then I just keep the comparisons that are close together, i.e. the first significant difference per factor level

stat_value2 <- stat_pvalue %>% group_by(group1) %>% slice_head(n=1)

stat_value2
#

A tibble: 10 × 12

.y. group1 group2 n1 n2 statistic p p.adj p.adj.signif p.signif y.position groups
1 rating ADUN UIUI 751 755 255755 7.9 e- 4 1.1 e- 2 * *** 15.5 <chr [2]>
2 rating ATLA KESH 747 754 242381 1.81e- 6 3.26e- 5 **** **** 13.3 <chr [2]>
3 rating DOTH ADUN 753 751 242065 8.64e- 7 1.73e- 5 **** **** 8.78 <chr [2]>
4 rating KESH VULC 754 749 252550 2.84e- 4 4 e- 3 ** *** 16.9 <chr [2]>
5 rating KHUZ KESH 750 754 243727 2.15e- 6 3.66e- 5 **** **** 11.2 <chr [2]>
6 rating KLIN NAVI 751 751 218406. 9.95e-15 3.18e-13 **** **** 1.40 <chr [2]>
7 rating NAVI DOTH 751 753 221564. 1.29e-13 4 e-12 **** **** 5.26 <chr [2]>
8 rating ORCI SIND 749 751 230356 5.86e-10 1.41e- 8 **** **** 17.9 <chr [2]>
9 rating UIUI SIND 755 751 239677 1.09e- 7 2.29e- 6 **** **** 18.6 <chr [2]>
10 rating VULC SIND 749 751 248230. 5.5 e- 5 8.8 e- 4 *** **** 19.3 <chr [2]>

Then I get the following error message for the figure

ggplot(pl, aes(x=lang2, y=rating)) + geom_boxplot() +
    ggpubr::stat_pvalue_manual(stat_value2, label = "p.signif") +
    theme_bw(base_size = 16)

Error in dplyr::mutate():
! Problem while computing label = as.character(data %>% pull("p.signif")).
label must be size 1, not 10.
ℹ The error occurred in group 1: group1 = “ADUN”.
Backtrace:

  1. ggpubr::stat_pvalue_manual(stat_value2, label = “p.signif”)
  2. dplyr:::mutate.data.frame(…)

I am at a total loss here. I found the lines in the code but I cannot figure out what changed. Help would be very much appreciated.

I tried to change the order of group1 and also the aesthetics of the figure but nothing worked.

Read more here: Source link