我创建了一个图表,其中包含每个人的曲线和以相同方式创建的平均曲线。我想在我的平均曲线上有一个置信区间。我怎样才能做到这一点?是否应该以不同的方式创建平均曲线? 这是我到目前为止的代码:
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")))
最佳答案
这可以像这样实现:
- 您可以使用例如将平均值添加为附加行来进行汇总 df ,而不是添加平均值
dplyr::summarize
- 利用
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"))
关于r - 创建具有均值和置信区间的 ggplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67956706/