Ggplot2: setting geom_bar baseline to 1 instead of zero

I am trying to create a histogram (with geom_bar) of relations and would like to set the x axis to y = 1. Therefore, relations <1 will be below the axis, and relations> 1 will 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 will be very preferable ... Ideally, the chart will look something like this: http://i.stack.imgur.com/isdnw.png , with the exception of the "negative" bars, will be relations <1.

Many thanks for your help!

FROM

+6
source share
3 answers

You can shift the geom_barline geom_barto 1 (instead of zero) as follows:

  1. -1, ratio=1 , , .

  2. 1 Y, .

    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(labels = function(y) y + 1)
    

enter image description here

+9

- geom_segment. "" y.

set.seed(123)
dat <- data.frame(x=1:10, ratio=sort(runif(10,0,2)))

#create flag
dat$col_flag <- dat$ratio > 1

ggplot(dat, aes(color=col_flag)) +
  geom_segment(aes(x=x,xend=x,y=1, yend=ratio), size=15)

enter image description here

+7

y:

shift_trans = function(d = 0) {
  scales::trans_new("shift", transform = function(x) x - d, inverse = function(x) x + d)
}

ggplot(dat, aes(x, ratio, fill = ifelse(ratio > 1,"GT1","LT1"))) +
  geom_bar(stat="identity") +
  scale_fill_manual(values=c("blue","red"), name="LT or GT 1") +
  scale_y_continuous(trans = shift_trans(1))

enter image description here

.


, eipi10: dat = data.frame(ratio=-4:11/3, x=1:16)

+1

Source: https://habr.com/ru/post/1628197/


All Articles