I have just prepared a reprex:
library(plyr)
library(tidyverse)
datas <- structure(list(Pain = structure(c(2L, 1L, 1L, 1L, 2L, 2L, 1L,
2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L,
1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 1L,
1L, 1L, 2L), levels = c("No", "Yes"), class = "factor"), Sex = structure(c(1L,
1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L,
1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L), levels = c("Female", "Male"
), class = "factor"), `23` = c(3.7, 9.9, 18.1, 12.8, 2.3, 0,
8.7, 3.7, 1.5, 8.6, 5.7, 5.8, 2.1, 1.5, 6.1, 1.6, 0.6, 2.9, 9.1,
7.4, 0, 0, 9.5, 8.7, 4.6, 0, 0, 8.6, 1.6, 7.3, 6.3, 3, 7.8, 2.2,
0.7, 3.9, 2.8, 0.4, 6.2, 1.1, 2.3, 4.9, 4.5, 8, 8.6, 3.9, 1.5,
1.6, 0, 1.4, 3.2, 3.6, 3, 5.1, 4.1, 8.8, 9.6, 0.7)), row.names = c(NA,
-58L), class = c("tbl_df", "tbl", "data.frame"))
medians <- ddply(datas, .(Pain, Sex), summarise, m = round(median(`23`), 1))
means <- ddply(datas, .(Pain, Sex), summarise, m = round(mean(`23`), 1))
datas %>%
dplyr::select(Pain, Sex, `23`) %>%
dplyr::group_by(Pain, Sex) %>%
ggplot(aes(y = `23`, x = Sex, fill = Pain)) +
geom_boxplot(fatten = 1, outlier.alpha = 0.50, position = position_dodge(width = 1.5), width = 0.8) +
geom_text(data = medians, aes(x = Sex, y = m, label = m), color = 'blue', vjust = -0.5, size = 5, position = position_dodge(width = 1.5)) +
geom_text(data = means, aes(x = Sex, y = m, label = m), color = 'red', vjust = 0.5, size = 5, position = position_dodge(width = 1.5)) +
stat_boxplot(geom ='errorbar', position = position_dodge(width = 1.5)) +
stat_summary(aes(ymax = ..y.., ymin = ..y.., color = 'Mean'),
fun = mean, geom = 'errorbar', width = .75, linetype = 'solid', position = position_dodge(width = 1.5)) +
stat_summary(aes(ymax = ..y.., ymin = ..y.., color = 'Median'),
fun = median, geom = 'errorbar', width = .75, linetype = 'solid', position = position_dodge(width = 1.5)) +
scale_colour_manual('Values', values = c(Median = 'blue', Mean = 'red')) +
scale_fill_manual(values = c('lightblue', 'lightgreen')) +
theme(legend.position = 'top',
text = element_text(size = 12),
strip.text = element_text(size = 11, face = "bold"),
legend.text = element_text(size = 12),
axis.text.x = element_text(size = 16, vjust = 1),
panel.spacing.x = unit(2, "lines")) +
facet_wrap(vars(Sex), scales = 'free')
Any help much appreciated, thank you