Adding significance to a plot with facet_nested

I want to add the level of significance my plots with facet_nested, I initially used an old code but it doesn't seem to be working this time around. In the end, I want a plot like the one below.

https://drive.google.com/file/d/1FwKfQ9eKxon4YIMzgI6fJTtS9zo_EQcq/view?usp=sharing

tgc <- structure(list(Group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L), .Label = c("Visible", "Remembered"), class = "factor"), 
    Condition = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 
    3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L
    ), .Label = c("CEN", "IPS", "CTL"), class = "factor"), test = structure(c(1L, 
    1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 
    1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L), .Label = c("Pre-test", "Post-test"
    ), class = "factor"), Session = structure(c(1L, 2L, 1L, 2L, 
    1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
    2L, 1L, 2L, 1L, 2L), .Label = c("Adaptation", "After-effects"
    ), class = "factor"), N = c(12, 12, 12, 12, 12, 12, 12, 12, 
    12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 
    12), Abs_IDE = c(23.7992344166667, 13.0386095591667, 15.7028633063333, 
    19.84749675725, 18.1572712615, 11.68093831675, 16.1626619356667, 
    22.9018392689167, 25.980293756, 13.45901734025, 24.4302041906667, 
    12.0791439846667, 22.4191223275, 8.96559583341667, 16.7862310038333, 
    18.9797044311667, 20.1929425604167, 11.7699855065833, 13.56023988325, 
    19.73171153625, 33.5879796116667, 14.6798949093333, 26.1433479941667, 
    8.74572327816667), sd = c(8.04062173855832, 9.0782958352921, 
    6.90557155294788, 10.7770226212508, 9.90142743265505, 6.13623590788893, 
    9.72344405555326, 11.4928324154261, 14.7569847333998, 8.68503337334045, 
    14.1914679614007, 8.09149043273921, 8.16863922288962, 7.04528824438605, 
    5.65528757276849, 10.2612900552688, 8.59034167901021, 9.73347852641845, 
    6.85089019152845, 10.7480537432634, 14.6034318371352, 7.50792731288404, 
    10.5564213216697, 4.75186261300342), se = c(2.3211275626043, 
    2.62067827214448, 1.99346679750134, 3.11105845572093, 2.85829589680247, 
    1.77137872661536, 2.80691652146197, 3.31769494439875, 4.25997455412779, 
    2.5071531780095, 4.09672392385531, 2.33581208974363, 2.35808302712413, 
    2.0337995322074, 1.63254090124132, 2.96217928782113, 2.4798180404037, 
    2.80981322368957, 1.97768164813376, 3.10269586096884, 4.21564765113117, 
    2.16735192757487, 3.04737634587255, 1.37174457938482), ci = c(5.10876731997174, 
    5.76807398636459, 4.38759083843585, 6.84739349321937, 6.29106685201305, 
    3.89877829018243, 6.17798160935384, 7.3021973383327, 9.37614077600327, 
    5.51820693887549, 9.01682856139793, 5.14108774628837, 5.19010574896418, 
    4.4763625889898, 3.59319829687477, 6.51971265402513, 5.45804270665608, 
    6.18435720796999, 4.35284795892798, 6.82898754627016, 9.27857792031489, 
    4.7703094292883, 6.70723011447976, 3.01918946266214)), row.names = c(NA, 
-24L), class = "data.frame")
library(tidyverse)
library(ggthemes)
library(ggsignif)
library(ggh4x)

 p <- ggplot(tgc, aes(x = Condition, y = Abs_IDE), fill = test) +
   geom_errorbar(aes(ymin=Abs_IDE-se, ymax=Abs_IDE+se, group = test), position = position_dodge(0.5), width=.1) +
   geom_bar(aes(fill = test), stat = "identity", width = 0.5, color = "black", position='dodge') + ylim(0,45) + theme_bw() + theme(
     axis.text.x = element_text(size = 12,face="bold"),#, angle = 10, hjust = .5, vjust = .5),
     axis.text.y = element_text(size = 12, face = "bold"),
     axis.title.y = element_text(vjust= 1.8, size = 16),
     axis.title.x = element_text(vjust= -0.5, size = 16),
     axis.title = element_text(face = "bold")) + xlab("Condition") + ylab("Score") + theme(legend.position="top") +
   scale_fill_manual(values = c("grey80", "grey20")) +
   facet_nested(. ~ Session + Group ) + theme(aspect.ratio = 6/4)
 
 
 p + guides(fill=guide_legend(title="Test:")) + theme(legend.text=element_text(size=14),legend.title=element_text(size=14) ) +
   theme(strip.text = element_text(face="bold", size=12)) 
 
 
 p + geom_signif(
   y_position = c(40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40), xmin = c(0.8, 1.8, 2.8, 3.8, 4.8, 5.8, 6.8, 7.8, 8.8, 9.8, 10.8, 11.8), xmax = c(1.2, 2.2, 3.2, 4.2, 5.2, 6.2, 7.2, 8.2, 9.2, 10.2, 11.2, 12.2),
   annotation = c("*", "*", "*","*", "*", "*","*", "*", "*","*", "*", "*"), tip_length = 0.01, textsize = 4, size = 1
 )
 

It looks like the facet_nested function come from the ggh4x package. You will need to install and load it.

install.packages("ggh4x")
library(ggh4x)

I have it in my main code, I will update my post

I wonder if geom_signif has a problem with barplots?

library(ggplot2)
library(ggsignif)

ggplot(iris, aes(x = Species, y = Sepal.Length)) +
  geom_bar( stat = "identity") + # using `ggsignif` to display comparison of interest
  geom_signif(
    comparisons = list(c("versicolor", "virginica")),
    map_signif_level = TRUE
  )

If you change to a boxplot things seem to work.

ibrary(tidyverse)
library(ggthemes)
library(ggsignif)
library(ggh4x)

p <- ggplot(tgc, aes(x = Condition, y = Abs_IDE), fill = test) +
  geom_boxplot() + facet_nested(. ~ Session + Group )

p +    geom_signif( comparisons = list(c("CEN", "IPS", "CTL")))

p <- ggplot(tgc, aes(x = Condition, y = Abs_IDE), fill = test) +
  geom_boxplot() + facet_nested(. ~ Session + Group )

p +    geom_signif( comparisons = list(c("CEN", "IPS", "CTL")))
1 Like