Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Reverse the order of a categorical axis in ggplot2
scale_x_discrete(limits = rev(levels(the_factor)))
@davidroberson

This comment has been minimized.

Copy link

@davidroberson davidroberson commented Feb 9, 2017

thanks this was very helpful. I found this via a google search.

@bck243

This comment has been minimized.

Copy link

@bck243 bck243 commented May 18, 2017

So helpful!!

@yfarjoun

This comment has been minimized.

Copy link

@yfarjoun yfarjoun commented Apr 8, 2018

@zammyfh

This comment has been minimized.

Copy link

@zammyfh zammyfh commented Aug 23, 2018

Amazing, thank you!

@mgei

This comment has been minimized.

Copy link

@mgei mgei commented Sep 18, 2018

You can also just specify it in the aesthetics
aes(x = reorder(the_factor, desc(the_factor)), ...)

@saskiakess

This comment has been minimized.

Copy link

@saskiakess saskiakess commented Oct 10, 2018

Thank you!

@jgarces02

This comment has been minimized.

Copy link

@jgarces02 jgarces02 commented Dec 13, 2018

Thanks a lot!

@ptompalski

This comment has been minimized.

Copy link

@ptompalski ptompalski commented Apr 10, 2019

@mgei 's method is slightly better as it works with a faceted graph and scales="free".

@novellisa

This comment has been minimized.

Copy link

@novellisa novellisa commented Jul 4, 2019

Thank you!

@Dohanaga92

This comment has been minimized.

Copy link

@Dohanaga92 Dohanaga92 commented Jul 17, 2019

perfecttt!!!

@ibarraespinosa

This comment has been minimized.

Copy link

@ibarraespinosa ibarraespinosa commented Feb 19, 2020

so simple, many thanks

@jzadra

This comment has been minimized.

Copy link

@jzadra jzadra commented May 5, 2020

unfortunately this does not work with rlang/purrr functional programming:

scale_x_discrete(limits = rev(levels(!!sym(the_factor))))

Error in !sym(the_factor) : invalid argument type

And, as an aside, it does not work if the variable is a character vector rather than a factor (though of course that's an easy fix).

@jennybc

This comment has been minimized.

Copy link
Owner Author

@jennybc jennybc commented May 5, 2020

@jzadra The constructs like !! are specific to tidyeval functions, which does not include base functions like levels().

@jzadra

This comment has been minimized.

Copy link

@jzadra jzadra commented May 5, 2020

Thanks. It's too bad that the scale_*_reverse() only works on numeric; it seems like it would be useful to have it perform this behavior on factor and character.

@casa-henrym

This comment has been minimized.

Copy link

@casa-henrym casa-henrym commented Sep 29, 2020

This works now: scale_x_discrete(limits = rev)

@ckm2016

This comment has been minimized.

Copy link

@ckm2016 ckm2016 commented Oct 16, 2020

This works now: scale_x_discrete(limits = rev)

Thanks for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.