3

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:

enter image description here

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 and expression objects as columns in the wilcox_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 that geom_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...

enter image description here

tjebo
  • 21,977
  • 7
  • 58
  • 94
vengefulsealion
  • 756
  • 11
  • 18

1 Answers1

2

I'd probably create the expressions using paste, (tbh, because I find it easier to include variables).

I have slightly shortened the code and also not used your full expression, but I think it should hopefully be enough to get the idea.

library(tidyverse)

sample_data <- diamonds %>%
  select(cut, color, table) %>%
  filter(color == c("E","J")) %>%
  mutate(time = if_else(table %% 2 == 0, "Before", "After")) %>%
  group_by(color, time) %>%
  sample_n(100) %>%
  ungroup() %>%
  mutate(numeric_cut = as.numeric(cut))

wilcox_test <- sample_data %>%
  group_by(color) %>%
  rstatix::wilcox_test(numeric_cut ~ time, paired = TRUE, detailed = TRUE) %>%
  select(color, statistic, p, n1)

wilcox_es <- sample_data %>%
  group_by(color) %>%
  rstatix::wilcox_effsize(numeric_cut ~ time, paired = TRUE, ci = TRUE) %>%
  select(color, effsize, conf.low, conf.high)

Here the crucial bit

wilcox_stats <- left_join(wilcox_test, wilcox_es) %>%
  mutate(statistic = round(statistic, 1),
         effsize = round(effsize, 2),
         p = round(p, 3),
         label = paste('V[Wilcoxon]==', statistic, '~italic(p)==~', p))
#> Joining, by = "color"
plot_data <- sample_data %>%
  count(time, cut, color) %>%
  group_by(color) %>%
  mutate(total_n = sum(n),
         percent = round((n/total_n)*100,1)) %>%
  ungroup() %>%
  left_join(wilcox_stats) %>%
  mutate(result = if_else(time == "After" & cut == "Very Good", label, ""))
#> Joining, by = "color"

plot_data %>%
  ggplot() +
  aes(x = cut, y = percent, fill = cut) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = result, y = 30), parse = TRUE) +
  facet_grid(color ~ time)

Created on 2020-04-26 by the reprex package (v0.3.0)

tjebo
  • 21,977
  • 7
  • 58
  • 94