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

标签 r ggplot2 tidyverse mean intervals

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

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

enter image description here

最佳答案

这可以像这样实现:

  1. 您可以使用例如将平均值添加为附加行来进行汇总 df ,而不是添加平均值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"))

关于r - 创建具有均值和置信区间的 ggplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67956706/

相关文章:

r - 检测日期列格式的中断/更改

r - 为什么 plotly-rendered 图表在 Mozilla 上不起作用

r - 使用 ggplot 绘制具有两个 y 刻度的图形

r - `stat_smooth()` : object 'C_crspl' not found 计算失败

r - Unstack lubridate 的间隔类

r - 使用 tidyverse 有条件地转置选择行

r - .local(drv, ...) 中的错误 : Failed to connect to database: Error: Can't connect to MySQL server on 'xx.143.13.xxx' (0)

r - 如何从 R 中的 sqldf 输出中获取列的总和?

r - 使用 ggplot 标记异常值

r - 更改 ggplot2 中刻度的位置(在绘图内)