Search code examples
pythonggplot2python-ggplot

Ordering the facet in python ggplot


I have a dataframe, df where print df.head() gives:

journey_time                  time_stamp    day_name  day_no  week_no
240           289 2017-03-06 00:03:58   Monday       0       10
241           288 2017-03-06 00:08:58   Monday       1       10
242           291 2017-03-06 00:13:58   Monday       1       10
243           289 2017-03-06 00:18:59   Monday       1       10
244           295 2017-03-06 00:23:58   Monday       2       10
243           289 2017-03-06 00:18:59   Monday       2       10
244           295 2017-03-06 00:23:58   Monday       2       10

I basically have a series of journey_time values for a each day, range of day_no.

p = ggplot(aes(x='time_stamp',y='journey_time'),data=df) + scale_x_date(labels = date_format("%H:%M"), date_minor_breaks = "1 hour") + geom_line() + labs(x="Time", y="journey_time (seconds)") + facet_grid('day_name',scales='free_x')

This works correctly in that I get 7 horizontal plots. However, the days are not in order (Friday,Monday,Saturday etc). How do I order by the day_no record?

enter image description here


Solution

  • By default, the ggplot appears to put categorical variables in alphabetical order, but you can use the category dtype from Pandas to specify your own order:

    df.day_name = pd.Categorical(df.day_name, 
                                 ordered=True,
                                 categories=["Monday", "Tuesday", "Wednesday", 
                                             "Thursday", "Friday", "Saturday", 
                                             "Sunday"])
    

    This works as expected in my testing environment (Python 3.4, Pandas 0.18.0, ggplot 0.11.5).