[英]Create ggplot with mean and confidence interval
我創建了一個圖表,其中包含每個人的曲線和以相同方式創建的平均曲線。 我想在我的平均曲線上有一個置信區間。 我怎樣才能做到這一點? 是否應該以不同的方式創建平均曲線? 到目前為止,這是我的代碼:
DNAMorfR %>%
drop_na(`Normal morphology (%)`) %>%
ggplot(aes(x = Time, y = `Normal morphology (%)`, linetype = Patient, color = Patient, group
= Patient, na.rm = TRUE)) +
geom_line(size = 1) +
theme_minimal() + ggtitle("(A1) Normal morphology") +
geom_point(size = 1.5) +
scale_y_continuous(limits = c(0, 25), breaks=seq(0, 25, by = 5)) +
geom_hline(yintercept = 4, color = "grey", size = 1) +
scale_color_manual(values = c("black", "#FF3333", "#FF9933", "#CC9900"))
這是我的數據:
data.frame(
stringsAsFactors = FALSE,
check.names = FALSE,
Patient = c("1","1","1","2","2","2","3","3","3","mean","mean","mean"),
`Normal morphology (%)` = c(7, 2, 3, 1, 3, 3, 6, 7, 8, 7, 9, 8),
Time = as.factor(c("Week 1","Week 2","Week 3","Week 1","Week 2","Week 3","Week 1","Week 2",
"Week 3","Week 1","Week 2","Week 3")))
這可以像這樣實現:
dplyr::summarize
來制作摘要 df,而不是將平均值添加為附加行stat_summay
計算匯總統計數據,並將置信區間計算為mean(x) +/- 1.96 / (length(x) - 1) * sd(x)
library(ggplot2)
library(tidyr)
library(dplyr)
DNAMorfR1 <- DNAMorfR %>%
drop_na(`Normal morphology (%)`) %>%
filter(Patient != "mean")
ggplot(DNAMorfR1, aes(x = Time, y = `Normal morphology (%)`)) +
geom_line(aes(linetype = Patient, color = Patient, group = Patient), size = 1) +
geom_point(aes(color = Patient, group = Patient), size = 1.5) +
stat_summary(aes(color = "mean", linetype = "mean", group = "mean"), geom = "line", fun = "mean") +
stat_summary(aes(color = "mean", group = "mean"), geom = "pointrange", fun = "mean",
fun.min = function(x) mean(x) - 1.96 / (length(x) - 1) * sd(x),
fun.max = function(x) mean(x) + 1.96 / (length(x) - 1) * sd(x), show.legend = FALSE) +
theme_minimal() +
ggtitle("(A1) Normal morphology") +
scale_y_continuous(limits = c(0, 25), breaks=seq(0, 25, by = 5)) +
geom_hline(yintercept = 4, color = "grey", size = 1) +
scale_color_manual(values = c("black", "#FF3333", "#FF9933", "#CC9900"))
您可以使用geom = "ribbon"
將 95% CI 帶置於您的平均線。 感謝 stefan,其中主要邏輯已經得到解答!
DNAMorfR %>%
drop_na(`Normal morphology (%)`) %>%
filter(row_number() <= n()-3) %>%
ggplot(aes(x = Time, y = `Normal morphology (%)`)) +
geom_line(aes(linetype = Patient, color = Patient, group = Patient), size = 1) +
geom_point(aes(color = Patient, group = Patient), size = 2) +
stat_summary(aes(color = "mean", linetype = "mean", group = "mean"), size=1.5, geom = "line", fun = "mean") +
stat_summary(aes(color = "mean", group = "mean"), geom = "ribbon", fun = "mean", size= 0.5, alpha=0.1,
fun.min = function(x) mean(x) - 1.96 / (length(x) - 1) * sd(x),
fun.max = function(x) mean(x) + 1.96 / (length(x) - 1) * sd(x), show.legend = FALSE) +
theme_minimal() +
ggtitle("(A1) Normal morphology") +
scale_y_continuous(limits = c(0, 25), breaks=seq(0, 25, by = 5)) +
geom_hline(yintercept = 4, color = "grey", size = 1) +
scale_color_manual(values = c("black", "#FF3333", "#FF9933", "#CC9900"))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.