Home > database >  how to generate multiple geom_errorbar for each geom_line in ggplot?
how to generate multiple geom_errorbar for each geom_line in ggplot?

Time:09-16

I'd like to generate multiple series on a single plot and to display the geom_errorbar for each geom_line and geom_point. Something very similar to this:

enter image description here

therefore I have written:

library(ggplot2)
raw_data <- data.frame(x = seq(10),
                     y1 = sample(x=20,size=10), # c(1.1, 2.4, 3.5, 4.1, 5.9, 6.7, 7.1, 8.3, 9.4, 10.0)
                     y2 = sample(x=20,size=10),
                     y3 = sample(x=20,size=10),
                     y4 = sample(x=20,size=10),
                     se1 = runif(n=10,min=0,max=1),
                     se2 = runif(n=10,min=0,max=1),
                     se3 = runif(n=10,min=0,max=1),
                     se4 = runif(n=10,min=0,max=1))

ggplot(gfg_data, aes(x, y=y1))   
geom_point(size = 4, shape = 17)  
geom_line(aes(y = y1), color = "black")  
geom_errorbar(aes(ymax = y1   se1,ymin = y1 - se1), colour = "black", width=0.17)

ggplot(gfg_data, aes(x, y=y2))   
geom_point(size = 4, shape = 15)  
geom_line(aes(y = y2), color = "red")  
geom_errorbar(aes(ymax = y2   se2,ymin = y2 - se2), colour = "red", width=0.17)
...

but it fails, can somebody please suggest a way how to write this so it would work and perhaps in a more elegant way. Thank you in advance!

CodePudding user response:

ggplot2 expects your data to be in a long or narrow data format. In your case this means you need the following four columns:

  1. x-coordinate
  2. variable (1 to 4 in your data)
  3. y-value
  4. se-value

You then tell ggplot that colours are based on the variable column to generate a separate trace for each variable. In R code, using the data you provided:

# Load libraries
library(ggplot2)
library(tidyverse)

# Generate data
set.seed(1)
raw_data <- data.frame(x = seq(10),
                       y1 = sample(x=20,size=10), # c(1.1, 2.4, 3.5, 4.1, 5.9, 6.7, 7.1, 8.3, 9.4, 10.0)
                       y2 = sample(x=20,size=10),
                       y3 = sample(x=20,size=10),
                       y4 = sample(x=20,size=10),
                       se1 = runif(n=10,min=0,max=1),
                       se2 = runif(n=10,min=0,max=1),
                       se3 = runif(n=10,min=0,max=1),
                       se4 = runif(n=10,min=0,max=1))

# Convert to a long format, in which each variable has a y-value and an se
long_data <- raw_data %>%
  pivot_longer(!x,
               names_pattern = "([[:alpha:]] )([0-9])",
               names_to = c("stat", "variable")) %>%
  pivot_wider(names_from = stat, 
              values_from = value)

  
# Plot 
long_data %>%
  ggplot(aes(x = x,
           y = y,
           colour = variable,
           shape = variable))  
  geom_point()  
  geom_line()  
  geom_errorbar(aes(ymax = y se, ymin = y-se), width = 0.17)

Created on 2022-09-14 by the reprex package (v2.0.1)

  • Related