gcamargo
gcamargo

Reputation: 3971

Plotnine bar plot order by variable

I have a question on ordering bar plots. For example:

http://pythonplot.com/#bar-counts

(ggplot(mpg) +
aes(x='manufacturer') +
geom_bar(size=20) +
coord_flip() +
ggtitle('Number of Cars by Make')
)

how to order by "mpg" ?

Upvotes: 5

Views: 5591

Answers (2)

Chris Nelson
Chris Nelson

Reputation: 3707

At STHDA I found:

Change the order of items in the legend The function scale_x_discrete can be used to change the order of items to “2”, “0.5”, “1” :

p + scale_x_discrete(limits=c("D2", "D0.5", "D1"))

My goal was to preserve the order of the df so I did:

scale_x_discrete(limits=df[xColumn].tolist())

then I realized that the first bar item was at the end so I switched to:

scale_x_discrete(limits=df[xColumn].tolist()[::-1])

I couldn't use reverse() because it works in place and doesn't return the list so limits didn't seem to see the effect.

Upvotes: 4

gcamargo
gcamargo

Reputation: 3971

Thanks to has2k1: https://github.com/has2k1/plotnine/issues/94

If the x mapping is an ordered categorical, it is respected.

from plydata import *
from plotnine import *
from plotnine.data import mpg

# count the manufacturer and sort by the count (see, plydata documentation
# or find out how to do the same thing using raw pandas)
m_categories = (
    mpg
    >> count('manufacturer', sort=True)
    >> pull('manufacturer')
)

df = mpg.copy()
df['manufacturer'] = pd.Categorical(df['manufacturer'],     categories=m_categories, ordered=True)

(ggplot(df) + 
   aes(x='manufacturer') +
geom_bar(size=20) + 
coord_flip() +
ggtitle('Number of Cars by Make')
)

Upvotes: 3

Related Questions