I have a data frame, and the head looks like below, and there are more rows with different names and readings:
df <- data.frame ("name" = c("A", "A", "A", "B", "B", "B", "C", "C", "C"),
"reading" = c(3.6, 1.2, 3.1, 5.5, 6.1, 6.3, 9.3, 9.1, 8.9))
I use the following code to produce a bar graph:
ggplot(aes(x=name, y=reading))+geom_col()+stat_summary(fun.data = mean_se, geom = "errorbar", color="red")+stat_summary(fun.y=mean, geom="point", color="red")
I then performed ANOVA anlysis/TukeyHSD separately, but I could not add the letters to the graph showing if each two name-pairs are statistically different. May I ask if this be achieved by the ggplot queries without performing separate ANOVA/Tukey test?
Thank you very much!
Try this one:
library(tidyverse)
stat_pvalue <- df %>%
rstatix::tukey_hsd(reading~name) %>%
rstatix::add_y_position(fun = "max")
ggplot(df, aes(x=name, y=reading))+
stat_summary(fun=mean, geom="bar") +
stat_summary(fun.data = mean_se, geom = "errorbar", color="red")+
ggpubr::stat_pvalue_manual(stat_pvalue, label = "p.adj.signif") +
ggpubr::stat_compare_means(method = "anova")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.