0

Following the example in this question: R: problems plotting p-values from conover test (Dunn's works fine)

# https://stackoverflow.com/questions/61922336/r-problems-plotting-p-values-from-conover-test-dunns-works-fine
library(dplyr)
library(ggpubr)
library(rstatix)

# I'm going to get the Conover test from this package 

require(DescTools)

# You didn't provide data I will use mtcars

mydata <- mtcars %>% select(mpg, gear)
mydata$gear <- factor(mydata$gear)

# Here's what you were doing

stat.test.dunn <- mydata %>% dunn_test(mpg ~ gear, p.adjust.method = "hochberg") 
stat.test.dunn <- stat.test.dunn %>% add_xy_position(x = "gear")
stat.test.dunn
#> # A tibble: 3 x 13
#>   .y.   group1 group2    n1    n2 statistic       p   p.adj p.adj.signif
#>   <chr> <chr>  <chr>  <int> <int>     <dbl>   <dbl>   <dbl> <chr>       
#> 1 mpg   3      4         15    12      3.76 1.69e-4 5.06e-4 ***         
#> 2 mpg   3      5         15     5      1.65 9.98e-2 2.00e-1 ns          
#> 3 mpg   4      5         12     5     -1.14 2.54e-1 2.54e-1 ns          
#> # … with 4 more variables: y.position <dbl>, groups <named list>, xmin <int>,
#> #   xmax <int>

ggboxplot(mydata, x = "gear", y = "mpg", fill = "gear") +
  stat_pvalue_manual(stat.test.dunn, hide.ns = FALSE)

I would like to know how I can change the order of the groups on the x-axis. They are automatically sorted in alphabetical order but I don't know how to assign a specific order to the groups. Following the example, I would like the gear 4 group to appear on the left, gear 5 in the center and gear 3 on the right. And I need the p values to be assigned correctly between the groups.

I tried to arrange the table by ordering the group1 column but the result is the same. Also tried adding:

+ scale_x_discrete(limits=c("group_I_want_on_left", "group_I_want_in_center", "group_I_want_on_right"))

This does change the order of the boxes but the p values are not correct because they appear in the same order as when I had the boxes in alphabetical order.

0 Answers0