Having trouble getting stat_pvalue_manual to facet across my ggplot

84 Views Asked by At

I'd like to make a facetted plot, with the p-values from my emmeans() table added using stat_pvalue_manual(). However, I can't seem to get them to facet. Any help would be very welcome!

Here is some demo data:

demo_data <- structure(list(tissue = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                              1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 
                                              3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), levels = c("Cortex", 
                                                                                                      "Striatum", "Brainstem", "Spinal cord"), class = "factor"), treatment = structure(c(1L, 
                                                                                                                                                                                          1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 
                                                                                                                                                                                          1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 
                                                                                                                                                                                          4L), levels = c("Vehicle", "3ug", "10ug", "30ug"), class = "factor"), 
                         level = c(95.55770146, 106.41916, 95.99984979, 102.0232887, 
                                   93.79787091, 95.49592139, 67.69702761, 91.06655218, 55.14942341, 
                                   90.74452771, 87.37290887, 93.64040623, 43.07433708, 51.11824551, 
                                   38.74906918, 41.70615026, 58.38525145, 96.34090317, 103.6658548, 
                                   98.55401948, 101.4392226, 81.43533635, 80.3636441, 75.10048942, 
                                   87.51421957, 49.99619784, 57.18770233, 54.19956166, 59.01564607, 
                                   22.62269918, 26.99647643, 17.39055145, 26.10645416, 20.97243233
                         )), row.names = c(NA, -34L), class = c("tbl_df", "tbl", "data.frame"
                         ))

Here are the comparisons from 'emmeans()':

demo_contrasts <- structure(list(tissue = c("Cortex", "Cortex", "Cortex"), contrast = c("Vehicle - 30ug", 
                                                                                     "3ug - 30ug", "10ug - 30ug"), estimate = c(53.3933892915, 40.4077323265, 
                                                                                                                                35.120205859), SE = c(7.90968646506267, 7.90968646506267, 7.90968646506267
                                                                                                                                ), df = c(13, 13, 13), t.ratio = c(6.75037999639306, 5.10863894605459, 
                                                                                                                                                                   4.44015145405915), p.value = c("7.1e-05", "1.0e-03", "3.3e-03"
                                                                                                                                                                   ), p.signif = structure(c("***", "**", "**"), class = "noquote"), 
                              group1 = c("Vehicle", "3ug", "10ug"), group2 = c("30ug", 
                                                                               "30ug", "30ug"), y.position = c(131.9999999835, 143.999999982, 
                                                                                                               155.9999999805)), row.names = c(NA, -3L), class = "data.frame")

And here is how I'm trying to plot it:

ggplot(demo_data, aes(x = treatment, y = level)) +
facet_wrap(~ tissue) +
stat_summary(aes(fill = treatment),
            fun = mean, colour = "black", geom = "bar") +
geom_jitter(aes(fill = treatment),
           shape = 21, colour = "black", 
           width = 0.1, height = 0, 
           size = point_size) +
stat_summary(fun.data = mean_se,  
            geom = "errorbar", width = 0.2) +
stat_pvalue_manual(demo_contrasts,
                  label = "{p.signif}",
                  tip.length = 0.01,
                  hide.ns = TRUE,
                  size = point_size + 3)

enter image description here

As you can see, it doesn't seem to facet!

0

There are 0 best solutions below