R中anova boxplot上的Posthoc标签

use*_*644 5 r boxplot anova posthoc

如果我有一些数据并进行ANOVA和事后测试,我如何制作一个自动添加事后分类的箱线图,而不是编辑R外的数字?

例如,以下是一些入门数据:

install.packages("reshape", dependencies=T)
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)
Run Code Online (Sandbox Code Playgroud)

这是运行简单的单向ANOVA和所有计划外比较事后测试的代码:

linear.model <- lm(value~variable, data=data.2)
anova(linear.model)

# Analysis of Variance Table
# Response: value
#           Df Sum Sq Mean Sq F value   Pr(>F)   
# variable   2 10.942  5.4710  5.8628 0.004087 **
# Residuals 87 81.185  0.9332     

TukeyHSD(aov(linear.model))

# Tukey multiple comparisons of means
# 95% family-wise confidence level
# Fit: aov(formula = linear.model)
# $variable
          # diff        lwr        upr     p adj
# y-x  0.8344105  0.2396705 1.42915051 0.0034468
# z-x  0.2593612 -0.3353788 0.85410126 0.5539050
# z-y -0.5750493 -1.1697893 0.01969078 0.0602975
Run Code Online (Sandbox Code Playgroud)

此时,我想对组"a"中的x,组"b"中的y和组"a,b"中的z进行分类.我可以制作一个箱形图,但你怎么用字母注释呢?

boxplot(value~variable, data=data.2)
Run Code Online (Sandbox Code Playgroud)

Jam*_*ers 6

如果您不介意使用ggplot2包,请按以下步骤操作:

首先,使用文本标签向数据框(data.2)添加一列:

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"
Run Code Online (Sandbox Code Playgroud)

安装并加载ggplot2包:

install.packages("ggplot2", dependencies=T)
library(ggplot2)
Run Code Online (Sandbox Code Playgroud)

要理解该图的代码,我将逐步构建它.首先只绘制三组中每一组的均值:

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

接下来,添加文本标签:

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

最后,添加boxplot geom并将其清理一下:

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()
Run Code Online (Sandbox Code Playgroud)

R anova boxplot标签