创建具有均值和置信区间的 ggplot

Posted

技术标签:

【中文标题】创建具有均值和置信区间的 ggplot【英文标题】:Create ggplot with mean and confidence interval 【发布时间】:2021-08-29 13:26:48 【问题描述】:

我创建了一个图表,其中包含每个人的曲线和以相同方式创建的平均曲线。我想在我的平均曲线上有一个置信区间。我怎样才能做到这一点?是否应该以不同的方式创建平均曲线? 到目前为止,这是我的代码:

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

【问题讨论】:

【参考方案1】:

这可以这样实现:

    除了将平均值添加为附加行之外,您还可以使用例如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"))

【讨论】:

【参考方案2】:

您可以使用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"))

【讨论】:

非常感谢!!我花了很多时间试图解决这个问题,现在终于奏效了。

以上是关于创建具有均值和置信区间的 ggplot的主要内容,如果未能解决你的问题,请参考以下文章

Python Matplotlib 在条形图中绘制样本均值,具有置信区间,但看起来像箱形图

怎么用matlab计算一组数据的总体均值和标准差σ的95%置信区间

如何求标准差的置信区间

均值的置信区间 - scipy 实现与数学公式不符

计算平均值的95%置信区间

两个数据集之间均值差的置信区间