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 %>%
tidyr::pivot_longer(., -c(A, C, D),'s')%>%
mutate(s = fct_relevel(s,
c("E", "F", "G",
"H", "I", "J",
"K", "L", "M",
"N", "O", "P")) %>%
arrange(s) %>%
group_by(s) %>%
pairwise_t_test(
value ~ D, paired = TRUE,
p.adjust.method = "bonferroni"
) %>%
#dplyr::select(., -'s')%>%
print()
while the Anova statistics have been obtained:
res.aov <- df %>%
tidyr::pivot_longer(., -c(A, C, D),'s')%>%
mutate(s = fct_relevel(s,c("E", "F", "G",
"H", "I", "J",
"K", "L", "M",
"N", "O", "P")
)))%>%
arrange(s) %>%
group_by(s) %>%
anova_test(., value ~ D, dv = value, wid = A, within = D)%>%
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 = "D")
comparisons
for (i in 5:ncol(df)) {
bxp <- ggboxplot(df,
x = 'D', y = colnames(df[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 concern.
Thank you
structure(list(A = 1:20, C = c("Maybe", "Maybe", "Maybe", "Maybe",
"Maybe", "Maybe", "Maybe", "Maybe", "Maybe", "Maybe", "Maybe",
"Maybe", "Maybe", "Maybe", "Maybe", "Maybe", "Maybe", "Maybe",
"Maybe", "Maybe"), D = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L), .Label = c("new_value_for_8",
"new_value_for_6", "new_value_for_4"), class = "factor"), E = c(988.368784828308,
988.856158671407, 996.004085290553, 999.685844324618, 1000.23888564896,
1005.03749946898, 999.786378084971, 997.039675082569, 998.028313183065,
997.168905747014, 1001.09291198164, 993.307008354785, 1004.23849942428,
1002.98988896299, 1003.55106999008, 1009.57481668809, 1005.41677956183,
1001.70676077155, 993.869639239065, 997.170442654021), F = c(994.834756009939,
994.468875098246, 1000.62150212342, 1002.23100741241, 1003.96990710863,
1007.75899775608, 998.699806256246, 996.401009591011, 998.076594704249,
1002.19344184533, 1005.87900720863, 994.076210622421, 1002.44958531768,
1003.10043497883, 1001.65779442628, 1013.71182331817, 1006.86178446511,
1005.31481098188, 995.867593313022, 1000.16218228559), G = c(1011.88022669726,
1012.10534266625, 1012.9554415821, 1015.09810043606, 1015.40462298842,
1016.67103699915, 1003.13771453335, 999.9107434841, 1002.15365554737,
1013.67789244066, 1014.38627383064, 1006.86762877786, 1007.47946451329,
1008.93405130319, 1008.45962311068, 1023.4166601996, 1015.18680921429,
1009.97183712754, 1006.2675210718, 1010.14219845841), H = c(988.221495702721,
990.850727928741, 992.418094914622, 995.984841639886, 993.398346143465,
997.971380355398, 1004.4672957051, 1002.54036572775, 1002.2292388993,
999.116379988893, 997.364309124077, 997.937032776913, 1001.14544537612,
1002.08056674659, 1000.0422658299, 1013.29862597967, 1005.06669915366,
1003.93467692475, 1000.02290694207, 1004.31923128858), I = c(994.035709684742,
994.890815628412, 997.18267770374, 998.564426335124, 996.851278420874,
1000.16039368502, 1003.52155765272, 1002.1043798945, 1002.7069399281,
1005.49897156208, 1005.81171180245, 998.62698748611, 999.56563615154,
1002.87987510596, 998.728473297166, 1017.2093269366, 1007.79412746756,
1008.11964589961, 1004.9525336386, 1009.50695673265), J = c(1008.23981597718,
1009.51261484649, 1009.42367409926, 1005.06332653216, 1005.02619159395,
1009.07903916629, 1007.56089165218, 1005.49719893791, 1004.91476855238,
1013.03209535721, 1010.84145164945, 1005.86927622259, 1003.25309970443,
1004.68478802971, 1002.71096740085, 1025.56743956652, 1016.32418136177,
1013.09901927997, 1011.92002817369, 1014.69013052771), K = c(994.327042030287,
995.608170991922, 997.033470393412, 1000.15918365269, 998.216388150646,
1001.97377908784, 1003.17401220482, 1001.60211665164, 1002.27932356239,
1002.41479226363, 999.832076213262, 1001.37236796086, 1001.17012593697,
1001.40362599894, 999.964771265342, 1012.75282463779, 1008.65746780516,
1005.290878105, 999.464067607865, 1005.14963479715), L = c(999.225538268699,
999.349990537239, 1001.14010250645, 1001.51403741206, 1000.25571835554,
1003.76051565494, 1002.74763442988, 1001.09116707486, 1003.29833843754,
1006.55857216695, 1007.06029312947, 1000.60539548502, 999.637387760292,
1002.72729847885, 998.034039799405, 1016.5065564384, 1009.68783611392,
1009.47863905986, 1003.56318544047, 1009.23934223585), M = c(1009.99385579756,
1011.12126521731, 1010.6989716872, 1003.7899021821, 1004.59413830322,
1008.52123662618, 1006.34418311104, 1004.1077131243, 1004.94124365003,
1011.89121961563, 1010.13326381032, 1005.33467168056, 1001.04545904874,
1002.86650202467, 1000.45601490752, 1022.13789464831, 1016.25544969107,
1012.37379951646, 1008.53744587416, 1012.00856171947), N = c(999.801263745036,
996.838989582336, 1000.89599227983, 1003.11042068113, 1002.27800090558,
1003.83846437952, 1000.70169995102, 1001.75290674649, 998.660833714301,
1006.69246804854, 1004.7636391085, 1005.63873342951, 1001.37744267414,
1000.97339668679, 1001.98775658049, 1004.70492544978, 1012.11738595707,
1001.0458886613, 996.725751886115, 1003.17906097432), O = c(1002.96437294923,
997.870867692911, 1002.94619035116, 1003.44844607015, 1003.02403433836,
1004.70457675466, 999.880559826981, 1000.66826545719, 999.59436981446,
1007.32640154038, 1006.7506344557, 1001.59973104217, 1000.71689406196,
1001.15587576193, 999.988638552344, 1006.4489695839, 1010.51785193511,
1003.79329103591, 999.118472788132, 1004.99936090838), P = c(1006.28027312932,
1005.24535230967, 1007.68162285336, 1001.08242973466, 1002.99896314,
1005.36085942954, 1001.22060069797, 1000.43007709819, 1000.47666761108,
1008.73650967215, 1007.20593389744, 1004.57722295264, 998.66379615346,
998.711140983915, 999.452420534917, 1008.11715753014, 1013.30601537204,
1002.03237948844, 1002.88799699943, 1005.57921718108)), row.names = c(NA,
20L), class = "data.frame")