Posthoc labels on anova boxplot in R

前端 未结 2 1294
半阙折子戏
半阙折子戏 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

    0 讨论(0)
  • 2021-01-01 03:43

    This would be simpler

    library(reshape)
    
    x <- rnorm(30)
    y <- rnorm(30)+1
    z <- rnorm(30)+0.5
    
    data.1 <- data.frame(x, y, z)
    data.2 <- melt(data.1)
    data.2$newgroup = factor(data.2$variable,labels=c("a","b","ab")) # only line added
    boxplot(value~newgroup, data=data.2)
    
    0 讨论(0)
提交回复
热议问题