ggplot2: Setting geom_bar baseline to 1 instead of zero

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

enter image description here

Leave a Comment