I have a dataset like the following.
Group <- c("A", "B", "A", "A", "A", "B", "A", "A", "B", "B","A", "A", "B", "B")
Score <- c(26, 22, 15, 5, 19, 3, 4, 5, 23, 3, 5, 2, 20, 4)
Order <- c("First", "First", "First", "Second", "First", "Second", "Second", "Second", "First", "Second", "Second", "Second", "First", "Second")
Data <- data.frame(Group, Score, Order)
Data
Group Score Order
1 A 26 First
2 B 22 First
3 A 15 First
4 A 5 Second
5 A 19 First
6 B 3 Second
7 A 4 Second
8 A 5 Second
9 B 23 First
10 B 3 Second
11 A 5 Second
12 A 2 Second
13 B 20 First
14 B 4 Second
I need to plot the differences in scores between each group, with effect sizes for each comparison (either Cohen's d or Hedge's g would work).
The code below gets me everything I want on the plot, but the effect size.
OrderComparison <- list(c("First", "Second"))
ggplot(Data, aes(Order, Score, fill=Order))+
stat_summary(geom = "bar", fun = mean, position = "dodge", color="black")+
stat_summary(geom = "errorbar", fun.data = mean_se, position = "dodge", width=.2)+
stat_compare_means(method = "t.test", comparisons = OrderComparison, label = "p.signif", position = "identity")+
facet_wrap(~Group, scales="fixed", strip.position = "bottom")+
theme_classic()+
theme(legend.position = "none")+
scale_fill_grey(start = .6, end = 1)

Question
What code (and/or package) do I need to automatically put an effect size on each comparison?
Any advice or guidance is appreciated.
library(effsize)
library(dplyr)
cohen=numeric()
for (i in LETTERS[1:2]) {
Group=filter(Data, Group==i)
treatment = filter(Group, Order=="First")%>%select(Score)%>%unlist()
control = filter(Group, Order=="Second")%>%select(Score)%>%unlist()
d = (c(treatment,control))
f = c(rep("Treatment", length(treatment)), rep("Control", length(control)))
c=cohen.d(treatment,control)
cohen[i]=c$estimate
}
effsize=data.frame(sz=c(rep(paste("Cohen's d:", cohen[1]),8), rep(paste("Cohen's d:", cohen[2]),6)), Group=c(rep("A", 8), rep("B", 6)))
ggplot(Data, aes(Order, Score, fill=Order))+
stat_summary(geom = "bar", fun = mean, position = "dodge", color="black")+
stat_summary(geom = "errorbar", fun.data = mean_se, position = "dodge", width=.2)+
stat_compare_means(method = "t.test", comparisons = OrderComparison, label = "p.signif", position = "identity")+
facet_wrap(~Group, scales="fixed", strip.position = "bottom")+
theme_classic()+
theme(legend.position = "none")+
scale_fill_grey(start = .6, end = 1)+
geom_text(aes(x=1.5,y=25, label=sz), data=effsize)
It manually calculates the Cohen's d for the two groups and puts it in the plot.

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With