I'm trying to plot two different results from tests in the boxplot below. The problem is that it is duplicating my annotation.
These are the results of two tests (E, P) that were taken by two groups (I and II) and each group's score. I want to plot the difference between scores in the plot, but I can't get it right.
> head(df)
ID GROUP TEST SCORE
1 1 I E 0
2 2 I P 3
3 3 I E 3
4 4 I P 11
5 5 I P 8
6 6 I E 0
#code:
ggplot(df, aes(x =GROUP, y = SCORE, fill = TEST)) +
stat_boxplot(geom = "errorbar",
width = 0.15) +
geom_boxplot(aes(fill = TEST), outlier.colour = "black", outlier.shape = 19,
outlier.size= 2, notch = T) +
stat_summary(fun = mean, geom = "point", shape = 20, size= 3,
color= "black", show.legend = FALSE) +
facet_wrap(~ TEST) +
labs(x = "Group",
y = "Score") +
annotate("text",
x = 1.2,
y = max(df$SCORE),
label = expression(paste(italic("t"), "(", 97, ") = ", 9.45, ", ", italic("p"), " < .001")),
parse = TRUE, hjust = 0, vjust = 1, size = 4, color = "black")
** Note: I saw it here that annotate() duplicates it, but I couldn't get the italics without it...
structure(list(ID = 1:525, GROUP = 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, 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, 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, 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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L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, 3L, 12L, 7L, 14L,
5L, 9L, 1L, 9L, 4L, 12L, 10L, 4L, 8L, 12L, 4L, 8L, 7L, 15L,
6L, 11L, 13L, 10L, 14L, 2L, 11L, 2L, 10L, 4L, 11L, 4L, 14L,
2L, 9L, 2L, 12L, 1L, 9L, 9L, 1L, 9L, 5L, 11L, 1L, 9L, 1L,
11L, 6L, 14L, 4L, 13L, 10L, 4L, 11L, 15L, 12L, 2L, 13L, 1L,
11L, 0L, 8L, 1L, 9L, 2L, 0L, 11L, 6L, 15L, 5L, 9L, 10L, 14L,
7L, 15L, 5L, 13L, 6L, 13L, 1L, 5L, 1L, 11L, 8L, 12L, 1L,
7L, 6L, 13L, 3L, 12L, 4L, 12L, 6L, 15L, 1L, 10L, 4L, 12L,
3L, 10L, 3L, 11L, 13L, 1L, 9L, 1L, 11L, 8L, 13L, 7L, 2L,
8L, 2L, 10L, 2L, 9L, 3L, 11L, 5L, 11L, 3L, 11L, 2L, 7L, 3L,
9L, 12L, 2L, 13L, 1L, 12L, 0L, 9L, 1L, 11L, 11L, 11L, 4L,
11L, 5L, 12L, 12L, 10L, 6L, 11L, 6L, 11L, 12L, 14L, 5L, 14L,
10L, 14L, 7L, 11L, 2L, 9L, 4L, 12L, 5L, 11L, 2L, 10L, 2L,
2L, 7L, 1L, 10L, 0L, 14L, 6L, 14L, 10L, 11L, 11L, 8L, 15L,
1L, 9L, 12L, 7L, 12L, 4L, 3L, 10L)), class = "data.frame", row.names = c(NA,
-525L))
You could create a little dummy data frame which includes the TEST
variable, then draw the annotation using geom_richtext
from ggtext
:
library(ggtext)
ggplot(df, aes(x = GROUP, y = SCORE, fill = TEST)) +
stat_boxplot(geom = "errorbar",
width = 0.15) +
geom_boxplot(aes(fill = TEST), outlier.colour = "black", outlier.shape = 19,
outlier.size= 2, notch = T) +
stat_summary(fun = mean, geom = "point", shape = 20, size= 3,
color= "black", show.legend = FALSE) +
facet_wrap(~ TEST) +
labs(x = "Group",
y = "Score") +
geom_richtext(data = data.frame(x = 1.5, SCORE = 16,
TEST = factor(c('E', 'P'), c('P', 'E')),
label = c('t(97) = 9.45, <i>p</i> < 0.001',
'W = 5253.50, <i>p</i> < 0.001')),
aes(x = x, label = label), fill = NA, label.colour = NA)