Anna-Sofie
Anna-Sofie

Reputation: 19

Create ggplot with mean and confidence interval

I have created a graph with a curve for each individual and a mean curve that is created the same way. I would like to have a confidence interval on my mean curve. How can I do this? Should the mean curve be created in a different way? This is my code until now:

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

And this is my data:

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

Upvotes: 2

Views: 1630

Answers (2)

TarJae
TarJae

Reputation: 78917

enter image description here

You can use geom = "ribbon" to get the 95% CI band to your mean line. Credits to stefan where the main logic is already answered!

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

Upvotes: 1

stefan
stefan

Reputation: 123828

This could be achieved like so:

  1. Instead of adding the mean as additional rows you could make a summary df using e.g. dplyr::summarize
  2. make use of stat_summay to compute the summary statistics on the fly as I do in my approach below and computed the condfidence interval as 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"))

Upvotes: 3

Related Questions