11

I'm using yhat's ggplot library. I have the following pandas DataFrame:

   degree  observed  percent observed  expected  percent expected
0       0         0               0.0         0          0.044551
1       1         1               0.1         1          0.138604
2       2         3               0.3         2          0.215607
3       3         4               0.4         2          0.223592
4       4         1               0.1         2          0.173905
5       5         1               0.1         1          0.108208

At the moment, I'm doing the following (where df returned in the first line in the first line in the function is the DataFrame above):

def chartObservedExpected(graph):
    df = observedExpected(graph)
    return ggplot(aes(x='degree', y='percent observed'), data=df) + \
           geom_point() + \
           ylim(-0.015,1.015) + \
           xlim(-0.05,max(df['degree']) + 0.25) + \
           labs("Degree","Proportion of Total") + \
           ggtitle("Observed Node Degree Distribution")

chartObservedExpected(G)

This is what I get:

scatter

However, whenever I try geom_bar() instead of geom_point(), I end up with just 100% bars. I've tried just plain geom_bar() and also geom_bar(stat="percent observed"), but neither seem to work. This is always what I get:

bar

What I'm trying to do is to mimic/reproduce the following:

attempt

Any idea how to get the bar part working (or the whole thing, for that matter)?

2
  • 2
    Looks like a bug/something that hasn't been ported over yet. See the issue on Github here. If it was working, it'd be something like ggplot(aes(x='degree'), df) + geom_bar(aes(y='percent_observed'), stat='identity'), but it looks like stats other than 'bin' don't work yet. Commented Mar 24, 2014 at 1:45
  • @Marius Ok, thanks. Not that big of a deal for now, but I hope they implement it soon! Commented Mar 24, 2014 at 2:10

1 Answer 1

24

Use weight, here is an example:

from ggplot import *
import pandas as pd
df = pd.DataFrame({"x":[1,2,3,4], "y":[1,3,4,2]})
ggplot(aes(x="x", weight="y"), df) + geom_bar()

the output looks like:

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Looks like this is a little funky if you try to use xlim to set the range of the x-axis, but it seems to work well otherwise. I also was unable to change the width of the bars.
Spent so much time trying to do this with stat='identity' and it didn't work. This does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.