Add a paired comparison between different groups in boxplot using ggplot

I need to create boxplots and prints with brackets around those groups, showing significant differences using a pairwise multiple comparison test (e.g. Dunn's test).

So far, I have the following code:

plot_list <- data %>%
  group_by(parts) %>%
  mutate(parts_count = n()) %>%
  filter(parts_count > 1) %>% # Filter out estacoes with only 1 data point
  ggplot(aes(x = as.factor(year), y = parameter)) +
  geom_boxplot(width = 0.5, outlier.shape = 16) +   labs x = "Years", y = "X") +
  labs( x = "Years", y = "parameter") +
  facet_wrap(~ parts, scales = "free") +
  theme_bw()   

which produces a print similar to the attached image [multiples Boxplots]

Any ideas? I tried stat_compare_means and pairwise_wilcox_test, but still no success. I don't receive any results or errors, it simply doesn't plot.

Raw data simulated data:

data <- data.frame( station = c("RD009", "RD009", "RD009", "RD009", "RD009", "RD013", "RD013", "RD013", "RD013", "RD013", "RD013", "RD013", "RD019", "RD019", "RD019", "RD019", "RD019", "RD019", "RD019", "RD023"), year = c("1997 a 2015", "2016", "1997 a 2015", "1997 a 2015", "2017 a 2019", "1997 a 2015", "1997 a 2015", "1997 a 2015", "1997 a 2015", "1997 a 2015", "1997 a 2015", "1997 a 2015", "1997 a 2015", "1997 a 2015", "1997 a 2015", "1997 a 2015", "1997 a 2015", "1997 a 2015", "1997 a 2015", "1997 a 2015"), parameter = c(1.88, 0.0800, 0.77, 0.1400, 0.7, 0.33, 1.5, 5.658, 0.3100, 2.22, 0.4100, 0.5, 8.99, 5.56, 3.4, 0.2700, 10.90, 0.4000, 0.9232, 25.52), parts = c("TI 1", "TI 1", "TI 1", "TI 1", "TI 1", "Ref 1", "Ref 1", "Ref 1", "Ref 1", "Ref 1", "Ref 1", "Ref 1", "TI 3", "TI 3", "TI 3", "TI 3", "TI 3", "TI 3", "TI 3", "TI 3") )

I am not completely sure what you want but have a look at {ggpubr} as it is described in 12 Extensions to ggplot2 for More Powerful R Visualizations

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.