I wonder if there is a way to automatically calculate mean of one group / mean of another group
and prints the result to the plot like in the attached image. I also want the bigger number to go the numerator. (e.g. 1.82/1.4, not 1.4/1.82. I guess this would require an if
statement).
I have made a code (stat_box_data_1
) that automatically calculates some statistics. Would it be able to do mean of one group / mean of another group
in this same manner?
Also, I want to paste Mean
and a %
symbol with an arrow like in the attached. I was able to do all this using paste("Mean", 1.82/1.4, "%")
. But the problem with this is that I have to do this manually changing the numbers whenever there is new data.
# summary statistics
stat_box_data_1 <- function(y, upper_limit = max(data$mean)) {
return(
data.frame(
y = .74*upper_limit,
label = paste('count =', length(y), '\n',
' mean =', round(mean(y), 2), '\n',
' median =', round(median(y), 2), '\n',
'min =', round(min(y), 2), '\n',
'max =', round(max(y), 2), '\n'
)
)
)
}
# label
label1 = paste("Mean", 1.82/1.4, "%")
# boxplot
p1 <- ggboxplot(data, x = "group", y = "mean", color = "group") +
stat_summary(fun.data = stat_box_data_1, geom = "text", color = "black", size = 3) +
labs(x = "group", y = "mean", tag = "1") +
annotate("text", x= 2, y = 2.5, label = label1, size = 4, fontface = "bold", color = "red") +
annotate("text", x= 2.2, y = 2.55, label = sprintf('\u2191'), fontface = "bold", color = "red", size = 5)
p1[enter image description here][1]