this is a follow up question. How can I apply APA's rule dynamically in geom_bracket()
?
- RULES:
"A p-value larger than .01 should be reported to two decimal places, p-values between .01 and .001 to three decimal places, and p-values less than .001 simply as p < .001." (source)
- Code:
library(tidyverse)
library(rstatix) # Identify outliers and Stats Summary function
library(ggpubr) # Plot correlation
library(RVAideMemoire) # Shapiro test on groups
library(glue) # Round p value in the plots
library(stringr)
library(weights) ## remove 0.
### long:
dfPaired <- df
dfLong <- df %>% pivot_wider(names_from = TEST, values_from = VALUE)
### Create list outside of the loop
PairedtestResults <- list()
### run loop
for (bb in seq(from = 1, to = 4, by = 2)) {
## Create vars
PairedVar1 <- dfPaired[bb+1] #G1 Variables
PairednVar1 <- names(PairedVar1)
dfPaired$PairedVar1Unlist <- unlist(PairedVar1)
PairedVar2 <- dfPaired[bb+2] #G2 Variables
PairednVar2 <- names(PairedVar2)
dfPaired$PairedVar2Unlist <- unlist(PairedVar2)
### store and perform tests:
PairedtestResults[[bb]] <- t.test(dfPairedFiltered$PairedVar1Unlist, dfPairedFiltered$PairedVar2Unlist,
paired = T, data = dfPaired, exact = F)
PairedtestResults[[bb]]$data.name <- str_glue("{PairednVar1} and {PairednVar2}")
## wide to long to plot it:
### wide to long to plot data and extract eff size:
dfplot <- dfPairedFiltered %>%
dplyr::select(ID, PairedVar1Unlist, PairedVar2Unlist) %>%
pivot_longer(c(PairedVar1Unlist, PairedVar2Unlist),
names_to = "TEST",
values_to = "VALUE") %>%
mutate(LANGUAGE = case_when(TEST == "PairedVar1Unlist" ~ "GROUP1",
TEST == "PairedVar2Unlist" ~ "GROUP2")) %>%
mutate(TEST = as.factor(TEST),
TEST = fct_relevel(TEST, c("GROUP1", "GROUP2"))) %>%
dplyr::select(ID, VALUE, TEST)
}
dfLong %>%
ggplot(., aes(x = TEST, y = VALUE)) +
stat_boxplot(geom = "errorbar",
width = 0.15) +
geom_boxplot(aes(fill = TEST), outlier.colour = "yellow", outlier.shape = 18,
outlier.size= 2, notch = F) +
geom_bracket(xmin = "TEST1", xmax = "TEST2",
y.position = 250,
position = "identity",
label = glue::glue(
paste0(
"~italic(t) ({PairedtestResults[[bb]][['parameter']]}) == {round(PairedtestResults[[bb]][['statistic']], 2)}*','",
"~italic(p) == '{rd(PairedtestResults[[bb]][['p.value']], digits = 3)}'*','"
)
),
type = "expression",
inherit.aes = F,
data = dfLong)
How can I dynamically apply APA's rule to geom_bracket()
's label
?
- edit: I tried to make a custom function to change the p-value before passing it into the graph, but it isn't working:
convertToAPA <- function(x) {
if (x < 0.001) {
PairedtestResults[[bb]][['p.value']] <- as.numeric(.001)
} else if (x >= 0.001 && x <= 0.01) {
PairedtestResults[[bb]][['p.value']] <- as.numeric(rd(x, digits = 3))
} else if (x > .01) {
PairedtestResults[[bb]][['p.value']] <- as.numeric(rd(x, digits = 2))
}
return(PairedtestResults[[bb]][['p.value']])
}
- data:
structure(list(ID = 1:31, TEST1 = c(70, 66, 59, 61, 73, 90, 59,
82, 60, 40, 77, 49, 61, 92, 56, 56, 68, 87, 87, 87, 79, 45, 83,
50, 44, 95, 82, 134, 42, 70, 84), TEST2 = c(73, 140, 59, 70,
107, 91, 95, 133, 80, 45, 96, 50, 56, 120, 110, 110, 83, 113,
114, 134, 57, 65, 71, 52, 56, 138, 126, 118, 72, 87, 112), TEST3 = c(34,
51, 67, 33, 56, 37, 38, 46, 47, 45, 45, 95, 39, 47, 32, 42, 36,
45, 39, 48, 35, 38, 60, 72, 31, 49, 60, 30, 27, 56, 38), TEST4 = c(40,
45, 46, 30, 72, 26, 22, 26, 27, 38, 21, 65, 19, 25, 32, 66, 50,
29, 16, 35, 40, 34, 40, 54, 28, 43, 48, 32, 36, 52, 23)), row.names = c(NA,
-31L), class = c("tbl_df", "tbl", "data.frame"))