簡體   English   中英

在 R (ggplot2) 的折線圖中添加來自 ANOVA + Tukey 的 posthoc 的誤差線

[英]Adding Error bars from ANOVA + Tukey's posthoc in a Line Graph in R (ggplot2)

我正在嘗試使用ggplot2在 R 中將 ANOVA 分析中的誤差線與 Tukey 的事后添加到我的折線圖中。 在此先感謝,我很樂意發布更多信息來幫助我解決這個問題!

到目前為止,這是我的代碼:

#### 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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM