Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading