0

When I plot barplot like this:

p<- ggplot(corttestunitedcol, aes(x=Sex, y=mean, fill=Treatment_Status)) + 
  geom_bar(stat="identity", color="black", 
           position=position_dodge()) +
  geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=.2,
                position=position_dodge(.9)) 

beautiful. What I want.

However, if I add anything to this, it get

Error: mapping must be created by aes()

For example adding titles:

p<- ggplot(corttestunitedcol, aes(x=Sex, y=mean, fill=Treatment_Status)) + 
  geom_bar(stat="identity", color="black", 
           position=position_dodge(),
           labs(title = "Corticosterone",
                y = "mean plasma Corticosterone (pg/ml)", x = "")) +
  geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=.2,
                position=position_dodge(.9)) 

Or

p<- ggplot(corttestunitedcol, aes(x=Sex, y=mean, fill=Treatment_Status)) + 
  geom_bar(stat="identity", color="black", 
           position=position_dodge(),
          facet_wrap(~Sex)) +
  geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=.2,
                position=position_dodge(.9)) 

How do I fix this?

3
  • 4
    don't put it into geom_* function, just add after all geoms + facet_wrap(~Sex) Commented Sep 1, 2020 at 11:48
  • 1
    What @inscaven said is also valid for the labs in the "For example adding titles:" code. Commented Sep 1, 2020 at 12:16
  • thank you @inscaven ! That was it! Commented Sep 1, 2020 at 15:05

1 Answer 1

2

It would be nice if you've shared the whole code with all the necessary variables to execute it, then we could reproduce your results.

Anyway, I think you're adding ggplot functions in the wrong way. labs() and facet_wrap() are functions just like geom_bar() and geom_errorbar(). Therefore, you should also add them, instead of throwing them into geom_bar().

I think the following code chunk would solve your problem:

p <- ggplot(corttestunitedcol, aes(x = Sex, y = mean, fill = Treatment_Status)) + 
  geom_bar(stat = 'identity', colour = 'black',
           position = position_dodge()) +
  geom_errorbar(aes(ymin = mean - sd, ymax = mean + sd),
                width = 0.2, position = position_dodge(0.9)) +
  facet_wrap(. ~ Sex) +
  labs(title = 'Corticosterone',
       x = '', y = 'mean plasma Corticosterone (pg/ml)')

Let us know if your problem was solved.

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

Comments

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.