ggplot2:将geom_bar基线设置为1而不是零 [英] ggplot2: Setting geom_bar baseline to 1 instead of zero

查看:544
本文介绍了ggplot2:将geom_bar基线设置为1而不是零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图制作比例的条形图(带有geom_bar),并且想要将y轴设置为y = 1。因此,比值<1将低于轴,比值> 1将高于轴。我可以用geom_point做类似的事情:

  ggplot(data,aes(x = ratio,y = reorder(place,ratio) ))+ geom_point()+ geom_vline(xintercept = 1.0)+ coord_flip()

然而geom_bar是更喜欢...理想情况下,图形看起来像这样:


I'm trying to make a bar graph (with geom_bar) of ratios, and would like to set the x-axis at y=1. Therefore, ratios <1 would be below the axis and ratios >1 would be above the axis. I can do something similar with geom_point:

ggplot(data, aes(x=ratio, y=reorder(place,ratio)))+geom_point()+geom_vline(xintercept=1.0)+coord_flip()

However geom_bar would be much preferred... Ideally the graph would look something like this: http://i.stack.imgur.com/isdnw.png, except the "negative" bars would be ratios <1.

Thanks so much for your help!

C

解决方案

You can shift the geom_bar baseline to 1 (instead of zero) as follows:

  1. Shift the data by -1, so that ratio=1 becomes zero and is therefore used as the baseline.

  2. Add 1 to the y-axis labels so that they reflect the actual data values.

    dat = data.frame(ratio=-4:11/3, x=1:16)
    
    ggplot(dat, aes(x, ratio-1, fill=ifelse(ratio-1>0,"GT1","LT1"))) +
      geom_bar(stat="identity") +
      scale_fill_manual(values=c("blue","red"), name="LT or GT 1") +
      scale_y_continuous(breaks=-3:3, labels=-3:3 + 1)
    

这篇关于ggplot2:将geom_bar基线设置为1而不是零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆