简体   繁体   English

带有coord_flip的ggplot2中errobar的自由缩放和位置

[英]Free scales and position of errobar in ggplot2 with coord_flip

Could this be related with coord_flip? 可以和coord_flip有关吗? I don't manage to get the free scale and the good position of errorbars with the following code: 我无法使用以下代码来获得自由缩放和误差线的良好位置:

ggplot(df, aes(x=d, y=value.mean, fill=s))+
  facet_grid(.~variable, 
             scales="free_y")+
  geom_bar(stat="identity", 
           position=position_dodge())+
  geom_errorbar(aes(ymin=value.mean-value.se, ymax=value.mean+value.se), 
                position=position_dodge(.9), 
                width=3)+
  scale_fill_manual(values=c("#7fc97f","#beaed4"))+
  scale_x_continuous(breaks=c(-60,-100))+
  coord_flip()+
  theme_bw()

Here is a sample of df: 这是df的示例:

s   d   variable    value.mean  value.se
f   -100    aa  315 48
g   -100    aa  394 73
f   -60 aa  284 48
g   -60 aa  293 82
f   -100    bb  60  6
g   -100    bb  55  7
f   -60 bb  116 14
g   -60 bb  123 21

To get good position of errorbars, one solution would be to convert to factor variable d and then position=position_dodge(0.9) will work 为了获得正确的误差线位置,一种解决方案是将其转换为因子变量d ,然后position=position_dodge(0.9)将起作用

ggplot(df, aes(x=as.factor(d), y=value.mean, fill=s))+
      facet_grid(.~variable)+
      geom_bar(stat="identity", 
               position=position_dodge())+
      geom_errorbar(aes(ymin=value.mean-value.se, ymax=value.mean+value.se), 
                    position=position_dodge(width=0.9), 
                    width=0.3)+
      scale_fill_manual(values=c("#7fc97f","#beaed4"))+
      coord_flip()+
      theme_bw()

If you take d as numeric then you have to adjust width= of position_dodge() according to your data values. 如果将d用作数字,则必须根据数据值调整position_dodge() width= In this case it is 35 that positions errorbars as supposed. 在这种情况下,按预期放置误差线的位置是35。

ggplot(df, aes(x=d, y=value.mean, fill=s))+
      facet_grid(.~variable, 
                 scales="free_x")+
      geom_bar(stat="identity", 
               position=position_dodge())+
      geom_errorbar(aes(ymin=value.mean-value.se, ymax=value.mean+value.se), 
                    position=position_dodge(width=35), 
                    width=10)+
      scale_fill_manual(values=c("#7fc97f","#beaed4"))+
      scale_x_continuous(breaks=c(-60,-100))+
      coord_flip()+
      theme_bw()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM