Posthoc labels on anova boxplot in R

前端 未结 2 1309
半阙折子戏
半阙折子戏 2021-01-01 02:45

If I have some data and do an ANOVA and post-hoc tests, how do I make a boxplot that adds the post-hoc classification automatically, rather than having to edit the figure ou

2条回答
  •  庸人自扰
    2021-01-01 03:22

    If you don't mind using the ggplot2 package, here's how I would make the figure:

    First, add a column to your data frame (data.2) with the text labels:

    data.2$posthoc[data.2$variable == "x"] <- "a"
    data.2$posthoc[data.2$variable == "y"] <- "b"
    data.2$posthoc[data.2$variable == "z"] <- "a,b"
    

    Install and load the ggplot2 package:

    install.packages("ggplot2", dependencies=T)
    library(ggplot2)
    

    To understand the code for the figure, I'll build it in steps. First just plot the means for each of the three groups:

    qplot(data=data.2,
        x = variable,
        y = value,
        stat = "summary",
        fun.y = "mean",
        geom = c("point")
        )
    

    Next, add the text labels:

    qplot(data=data.2,
        x = variable,
        y = value,
        stat = "summary",
        fun.y = "mean",
        label = posthoc,
        vjust = -12,
        geom = c("point", "text")
        )
    

    Finally, add the boxplot geom and clean it up a little:

    qplot(data=data.2,
        x = variable,
        y = value,
        stat = "summary",
        fun.y = "mean",
        label = posthoc,
        vjust = -12,
        ylim = c(-1, 3.5),
        geom = c("point", "text"),
        main="ggplot2 ANOVA boxplot"
        ) + 
        geom_boxplot(aes(fill=posthoc)) + 
        theme_bw()
    

    R anova boxplot with labels

提交回复
热议问题