Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

order bar charts #94

Closed
gcamargo1 opened this issue Nov 28, 2017 · 7 comments
Closed

order bar charts #94

gcamargo1 opened this issue Nov 28, 2017 · 7 comments
Labels

Comments

@gcamargo1
Copy link

Hi!
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" ?
Thanks!

@has2k1
Copy link
Owner

has2k1 commented Nov 28, 2017

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')
)

ordered-cat

@has2k1 has2k1 closed this as completed Nov 28, 2017
@Steve-Fry
Copy link

I spent an embarrassing amount of time trying to find the solution to this today. Also based from reading around some of the other plotting packages this seems a common problem.

Is there anywhere in the docs this could be highlighted a bit more clearly?

Also as a newcomer to Plotnine, this is an awesome tool you have put together! Thanks!

@has2k1
Copy link
Owner

has2k1 commented Dec 6, 2017

@Steve-Fry, what parts of the docs did you reach for?

@Steve-Fry
Copy link

In fairness it is probably mostly about me being inexperienced in pandas/plotnine, rather than a deficiency.

I checked;
https://github.com/has2k1/plotnine-examples
https://plotnine.readthedocs.io/en/stable/api.html
https://plotnine.readthedocs.io/en/stable/tutorials.html

If it would be helpful I could look at writing a demo for the examples repo?

@has2k1
Copy link
Owner

has2k1 commented Dec 7, 2017

I thought of putting a note at aes but a demo would be more illustrative.

Just follow the conventions used at https://github.com/has2k1/plotnine-examples/tree/master/plotnine_examples/tutorials. i.e

  • filename that starts with miscellaneous-
  • # Title at the top of the notebook

Then it will be picked up when the documentation is built.

Thanks.

@Steve-Fry
Copy link

I added a new notebook and created a PR. I'm hoping I went about it in the right way, lemme know what I'm missing.

Thanks 👍

@Akaban
Copy link

Akaban commented Aug 1, 2019

Hello,

I would like to reopen this issue, I don't think it's really user friendly to not include automatic sorting in geom_bar function. If I understand correctly we need to make a pandas.Categorical each time we want to define the order of our graph. That's something I find really painful.

Can't we implement a keyword_argument sort_by="xxxx" in geom_bar?

I can do the MR if it's not too much complicated, I don't know plotnine code as of now :)

Thanks for the answer

@Akaban Akaban mentioned this issue Aug 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants