Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding SEM bars to ggline graph when SEM is already calculated in dataset

I have a data set that looks like this

well    time        mm          sem
x       basal       83.96792    8.068338
x       stimulus1   153.17250   8.338465
x       recovery    60.45712    5.97283
x       stimulus2   154.26550   6.533665

and a graph that is made from this code

    ggline(df, x = 'time', y = 'mm', color = 'well', palette = c("#00AFBB", "#E7B800")) + 
  theme_grey()

I want to add SEM bars to each of the four data points with my already calculated SEM number (each of the four points is a sum of a duration so that's how I have an SEM for each point) to make the graph look like this:

any idea on how to go about that?

like image 731
neuroandstats Avatar asked Sep 07 '25 21:09

neuroandstats


1 Answers

You can write:

ggplot(df, aes(x  = time, y = mm, group = well)+
   geom_point()+
   geom_line()+
   geom_errorbar(aes(ymin = mm-sem, ymax = mm+sem), width = 0.2)

So, it should look like:

enter image description here

But based on your dataframe and your code, I think you are more looking for something like this:

ggplot(df, aes(x = factor(time,unique(time)), y =mm, group = well, color = time))+
  geom_point()+
  geom_errorbar(aes(ymin = mm-sem, ymax = mm+sem), width = 0.2)+
  scale_color_manual(values = rep(c("#00AFBB", "#E7B800"),2))

enter image description here

Does it answer your question ?


Reproducible example

structure(list(well = c("x", "x", "x", "x"), time = c("basal", 
"stimulus1", "recovery", "stimulus2"), mm = c(83.96792, 153.1725, 
60.45712, 154.2655), sem = c(8.068338, 8.338465, 5.97283, 6.533665
)), row.names = c(NA, -4L), class = c("data.table", "data.frame"
))
like image 139
dc37 Avatar answered Sep 09 '25 12:09

dc37