在 R (ggplot2) 的折线图中添加来自方差分析 + Tukey 事后分析的误差线
Adding Error bars from ANOVA + Tukey's posthoc in a Line Graph in R (ggplot2)
我正在尝试使用 ggplot2
将使用 Tukey 的 post-hoc 的 ANOVA 分析的误差线添加到我在 R 中的折线图中。提前致谢,我很乐意 post 任何有助于我解决此问题的信息!
到目前为止,这是我的代码:
#### ANOVA ####
aov_CW <- aov(CW ~ Subject, data = VisualAcuity)
summary(aov_CW)
model.tables(aov_CW, "means")
#### Tukey HSD post-hoc Test ####
TukeyHSD(aov_CW, conf.level = 0.95)
#### Line Plot ####
VisualAcuity$Subject <- as.factor(VisualAcuity$Subject)
VisualAcuity$Day <- as.factor(VisualAcuity$Day)
ggplot(data=VisualAcuity, aes(x=Day, y=CW, group = Subject)) +
geom_line(size=1, aes(color = Subject)) +
geom_point(size=2, shape=21, aes(color = Subject, fill = Subject)) +
ylim(0, max(1)) + ylab ('Visual Acuity (CW)') +
geom_errorbar(aes(ymin = CW - se, ymax = CW + se))
TukeyHSD
生成的值用于成对比较,因此无法绘制在您提供的代码之前的图表上。
但是,这里有一种使用 dplyr
添加标准错误 sd/sqrt(n)
条的方法。显然你不能为 1 次观察添加误差线,所以我用一些随机数扩展了你的数据集。
library(dplyr)
library(ggplot2)
VisualAcuity$Subject <- as.factor(VisualAcuity$Subject)
VisualAcuity$Day <- as.factor(VisualAcuity$Day)
VisualAcuity %>%
dplyr::group_by(Day,Subject) %>%
dplyr::summarize(Mean = mean(CW),std_err = sd(CW)/sqrt(n()), n = n()) %>%
ggplot(aes(x=Day, y=Mean, color = Subject, group = Subject)) +
geom_line(size=1,) +
geom_point(size=2, shape=21, aes(fill = Subject)) +
ylim(0, max(1)) + ylab ('Visual Acuity (CW)') +
geom_errorbar(aes(ymin = Mean - std_err, ymax = Mean + std_err))
数据
VisualAcuity <- structure(list(Subject = structure(c(1L, 1L, 1L, 2L, 2L, 2L,
3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L,
4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L), .Label = c("1",
"2", "3", "4"), class = "factor"), Day = structure(c(1L, 2L,
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L,
1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L,
2L, 3L), .Label = c("1", "2", "3"), class = "factor"), CW = c(0.528,
0.56, 0.486, 0.436, 0, 0.525, 0.622, 0.6, 0.522, 0.453, 0.5,
0.494, 0.566, 0.606, 0.56, 0.509, 0.054, 0.593, 0.645, 0.668,
0.56, 0.51, 0.508, 0.533, 0.518, 0.502, 0.413, 0.412, 0.042,
0.431, 0.582, 0.508, 0.435, 0.368, 0.417, 0.485)), row.names = c(NA,
-36L), class = "data.frame")
我正在尝试使用 ggplot2
将使用 Tukey 的 post-hoc 的 ANOVA 分析的误差线添加到我在 R 中的折线图中。提前致谢,我很乐意 post 任何有助于我解决此问题的信息!
到目前为止,这是我的代码:
#### ANOVA ####
aov_CW <- aov(CW ~ Subject, data = VisualAcuity)
summary(aov_CW)
model.tables(aov_CW, "means")
#### Tukey HSD post-hoc Test ####
TukeyHSD(aov_CW, conf.level = 0.95)
#### Line Plot ####
VisualAcuity$Subject <- as.factor(VisualAcuity$Subject)
VisualAcuity$Day <- as.factor(VisualAcuity$Day)
ggplot(data=VisualAcuity, aes(x=Day, y=CW, group = Subject)) +
geom_line(size=1, aes(color = Subject)) +
geom_point(size=2, shape=21, aes(color = Subject, fill = Subject)) +
ylim(0, max(1)) + ylab ('Visual Acuity (CW)') +
geom_errorbar(aes(ymin = CW - se, ymax = CW + se))
TukeyHSD
生成的值用于成对比较,因此无法绘制在您提供的代码之前的图表上。
但是,这里有一种使用 dplyr
添加标准错误 sd/sqrt(n)
条的方法。显然你不能为 1 次观察添加误差线,所以我用一些随机数扩展了你的数据集。
library(dplyr)
library(ggplot2)
VisualAcuity$Subject <- as.factor(VisualAcuity$Subject)
VisualAcuity$Day <- as.factor(VisualAcuity$Day)
VisualAcuity %>%
dplyr::group_by(Day,Subject) %>%
dplyr::summarize(Mean = mean(CW),std_err = sd(CW)/sqrt(n()), n = n()) %>%
ggplot(aes(x=Day, y=Mean, color = Subject, group = Subject)) +
geom_line(size=1,) +
geom_point(size=2, shape=21, aes(fill = Subject)) +
ylim(0, max(1)) + ylab ('Visual Acuity (CW)') +
geom_errorbar(aes(ymin = Mean - std_err, ymax = Mean + std_err))
数据
VisualAcuity <- structure(list(Subject = structure(c(1L, 1L, 1L, 2L, 2L, 2L,
3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L,
4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L), .Label = c("1",
"2", "3", "4"), class = "factor"), Day = structure(c(1L, 2L,
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L,
1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L,
2L, 3L), .Label = c("1", "2", "3"), class = "factor"), CW = c(0.528,
0.56, 0.486, 0.436, 0, 0.525, 0.622, 0.6, 0.522, 0.453, 0.5,
0.494, 0.566, 0.606, 0.56, 0.509, 0.054, 0.593, 0.645, 0.668,
0.56, 0.51, 0.508, 0.533, 0.518, 0.502, 0.413, 0.412, 0.042,
0.431, 0.582, 0.508, 0.435, 0.368, 0.417, 0.485)), row.names = c(NA,
-36L), class = "data.frame")