Catherine
Catherine

Reputation: 161

Main barplot with one dataframe and add errorbar with other dataframe

I have two dataframes first one:

enter image description here

Second one:

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!

Upvotes: 2

Views: 29

Answers (1)

TarJae
TarJae

Reputation: 79174

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

Upvotes: 1

Related Questions