0

My dataframe (df) looks like this:

   df
 me  SD class  
 3.11 4.08  A
 2.09 3.50  B
 1.75 2.72  C
 0.34 0.85  D

Where me is the mean and SD is the standard deviation. I want to create a barplot of these mean values for each class and add the standard deviation on top of those bars:

To plot the bars:

ggplot(data=df) +
  geom_bar(aes(x=class,y= me), 
           stat="identity",position="dodge")+ 
  ylab("mean")+ 
  xlab("class")+ 
  theme(
    text = element_text(size=20, colour="black"),  
    axis.text.x = element_text(angle=90, vjust=1, colour="black"), 
    axis.text.y = element_text(colour="black")) + 
  scale_y_continuous(breaks = round(seq(-2, 5, by = 0.5),1))

This works fine.

Now add the error-bar:

ggplot(data=df3) +
  geom_bar(aes(x=class,y= me), 
           stat="identity",position="dodge")+ 
  ylab("mean")+   
  xlab("class")+ 
  theme(
    text = element_text(size=20, colour="black"),  
    axis.text.x = element_text(angle=90, vjust=1, colour="black"), 
    axis.text.y = element_text(colour="black"))+ 
  scale_y_continuous(breaks = round(seq(-2, 5, by = 0.5),1))+ 
  geom_errorbar(aes(ymax = me + SD, ymin= me - SD), 
                position=position_dodge(0.9))

I got this error:

   Error in data.frame(list(ymin = c(-1.96681604736652, -1.40293149619775,  : 
    arguments imply differing number of rows: 28, 102   

1 Answer 1

2

Define x and y wthin the ggplot:

ggplot(data=df, aes(x = class, y = me)) + 
geom_bar(stat="identity",position="dodge")+ ylab("mean")+   
xlab("class") + theme(text = element_text(size=20, colour="black"),  
axis.text.x = element_text(angle=90, vjust=1, colour="black"), 
axis.text.y = element_text(colour="black"))+ scale_y_continuous(breaks 
= round(seq(-2, 5, by = 0.5),1)) + geom_errorbar(aes(ymax = me + SD, 
ymin= me - SD), position=position_dodge(0.9))

enter image description here

Alternatively:

ggplot(data=df, aes(x = class, y = me)) + 
    geom_bar(stat="identity",position="dodge")+ ylab("mean")+   
    xlab("class") + theme(text = element_text(size=20, colour="black"),  
                          axis.text.x = element_text(angle=90, vjust=1, colour="black"), 
                          axis.text.y = element_text(colour="black"))+ scale_y_continuous(breaks 
                                                                                          = round(seq(-2, 5, by = 0.5),1)) + geom_errorbar(aes(ymax = SD, 
                                                                                                                                               ymin= me - (SD-me)), position=position_dodge(0.9))

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

They are plotted as you defined them in the geom_errorbar function. Do you want to include the numbers in the plot?
What about the second version?
It creates a symmetric range

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.