Adding Tukey's significance letters to boxplot
Asked Answered
S

1

6

I'm trying to create a boxplot with ggplot of a count (MedMean) on yaxis and various independent samples (Site_Name) on xaxis.

ggplot(medianlist,aes(x=reorder(Site_Name,MedMean,FUN=median),y=MedMean))+
geom_boxplot()

I want to add Tukey's significance letters to the boxes.

Thanks

Shotten answered 5/2, 2018 at 15:14 Comment(4)
How about a reproducible example?Gratulation
ggplot(iris,aes(x=reorder(Species,Petal.Length,FUN = median),y=Petal.Length))+geom_boxplot()Shotten
This is the same function using the "iris" file. I want to add letters showing significance between species (in this example)Shotten
Possibly related: https://mcmap.net/q/1779230/-tukeys-post-hoc-on-ggplot-boxplot/783245Reba
R
5

Using agricolae::HSD.test you can do

library(dplyr)
library(agricolae)
library(ggplot2)
iris.summarized = iris %>% group_by(Species) %>% summarize(Max.Petal.Length=max(Petal.Length))
hsd=HSD.test(aov(Petal.Length~Species,data=iris), "Species", group=T)
ggplot(iris,aes(x=reorder(Species,Petal.Length,FUN = median),y=Petal.Length))+geom_boxplot()+geom_text(data=iris.summarized,aes(x=Species,y=0.2+Max.Petal.Length,label=hsd$groups$groups),vjust=0)

enter image description here

Reba answered 24/4, 2018 at 1:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.