I'm looking to include the results of multiple statistical tests in a faceted ggplot chart.
I've found lots of excellent examples (like this) on how to include something similar in a title or annotation, however, my interest lies in including it as a text annotation so that I can show the results of many tests on one figure.
I have been able to do this using standard text annotations, however I want to present my results using polymath
/expressions
so that I can produce an annotation that follows the APA style guide as implemented in the package [ggstatsplot]
1, see example below:
I've included code for a reproducible example below using the diamonds
data from ggplot2
. Some of the things that I have tried include:
- trying to store
bquote
andexpression
objects as columns in thewilcox_stats
object — however dplyr doesn't seem to like it - trying to call this all from
ggplot
— however it got quite messy trying to exclude all the annotations thatgeom_text
wanted to print
Any assistance or pointers you can provide would be much appreciated.
# LOAD REQUIRED PACKAGES
library(ggplot2)
library(tidyverse)
library(rstatix)
# CREATE SAMPLE DATA
sample_data <- diamonds %>%
select(cut, color, table) %>%
filter(color == c("E","J")) %>%
mutate(time = factor(case_when(
table %% 2 == 0 ~ "Before",
TRUE ~ "After"))) %>%
group_by(color, time) %>%
sample_n(100) %>%
ungroup() %>%
mutate(numeric_cut = case_when(
cut == "Ideal" ~ 1,
cut == "Premium" ~ 2,
cut == "Very Good" ~ 3,
cut == "Good" ~ 4,
cut == "Fair" ~ 5))
# STAT TESTS
wilcox_test <- sample_data %>%
group_by(color) %>%
wilcox_test(numeric_cut ~ time, paired = TRUE, detailed = TRUE) %>%
select(color, statistic, p, n1)
wilcox_es <- sample_data %>%
group_by(color) %>%
wilcox_effsize(numeric_cut ~ time, paired = TRUE, ci = TRUE) %>%
select(color, effsize, conf.low, conf.high)
## EXTRACT ELEMENTS OF STAT TESTS AND USE THEM TO CREATE ANNOTATION
wilcox_stats <- left_join(wilcox_test, wilcox_es) %>%
mutate(statistic = round(statistic, 1)) %>%
mutate(effsize = round(effsize, 2)) %>%
mutate(p = round(p, 3)) %>%
mutate(result = deparse(bquote(
V[Wilcoxon]==.(statistic)~ #this code does not work
italics(p)==.p~
hat(r) == .effsize~
"CI"["95%"]~
.conf.low~.conf.high~
n[pairs]==.n1)))
## PREPARE PLOT DATA
plot_data <- sample_data %>%
group_by(time, cut, color) %>%
tally() %>%
ungroup() %>%
group_by(color) %>%
mutate(total_n = sum(n)) %>%
mutate(percent = (n/total_n)*100) %>%
mutate(percent = round(percent, 1)) %>%
ungroup() %>%
left_join(wilcox_stats) %>%
mutate(result = case_when(
time == "Before" & cut == "Ideal" ~ "",
time == "After" & cut == "Ideal" ~ "",
time == "Before" & cut == "Premium" ~ "",
time == "After" & cut == "Premium" ~ "",
time == "Before" & cut == "Very Good" ~ "",
time == "After" & cut == "Very Good" ~ result,
time == "Before" & cut == "Good" ~ "",
time == "After" & cut == "Good" ~ "",
time == "Before" & cut == "Fair" ~ "",
time == "After" & cut == "Fair" ~ "")) %>%
mutate(time = factor(time, levels = c("Before", "After", ordered = TRUE)))
## PLOT RESULTS
plot <- plot_data %>%
ggplot() +
aes(x = cut, y = percent, fill = cut) +
geom_bar(stat = "identity") +
geom_text(aes(label = result, y = 30), size = 5, parse = TRUE) +
facet_grid(color ~ time)
The figure below shows the gist of the output I wish to create...