Home > Net >  How to add SD to a geom_line if it uses predictions that are in a separate table?
How to add SD to a geom_line if it uses predictions that are in a separate table?

Time:11-10

I have a data frame (data.frame.2) with my fitted values from a model and a data frame (data.frame.1) with my observations. The fitted values are shown with geom_line and the observations with geom_point. That works well. I have calculated SD values of the fitted values stored in data.frame.1, together with the fitted values.

This is working:

data.frame.1 %>%
 ggplot(aes(my_x, my_y, col = my_groups))  
 geom_point()  
 geom_line(
  data = data.frame.2,
  aes(y = .fitted)
  )

This is not working:

data.frame.1 %>%
 ggplot(aes(my_x, my_y, col = my_groups))  
 geom_point()  
 geom_line(
  data = data.frame.2,
  aes(y = .fitted)
  )  
 geom_errorbar(
  data = data.frame.2,
  aes(
   ymin = .fitted - SD.fitted,
   ymax = .fitted   SD.fitted
   )
  )

R complains that "my_y" is not provided, but that seems not to be a problem for geom_line, as data.frame.2 does not contain "my_y". What do I have to change?

I work with 2 data frames because "my_y" has 10 observations that are plotted as points. The corresponding .fitted plotted with geom_line has ~3000 values in order to get a smooth curve. I am very sorry that I could not provide a reprex this time.

CodePudding user response:

Have you tried moving the aes from ggplot() to geom_point() like this? I can't test if this works without some data

data.frame.1 %>%
 ggplot()  
 geom_point(aes(my_x, my_y, col = my_groups))  
 geom_line(
  data = data.frame.2,
  aes(y = .fitted)
  )  
 geom_errorbar(
  data = data.frame.2,
  aes(
   ymin = .fitted - SD.fitted,
   ymax = .fitted   SD.fitted
   )
  )
  • Related