Search code examples
rggplot2scalegeom-bar

ggplot geom_bar: Reversing Y axis but I would like to have bars coming from down-up not from top-down


The context of the data is that I am quantifying an assay where lower number is better. So I want lower number to be high up on the Y axis and my bars starting from bottom going up. However, when I use scale_Y_reverse, bars start from the top. How can I achieve what I want?

enter image description here

Thanks

Scale_Y_reverse makes bars start from top not from bottom. I want it to be from the bottom.

o <- data.frame('Time'=c(24,48),'Count'=c(20,40))

ggplot(o, aes(x=as.factor(Time),y=Count)) + 
  geom_col() + 
  scale_y_reverse()

Solution

  • ggplot is pretty strict about bars starting at 0, since that's almost universally a good idea. Unless 40 is a super special value for some reason, this plot is likely going to confuse a lot of people.

    But I think you can do what you want by plotting:

    ggplot(o, aes(x=as.factor(Time), y= 40 - Count)) + 
        geom_col() +
        scale_y_continuous(labels = \(x) 40 - x, limits = c(0, 40))
    

    enter image description here

    Not recommended.