Where can we find the significance level in the ggplot object slots?
eleonore-schneeg opened this issue · 1 comments
eleonore-schneeg commented
Hi,
I'm looking to find the slot where the geom_signif value (ie. '*' or ns) is stored in a ggplot object.
Thanks!
IndrajeetPatil commented
Hi, to access annotations, you can first build the plot using ggplot_build()
and then extract a list of data frames, each corresponding to a geom. The one corresponding to geom_signif()
will have an annotation column.
library(ggplot2)
library(ggsignif)
p <- ggplot(mpg, aes(class, hwy)) +
geom_boxplot() +
geom_signif(
comparisons = list(c("compact", "midsize"), c("minivan", "suv")),
map_signif_level = TRUE
)
pb <- ggplot_build(p)
#> Warning in wilcox.test.default(c(29, 29, 31, 30, 26, 26, 27, 26, 25, 28, :
#> cannot compute exact p-value with ties
pb$data
#> [[1]]
#> ymin lower middle upper ymax outliers notchupper
#> 1 23 24.0 25.0 26.0 26 26.41319
#> 2 23 26.0 27.0 29.0 33 35, 37, 35, 44 27.69140
#> 3 23 26.0 27.0 29.0 32 27.74026
#> 4 21 22.0 23.0 24.0 24 17 23.95278
#> 5 15 16.0 17.0 18.0 20 12, 12, 12, 22 17.55009
#> 6 20 24.5 26.0 30.5 36 44, 41 27.60241
#> 7 14 17.0 17.5 19.0 22 12, 12, 25, 24, 27, 25, 26, 23 17.90132
#> notchlower x flipped_aes PANEL group ymin_final ymax_final xmin xmax xid
#> 1 23.58681 1 FALSE 1 1 23 26 0.625 1.375 1
#> 2 26.30860 2 FALSE 1 2 23 44 1.625 2.375 2
#> 3 26.25974 3 FALSE 1 3 23 32 2.625 3.375 3
#> 4 22.04722 4 FALSE 1 4 17 24 3.625 4.375 4
#> 5 16.44991 5 FALSE 1 5 12 22 4.625 5.375 5
#> 6 24.39759 6 FALSE 1 6 20 44 5.625 6.375 6
#> 7 17.09868 7 FALSE 1 7 12 27 6.625 7.375 7
#> newx new_width weight colour fill size alpha shape linetype
#> 1 1 0.75 1 grey20 white 0.5 NA 19 solid
#> 2 2 0.75 1 grey20 white 0.5 NA 19 solid
#> 3 3 0.75 1 grey20 white 0.5 NA 19 solid
#> 4 4 0.75 1 grey20 white 0.5 NA 19 solid
#> 5 5 0.75 1 grey20 white 0.5 NA 19 solid
#> 6 6 0.75 1 grey20 white 0.5 NA 19 solid
#> 7 7 0.75 1 grey20 white 0.5 NA 19 solid
#>
#> [[2]]
#> x xend y yend annotation group flipped_aes PANEL shape
#> 1 2 2 44.64 45.60 NS. compact-midsize-1 FALSE 1 19
#> 2 2 3 45.60 45.60 NS. compact-midsize-1 FALSE 1 19
#> 3 3 3 45.60 44.64 NS. compact-midsize-1 FALSE 1 19
#> 4 4 4 44.64 45.60 *** minivan-suv-2 FALSE 1 19
#> 5 4 7 45.60 45.60 *** minivan-suv-2 FALSE 1 19
#> 6 7 7 45.60 44.64 *** minivan-suv-2 FALSE 1 19
#> colour textsize angle hjust vjust alpha family fontface lineheight linetype
#> 1 black 3.88 0 0.5 0 NA 1 1.2 1
#> 2 black 3.88 0 0.5 0 NA 1 1.2 1
#> 3 black 3.88 0 0.5 0 NA 1 1.2 1
#> 4 black 3.88 0 0.5 0 NA 1 1.2 1
#> 5 black 3.88 0 0.5 0 NA 1 1.2 1
#> 6 black 3.88 0 0.5 0 NA 1 1.2 1
#> size
#> 1 0.5
#> 2 0.5
#> 3 0.5
#> 4 0.5
#> 5 0.5
#> 6 0.5
Created on 2022-08-11 by the reprex package (v2.0.1.9000)