I'm struggling to create a series of high-quality ggboxplots, like the one I attach as follows:
- With labels for ANOVA with F(df) test value, p.value and effects size
- With multi-pairwose comparisons bars with bars and stars with significant difference.
Statistics for post-hocs comparisons have been obtained for the example above in the way you could find following this link page and I've run the following code
#Compute the post-hocs
postHocs <- df_join %>%
tidyr::pivot_longer(., -c(ID, GR, SES, COND),'signals')%>%
mutate(signals = fct_relevel(signals,
c("P3FCz", "P3Cz", "P3Pz",
"LPPearlyFCz", "LPPearlyCz", "LPPearlyPz",
"LPP1FCz", "LPP1Cz", "LPP1Pz",
"LPP2FCz", "LPP2Cz", "LPP2Pz"))) %>%
arrange(signals) %>%
group_by(signals) %>%
pairwise_t_test(
value ~ COND, paired = TRUE,
p.adjust.method = "bonferroni"
) %>%
#dplyr::select(., -'signals')%>%
print()
while the Anova statistics have been obtained:
res.aov <- df_join %>%
tidyr::pivot_longer(., -c(ID, GR, SES, COND),'signals')%>%
mutate(signals = fct_relevel(signals,
c("P3FCz", "P3Cz", "P3Pz",
"LPPearlyFCz", "LPPearlyCz", "LPPearlyPz",
"LPP1FCz", "LPP1Cz", "LPP1Pz",
"LPP2FCz", "LPP2Cz", "LPP2Pz"))) %>%
arrange(signals) %>%
group_by(signals) %>%
anova_test(., dv = value, wid = ID, within = COND) %>%
print()
from which I've tried to obtain the interested statistics with this code:
unnested_aov <- lapply(res.aov$anova, `[[`, 1)
I scripted this for loop to adding the statistics to graphs
comparisons <- postHocs %>% add_xy_position(x = "COND")
comparisons
for (i in 5:ncol(df_join)) {
bxp <- ggboxplot(df_join,
x = 'COND', y = colnames(df_join[i]))
print(
bxp + stat_pvalue_manual(comparisons[,i]) +
labs(
subtitle = get_test_label(unnested_aov[[i]], detailed = TRUE),
caption = get_pwc_label(comparisons[,i])))
Sys.sleep(1)
}
Since I'm getting back some error, I thing that the problem is that 'comparison' contains 36 rows and length does not fit well the number of the graph I should obtain (12).
I'll let you here the code below and I'll be open to your best suggestion in this concerns.
Thank yuo
> dput(head(df_join))
structure(list(ID = c("01", "01", "01", "04", "04", "04"), GR = c("RP",
"RP", "RP", "RP", "RP", "RP"), SES = c("V", "V", "V", "V", "V",
"V"), COND = structure(c(1L, 2L, 3L, 1L, 2L, 3L), .Label = c("NEG-CTR",
"NEG-NOC", "NEU-NOC"), class = "factor"), P3FCz = c(-11.6312151716924,
-11.1438413285935, -3.99591470944713, -0.314155675382471, 0.238885648959708,
5.03749946898385), P3Cz = c(-5.16524399006139, -5.53112490175437,
0.621502123415388, 2.23100741241039, 3.96990710862955, 7.75899775608441
), P3Pz = c(11.8802266972569, 12.1053426662461, 12.955441582096,
15.0981004360619, 15.4046229884164, 16.671036999147), LPPearlyFCz = c(-11.7785042972793,
-9.14927207125904, -7.58190508537766, -4.01515836011381, -6.60165385653499,
-2.02861964460179), LPPearlyCz = c(-5.96429031525769, -5.10918437158799,
-2.81732229625975, -1.43557366487622, -3.14872157912645, 0.160393685024631
), LPPearlyPz = c(8.23981597718437, 9.51261484648731, 9.42367409925817,
5.06332653216481, 5.02619159395405, 9.07903916629231), LPP1FCz = c(-5.67295796971287,
-4.3918290080777, -2.96652960658775, 0.159183652691071, -1.78361184935376,
1.97377908783621), LPP1Cz = c(-0.774461731301161, -0.650009462761383,
1.14010250644923, 1.51403741206392, 0.25571835554024, 3.76051565494304
), LPP1Pz = c(9.99385579756163, 11.1212652173052, 10.6989716871958,
3.7899021820967, 4.59413830322224, 8.52123662617732), LPP2FCz = c(-0.198736254963744,
-3.16101041766438, 0.895992279831378, 3.11042068112836, 2.27800090558473,
3.83846437952292), LPP2Cz = c(2.96437294922766, -2.12913230708907,
2.94619035115619, 3.44844607014521, 3.02403433835637, 4.7045767546583
), LPP2Pz = c(6.28027312932027, 5.24535230966772, 7.68162285335806,
1.08242973465635, 2.99896314000211, 5.36085942954182)), row.names = c(NA,
6L), class = "data.frame")
See Question&Answers more detail:
os