Is there a function to add AOV post-hoc testing results to ggplot2 boxplot?

前端 未结 1 1425
小鲜肉
小鲜肉 2020-12-13 16:37

I\'d like to add results of a Tukey.HSD post-hoc test to a ggplot2 boxplot. This SO answer contains a manual example of what I want (i.e., the letters on the pl

相关标签:
1条回答
  • 2020-12-13 16:49

    You can use 'multcompLetters' from the 'multcompView' package to generate letters of homologous groups after a Tukey HSD test. From there, it's a matter of extracting the group labels corresponding to each factor tested in the Tukey HSD, as well as the upper quantile as displayed in the boxplot in order to place the label just above this level.

    library(plyr)
    library(ggplot2)
    library(multcompView)
    
    set.seed(0)
    lev <- gl(3, 10)
    y <- c(rnorm(10), rnorm(10) + 0.1, rnorm(10) + 3)
    d <- data.frame(lev=lev, y=y)
    
    a <- aov(y~lev, data=d)
    tHSD <- TukeyHSD(a, ordered = FALSE, conf.level = 0.95)
    
    generate_label_df <- function(HSD, flev){
     # Extract labels and factor levels from Tukey post-hoc 
     Tukey.levels <- HSD[[flev]][,4]
     Tukey.labels <- multcompLetters(Tukey.levels)['Letters']
     plot.labels <- names(Tukey.labels[['Letters']])
    
     # Get highest quantile for Tukey's 5 number summary and add a bit of space to buffer between    
     # upper quantile and label placement
        boxplot.df <- ddply(d, flev, function (x) max(fivenum(x$y)) + 0.2)
    
     # Create a data frame out of the factor levels and Tukey's homogenous group letters
      plot.levels <- data.frame(plot.labels, labels = Tukey.labels[['Letters']],
         stringsAsFactors = FALSE)
    
     # Merge it with the labels
       labels.df <- merge(plot.levels, boxplot.df, by.x = 'plot.labels', by.y = flev, sort = FALSE)
    
    return(labels.df)
    }
    

    Generate ggplot

     p_base <- ggplot(d, aes(x=lev, y=y)) + geom_boxplot() +
      geom_text(data = generate_label_df(tHSD, 'lev'), aes(x = plot.labels, y = V1, label = labels))
    

    Boxplot with automatic Tukey HSD group label placement

    0 讨论(0)
提交回复
热议问题