堆叠条形图上的位置偏差误差线

时间:2019-06-04 02:55:48

标签: r ggplot2

错误条现在不在位置。

我尝试使用ggplot定位它们:

ggplot(d.Mean, aes(x=Treatment, y=Flux.m, fill=Plant)) +
  geom_bar(size=4, stat="identity", position="stack") +
  geom_errorbar(aes(ymin=Flux.m - Flux.se, ymax=Flux.m + Flux.se), 
                width=.2, stat="identity") +
  guides(fill=FALSE) +
  labs(x="Treatment", y="N uptake %") +
  mytheme +
  theme(legend.text = element_text(size=15)) +
  guides(fill=FALSE) +
  scale_fill_grey()    

我的数据

    Treatment Plant Flux.m Flux.se
    NC  Weed    1.1907929   0.162910442
    NC  Wheat   0.2198656   0.009046636
    NF  Weed    0.5080035   0.116434029
    NF  Wheat   0.6842919   0.073096108
    TC  Weed    0.1323349   0.024147990
    TC  Wheat   0.2239926   0.075459460
    TF  Weed    0.1435406   0.019167675
    TF  Wheat   0.5904143   0.066450625

现在,它不在位置上。我想把它放在酒吧上面。

1 个答案:

答案 0 :(得分:2)

geom_errorbar不知道您的geom_bar层中的堆栈,因此您需要添加一个帮助程序列以告诉它从哪里开始,以y开头:

d.Mean %>%
  group_by(Treatment) %>%
  arrange(Treatment, desc(Plant)) %>%
  mutate(Flux.m_cuml = cumsum(Flux.m)) %>%

ggplot(aes(x=Treatment,y=Flux.m,fill=Plant))+
  geom_bar(size=4,stat="identity",position="stack")+
  geom_errorbar(aes(ymin=Flux.m_cuml - Flux.se, ymax=Flux.m_cuml + Flux.se), 
                width=.2, stat="identity")+
  guides(fill=FALSE)+
  scale_fill_grey() +
  labs(x="Treatment",y="N uptake %")+ 
  #mytheme+
  theme(legend.text = element_text(size=15))

enter image description here