How can I order the months chronologically in ggplot2 short of writing the months out?

Use the built-in month.name or month.abb variable to specify the levels of your factor in the correct order. In this case, you have abbreviations so month.abb is appropriate.

your_data$month = factor(your_data$month, levels = month.abb)

I think creating the factor in the correct order is the best way, but you can also just order the axis using the limits argument of the discrete scale (see ?discrete_scale for more info).

+ scale_x_discrete(limits = month.abb)

Locales

If you are in a non-English locale, you can construct your own month name constants with a little date formatting (basically stolen from Brian Ripley in this R-Help thread):

 month.name.loc = format(ISOdate(2004, 1:12, 1), "%B")
 month.abb.loc  = format(ISOdate(2004, 1:12, 1), "%b")

If you want to use month names/abbreviations from a different locale than you’re in, the withr package is useful.

Leave a Comment