B.Shermeister
B.Shermeister

Reputation: 557

Adding Tukey's significance letters to boxplot

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

Upvotes: 6

Views: 15205

Answers (1)

Marcus Ritt
Marcus Ritt

Reputation: 477

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

Upvotes: 5

Related Questions