TukeyHSD在双向anova之后的箱线图上得到结果

时间:2017-09-12 20:05:44

标签: r ggplot2 graphics boxplot tukey

我在单因素方差分析中有类似的代码正常工作,我的所有命令似乎都正常工作,但这些字母不是正在绘制的。我在我的控制台中没有收到任何错误,所以任何帮助都会受到赞赏!

我的数据:

> 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")

包(其中一些用于下游内容):

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

我的代码:

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))

以下代码应该产生此结果(加上重要字母): Plot

0 个答案:

没有答案