【问题标题】:Create ggplot with mean and confidence interval创建具有均值和置信区间的 ggplot
【发布时间】: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")))

【问题讨论】:

    标签: r ggplot2 tidyverse mean intervals


    【解决方案1】:

    这可以这样实现:

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

      【讨论】:

      • 非常感谢!!我花了很多时间试图解决这个问题,现在终于奏效了。
      猜你喜欢
      • 1970-01-01
      • 2020-08-04
      • 1970-01-01
      • 1970-01-01
      • 2015-09-14
      • 2012-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多