Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Main barplot with one dataframe and add errorbar with other dataframe

I have two dataframes first one:

enter image description here

Second one:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

I used the first dataframe to create bar graph like below.

enter image description here

Though I failed to put error bars on it using the se values in each Congruency group.

This is my code below:

ggplot(AAData_cong, aes(x=Congruency, y=joystick_reaction, fill = as.factor(Congruency)))+
  geom_bar(position=position_dodge(.5), size=.75,  width=0.5, colour="black", stat= "summary", fun= "mean") +
  geom_line(aes(group=participant), linetype = "dashed") + 
  geom_point(aes(group=participant), shape = 21, color = 'black')+
  # geom_errorbar(aes(ymin=RT_Cong$joystick_reaction-RT_Cong$se, ymax=RT_Cong$joystick_reaction+RT_Cong$se), width=.2,
  #                position=position_dodge(0.05))+
  theme_classic(base_size=18)+
  coord_cartesian(ylim = c(700, 1200))+ # set the y limits
  ylab("RT (ms)")+labs(fill="j")+
  theme(axis.title.y = element_text(size = rel(1), angle = 90,  vjust=1.25))+
  theme(axis.title.x = element_text(size = rel(1), angle = 0,  vjust=0))+
  theme(axis.text.x = element_text(size=rel(1)))+
  theme(axis.text.y = element_text(size=rel(1)))+theme(legend.position = "none")+
  theme(strip.text.x = element_text(size=9))+
  # from set 1 color brewer
  scale_fill_manual(values=c("#0571b0","#ca0020"))

Thank you in advance!

>Solution :

df1 is the first dataframe
df2 is the second one with sd and se:

Just add this to your code:

  geom_errorbar(data = df2, aes(ymin=joystick_reaction-sd, ymax= joystick_reaction  +sd), width=.2, position=position_dodge(.9)) 
library(tidyverse)

ggplot(df1, aes(x=Congruency, y=joystick_reaction, fill = as.factor(Congruency)))+
  geom_bar(position=position_dodge(.5), size=.75,  width=0.5, colour="black", stat= "summary", fun= "mean") +
  geom_line(aes(group=participant), linetype = "dashed") + 
  geom_point(aes(group=participant), shape = 21, color = 'black')+
  # geom_errorbar(aes(ymin=RT_Cong$joystick_reaction-RT_Cong$se, ymax=RT_Cong$joystick_reaction+RT_Cong$se), width=.2,
  #                position=position_dodge(0.05))+
  theme_classic(base_size=18)+
 # coord_cartesian(ylim = c(700, 1200))+ # set the y limits
  ylab("RT (ms)")+labs(fill="j")+
  theme(axis.title.y = element_text(size = rel(1), angle = 90,  vjust=1.25))+
  theme(axis.title.x = element_text(size = rel(1), angle = 0,  vjust=0))+
  theme(axis.text.x = element_text(size=rel(1)))+
  theme(axis.text.y = element_text(size=rel(1)))+theme(legend.position = "none")+
  theme(strip.text.x = element_text(size=9))+
  # from set 1 color brewer
  scale_fill_manual(values=c("#0571b0","#ca0020"))+
  geom_errorbar(data = df2, aes(ymin=joystick_reaction-sd, ymax= joystick_reaction  +sd), width=.2, position=position_dodge(.9)) 

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading