Home > Enterprise >  Moving statistic annotations in ggplot
Moving statistic annotations in ggplot

Time:01-20

Using this data set:

structure(list(sample = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 
28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 
44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54), day = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L), levels = c("2", "6"), class = "factor"), 
    treatment = c("a", "a", "a", "a", "a", "a", "a", "b", "b", 
    "b", "b", "b", "b", "c", "c", "c", "c", "c", "c", "d", "d", 
    "d", "d", "d", "d", "d", "d", "a", "a", "a", "a", "a", "a", 
    "a", "b", "b", "b", "b", "b", "b", "c", "c", "c", "c", "c", 
    "c", "d", "d", "d", "d", "d", "d", "d", "d"), group = c("count", 
    "count", "count", "count", "count", "count", "count", "count", 
    "count", "count", "count", "count", "count", "count", "count", 
    "count", "count", "count", "count", "count", "count", "count", 
    "count", "count", "count", "count", "count", "count", "count", 
    "count", "count", "count", "count", "count", "count", "count", 
    "count", "count", "count", "count", "count", "count", "count", 
    "count", "count", "count", "count", "count", "count", "count", 
    "count", "count", "count", "count"), result = c(1.94000836127381, 
    2.07418521661429, 0.812685661255674, 0.199532997122114, 0.720956738045624, 
    2.25080298569014, 1.72685286125659, 1.1066027850052, 4.1487948134003, 
    5.06163333946851, 8.45581635201957, 1.39519183814535, 5.22744057847467, 
    77.578763434025, 81.5688787451947, 57.7998831807876, 72.5246292216229, 
    53.7941684202605, 18.1902377363129, 7.2040245328528, 18.7399963681316, 
    12.0408266827075, 16.9381875648501, 4.94300230430152, 7.7656112238281, 
    3.62337602915357, 9.29131381820146, 17.3474341955159, 0.654156425021601, 
    18.2284156894217, 8.1096329723588, 2.59461805212543, 11.635214608248, 
    10.338591963394, 17.096512619713, 2.29518753169706, 13.6312931040208, 
    2.40586814654832, 18.2260582559852, 0.813121453291961, 86.1680580200406, 
    86.1245441941217, 75.7365169486812, 51.2171310942499, 62.4301976210013, 
    38.1836807429795, 31.2339903053221, 7.92988025761869, 8.27444313173916, 
    30.062084984576, 44.8857368621187, 15.0021164008775, 23.395907046137, 
    45.0063042518017)), row.names = c(NA, -54L), class = c("tbl_df", 
"tbl", "data.frame"))

I created the following code to create graphs with build-in Mann-Whitney results.

stat_test <- list(c("a","b"), c("a","c"))

ggplot(data = filter(dummy_data)) 
  aes(x = treatment, y = result, color = day) 
    geom_point(shape = 1, position = position_jitterdodge(dodge.width = 0.5, jitter.width = 0.1)) 
  stat_summary(fun = mean, geom = "crossbar", width = 0.3, mapping = aes(group = day),
        position=position_dodge(0.5)) 
  stat_compare_means(data = filter(dummy_data, day == "6"),
                     aes(x = treatment, y = result),
                     comparisons = stat_test,
                     method = "wilcox.test", 
                     paired = FALSE,
                     label = "p.signif") 
  theme_classic() 
  theme(legend.position="bottom") 
  labs(
    y = "Dummy data", 
    x = "Treatment" 
  )

ggsave("Dummy.png")

which results in (an almost) nice graph..

Dummy data

However, my statistical test is only relevant for day 6. But the statistical lines are positioned in the middle between day and 6. So I want to move these lines a bit so they are above day 6.

Thanks for your help...

PS (I modified my previous post, now using dummy data)

CodePudding user response:

You could use ggplot_build to modify the stat_compare_means layer by adding a small value to the x and xend columns which are the coordinates of the lines. Here is a reproducible example:

stat_test <- list(c("a","b"), c("a","c"))
library(ggplot2)
library(ggpubr)
library(dplyr)

p <- ggplot(data = filter(dummy_data)) 
  aes(x = treatment, y = result, color = day) 
  geom_point(shape = 1, position = position_jitterdodge(dodge.width = 0.5, jitter.width = 0.1)) 
  stat_summary(fun = mean, geom = "crossbar", width = 0.3, mapping = aes(group = day),
               position=position_dodge(0.5)) 
  stat_compare_means(data = filter(dummy_data, day == "6"),
                     aes(x = treatment, y = result),
                     comparisons = stat_test,
                     method = "wilcox.test", 
                     paired = FALSE,
                     label = "p.signif") 
  theme_classic() 
  theme(legend.position="bottom") 
  labs(
    y = "Dummy data", 
    x = "Treatment" 
  )

q <- ggplot_build(p)

q$data[[3]]$x <- q$data[[3]]$x   0.12
q$data[[3]]$xend <- q$data[[3]]$xend   0.12

q <- ggplot_gtable(q)

plot(q)

Created on 2023-01-19 with reprex v2.0.2

CodePudding user response:

Thanks for this solution, would have never found this myself.

  • Related