R 中方差分析箱线图的事后标签
如果我有一些数据并进行方差分析和事后测试,如何制作自动添加事后分类的箱线图,而不必在 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
此时,我想将 x 分类为组“a”,y 分类为组“ b”和 z 在组“a,b”中。我可以制作箱线图,但是如何用字母对其进行注释?
boxplot(value~variable, data=data.2)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不介意使用 ggplot2 包,以下是我制作图形的方法:
首先,使用文本标签向数据框 (data.2) 添加一列:
安装并加载 ggplot2 包:
了解以下代码:该图,我将逐步构建它。首先绘制三组中每组的均值:
接下来,添加文本标签:
最后,添加箱线图几何图形并稍微清理一下:
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:
Install and load the ggplot2 package:
To understand the code for the figure, I'll build it in steps. First just plot the means for each of the three groups:
Next, add the text labels:
Finally, add the boxplot geom and clean it up a little:
这会更简单
This would be simpler