TukeyHSD results on boxplot after two-way anova

1k views Asked by At

I've had similar code working fine on a one-way ANOVA and all of my commands appear to be working, but the letters aren't plotting. I'm not getting any errors in my console, so any help would be appreciated!

My data:

> dput(BodyComp)
structure(list(TimePoint = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("5", "10"
), class = "factor"), LayingSubstrate = structure(c(2L, 2L, 2L, 
2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("CheeseCloth", 
"Log"), class = "factor"), FreshMass = c(1.1854, 1.423, 1.122, 
0.6426, 0.3207, 0.3956, 0.4856, 0.8944, 1.9665, 2.2618, 0.9236, 
1.8103, 2.2504, 1.378, 1.3022, 1.3059), DryMass = c(0.4996, 0.6065, 
0.465, 0.2394, 0.1133, 0.1398, 0.1654, 0.3198, 0.8455, 0.9618, 
0.3735, 0.746, 0.8512, 0.5436, 0.5042, 0.5171), WaterContent = c(57, 
57.48, 59.56, 58.79, 62.18, 60.55, 61.28, 60.4, 57.85, 57.38, 
58.56, 62.75, 64.67, 64.66, 65.94, 64.24), LipidContent = c(39.91, 
35.524, 40.344, 37.035, 38.596, 27.938, 35.466, 32.623, 37.976, 
38.999, 44.252, 43.133, 38.462, 33.54, 40.078, 32.863)), .Names = c("TimePoint", 
"LayingSubstrate", "FreshMass", "DryMass", "WaterContent", "LipidContent"
), row.names = c(NA, -16L), class = "data.frame")

Packages (some of these are for downstream stuff):

library(ggplot2)
library(multcompView)
library(plyr)
library(gridExtra)
library(cowplot)

My code:

BodyComp$TimePoint <- factor(BodyComp$TimePoint, levels=c("5", "10"))

## Figure formatting (Theme modification)
Alex_Theme = theme_bw() +
  theme(plot.title = element_text(hjust = 0.5, face='bold')) +
  theme(plot.title = element_text(vjust=-1.8)) +
  theme(panel.border = element_rect(fill=NA, colour = "black", size=1)) +
  theme(axis.text = element_text(face="bold", size = 10)) +
  theme(axis.title.x = element_text(margin = margin(t = 10, r = 20, b = 0, l = 0))) +
  theme(axis.title = element_text(face="bold", size = 12))

## Dry mass linear model and ANOVA
DryMassmodel=lm(DryMass ~ TimePoint + LayingSubstrate + TimePoint:LayingSubstrate, data = BodyComp)
DryMassANOVA=aov(DryMassmodel)

# Tukey test to study each pair of treatment :
DryMassTUKEY <- TukeyHSD(x=DryMassANOVA, conf.level=0.95)

## Function to generating Tukey HSD labels for boxplot
generate_label_df <- function(DryMassTUKEY, variable){

  # Extract labels and factor levels from Tukey post-hoc 
  Tukey.levels <- DryMassTUKEY[[variable]][,4]
  Tukey.labels <- data.frame(multcompLetters(Tukey.levels)['Letters'])

  #I need to put the labels in the same order as in the boxplot :
  Tukey.labels$treatment=rownames(Tukey.labels)
  Tukey.labels=Tukey.labels[order(Tukey.labels$treatment) , ]
  return(Tukey.labels)
}

labels<-generate_label_df(DryMassTUKEY , "TimePoint:LayingSubstrate") #generate labels using function

names(labels)<-c('Letters','TimePoint') #rename columns for merging

DryMassyvalue<-aggregate(.~TimePoint, data=BodyComp, max) # obtain letter position for y axis using means

DryMassfinal<-merge(labels,DryMassyvalue) #merge dataframes


## Dry mass box plot with Tukey HSD results
DryMassPlot <- ggplot(BodyComp, aes(x = TimePoint, y = DryMass, fill=LayingSubstrate)) +
  Alex_Theme +
  labs(x = 'Time (weeks)', y = 'Dry Mass (g)') +
  ggtitle(expression(atop(bold("Dry Mass")))) +
  geom_boxplot(stat = "boxplot", position = position_dodge(width=1)) +
  geom_text(data = DryMassfinal, aes(x = TimePoint, y = DryMass, label = Letters),vjust=-2,hjust=.5) +
  theme(legend.title.align=0.5, legend.position=c(0.165, 0.2), legend.background = element_rect(size=0.5, linetype="solid", colour ="black")) +
  scale_fill_manual(values=c("#E69F00", "#3399CC"), name = "Substrate", labels = c("Cheese Cloth", "Log")) +
  scale_y_continuous(limit = c(0, 1.2), breaks = c(0, 0.25, 0.5, 0.75, 1.0))

The following code should produce this result (plus the significance letters): Plot

0

There are 0 answers