分组条形图的平均值

时间:2020-08-26 09:57:18

标签: r ggplot2

我想创建一个条形图,并向每组添加一条平均线。由于值是负数,因此我反转了Y轴。条形图成功地用反转的Y轴表示。

然后,我添加了每个组(类型)的平均线。不幸的是,平均线没有在反转的Y轴上绘制。 平均线符号错误,为正,但应为负。

有人可以帮我把中线移到Y轴的上半部分,移到条形图上吗?

See image of current bar plot with mean value

这是到目前为止使用的代码:

library(scales)
p <- ggplot(data, aes(x=Type,y=Value, fill=Iteration))+
  geom_bar(stat="identity", position = "dodge")+
  stat_summary(fun = mean, geom = "errorbar", 
               aes(ymax = ..y.., ymin = ..y.., group = Type),
               width = 1, linetype = "solid", color = "red")+
  scale_y_continuous(trans = "reverse")

1 个答案:

答案 0 :(得分:0)

您可以像这样简单地在前面添加-

library(scales)
p <- ggplot(data, aes(x=Type,y=Value, fill=Iteration))+
  geom_bar(stat="identity", position = "dodge")+
  stat_summary(fun = mean, geom = "errorbar", 
               aes(ymax = - ..y.., ymin = - ..y.., group = Type),
               width = 1, linetype = "solid", color = "red")+
  scale_y_continuous(trans = "reverse")
相关问题