2

I have a plot, similar to the one in the picture (taken from here):

library(ggplot2)

# create fake dataset with additional attributes - sex, sample, and temperature
x <- data.frame(
 values = c(runif(100, min = -2), runif(100), runif(100, max = 2), runif(100)),
 sex = rep(c('M', 'F'), each = 100),
 sample = rep(c('sample_a', 'sample_b'), each = 200),
 temperature = sample(c('15C', '25C', '30C', '42C'), 400, replace = TRUE)
)

# compare different sample populations across various temperatures
ggplot(x, aes(x = sample, y = values, fill = sex)) +
geom_boxplot() +
facet_wrap(~ temperature)

enter image description here

I want that for each sample (sample_a/b), there would be a statistical comparison (wilcoxon) between the F and M groups against an additional expected data.

I've tried adding the expected data as another boxplot next to F & M samples, or as points over the data - but for none of these options I succeeded in figuring how to do the statistical analysis using ggplot2 stat layers.

Michael Harper
  • 14,721
  • 2
  • 60
  • 84
Keity
  • 143
  • 1
  • 10
  • 2
    You will probably have to precalculate these statistics, as I don't believe ggplot is designed for such calculations. [This](https://stackoverflow.com/questions/46783163/r-ggplot2-boxplots-with-wilcoxon-significance-levels-and-facets-show-only-sig) question might help. Alternatively the [ggpubr](https://rpkgs.datanovia.com/ggpubr/reference/stat_compare_means.html) package looks potentially suitable for this – Michael Harper Mar 12 '19 at 11:53
  • I didn't see it before, thank you. even though i'm not sure it answers my question. but your answer did support my assumption - that it's not possible to do that using ggplot2. thank you again! :) – Keity Mar 12 '19 at 11:59

0 Answers0