Home > OS >  R ggplot2 show significance between subgroups
R ggplot2 show significance between subgroups

Time:11-30

I am trying to show the significance levels within a group consisting of two factors, but I seem to always get the significance levels between groups which is not what I want.

df <- structure(list(Datum = structure(c(2L, 1L, 3L, 1L, 1L, 3L, 1L, 
2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 3L, 1L, 2L), .Label = c("2021-04-08", 
"2021-05-17", "2021-07-07"), class = "factor"), Soll = c("1202", 
"172", "119", "1192", "119", "1189", "1189", "552", "1189", "1192", 
"2484", "119", "1189", "1189", "172", "552", "1192", "172", "1189", 
"172"), Plot = c("6", "5", "3", "4", "6", "5", "4", "5", "7", 
"8", "3", "6", "6", "1", "8", "3", "1", "3", "8", "4"), Entfernung = structure(c(2L, 
1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 
1L, 2L, 2L), .Label = c("2", "5"), class = "factor"), Behandlung = structure(c(1L, 
1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
2L, 1L, 1L), .Label = c("a", "b"), class = "factor"), DGUnkraut = c(3.5, 
0, 2.8, 3, 0.3, 2, 1, 3, 0, 0.3, 10, 0, 1.7, 2.5, 0.2, 0.3, 9, 
0.3, 2.5, 0.2)), class = "data.frame", row.names = c(NA, -20L
))

This what I have tried so far:

library(tidyverse)
library(ggsignif)

df   %>% group_by (Datum, Entfernung)%>%
  ggplot(., aes(Entfernung, DGUnkraut , color = Datum))  
  geom_boxplot() 
  geom_signif(comparisons =list (c("2","5")), 
              map_signif_level = T)

So I would like to see the significant differences between "2" and "5" for each of the three dates, so for example that the significance level of the red boxplot with the date "2021-04-08" and Entfernung = "2" is compared to the one where Entfernung = "5" Any help is appreciated! Thanks a lot in advance :)

CodePudding user response:

Facets don't seem to work with {ggsignif}, but you could fake them, by looping over your dates, and then patching the plots together.

Below one way

library(ggsignif)
library(patchwork)

df   %>% 
split(., .$Datum) %>%
map(~{
  ggplot(., aes(Entfernung, DGUnkraut , color = Datum))  
  geom_boxplot() 
  geom_signif(comparisons =list(c("2","5")), 
              map_signif_level = T)  
scale_x_discrete(drop = FALSE)
}) %>%
wrap_plots()   plot_layout(guides = "collect")

enter image description here

  • Related