R 添加任何内容时发生ggplot映射aes错误

R 添加任何内容时发生ggplot映射aes错误,r,ggplot2,R,Ggplot2,当我这样绘制条形图时: 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=

当我这样绘制条形图时:

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)) 


p如果您将整个代码与执行它所需的所有变量共享,那就太好了,这样我们就可以重现您的结果

无论如何,我认为您以错误的方式添加了ggplot函数。
labs()
facet\u wrap()
是类似于
geom\u bar()
geom\u errorbar()的函数。因此,您还应该添加它们,而不是将它们放入
geom\u bar()

我认为以下代码块可以解决您的问题:

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)')

p如果您将整个代码与执行它所需的所有变量共享,那就太好了,这样我们就可以重现您的结果

无论如何,我认为您以错误的方式添加了ggplot函数。
labs()
facet\u wrap()
是类似于
geom\u bar()
geom\u errorbar()的函数。因此,您还应该添加它们,而不是将它们放入
geom\u bar()

我认为以下代码块可以解决您的问题:

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)')

p不要将其放入
geom.*
函数中,只需在所有geom
+facet\u wrap(~Sex)
之后添加,@inscaven所说的内容也适用于“例如添加标题:”code中的
labs
。谢谢@inscaven!就这样!不要将其放入
geom.*
函数中,只需在所有geom
+facet.\u wrap(~Sex)
之后添加@inscaven所说的内容也适用于
labs
中的“例如添加标题:”code.谢谢@inscaven!就这样!
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)')