R中的方差分析箱图上的Posthoc标签 [英] Posthoc labels on anova boxplot in R

查看:186
本文介绍了R中的方差分析箱图上的Posthoc标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一些数据并进行了方差分析和事后检验,我该如何制作一个箱形图来自动添加事后分类,而不必在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)

这里是用于运行简单的单向方差分析和所有计划外比较的事后测试的代码:

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

在这一点上,我想将"a"组中的x,"b"组中的y和"a,b"组中的z进行分类.我可以制作一个箱线图,但是您如何用字母对其进行注释?

boxplot(value~variable, data=data.2)

解决方案

如果您不介意使用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"

安装并加载ggplot2软件包:

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

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

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

接下来,添加文本标签:

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

最后,添加箱线图几何,然后将其清理一点:

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

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 outside of R?

For example, here are some data to get started:

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)

Here's code for running a simple one way ANOVA and all of the unplanned comparision post-hoc tests:

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

At this point, I want to classify x in group "a", y in group "b" and z in group "a,b". I can make a boxplot, but how do you annotate it with the letters?

boxplot(value~variable, data=data.2)

解决方案

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中的方差分析箱图上的Posthoc标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆