geom_bar bug?

383 views
Skip to first unread message

Elaine

unread,
Sep 13, 2011, 11:57:22 AM9/13/11
to ggplot2
My general understanding in ggplot is that the aes() statement in the
original ggplot() can include any mappings that might be used in the
subsequent layers, and any that are not needed for a particular layer
will be ignored. However, this assumption is broken with geom_bar as
explained below -- this is either a bug or it change my understanding
of ggplot.

df <- data.frame(x=1:5, y=6:10) 11:21:17 AM

This works:
ggplot(data=df, aes(x=x, weight=y)) + geom_bar()

So does this one -- in this case it uses the default geom_bin
behvavior and plots counts when no weight is specified:
ggplot(data=df, aes(x=x)) + geom_bar() 11:22:03 AM

This also works, and makes the same type of summary as the first one:
ggplot(data=df, aes(x=x, y=y)) + geom_bar(stat='identity')

But this gives an error: "Error in pmin(y, 0) : object 'y' not
found". I would have expected it to make the same plot as it would if
the y=y mapping were removed.
ggplot(data=df, aes(x=x, weight=y, y=y)) + geom_bar()

I assume this is a bug caused by some sub-call in which weight would
be passed to y but gets confused with the mapped y, etc. (though I
can't figure out from looking at the source code exactly where).

It's certainly possible to work around this, but I would have assumed
that if I had a plot with multiple layers including geom_bar and
geom_point, for example, that I could have done all the mapping in the
original aes() instead of having to handle them separately within each
geom to avoid an error.

<><><><><><><><><>
Elaine McVey
Senior Statistician
BD Technologies
<><><><><><><><><>

Juan Carlos Borrás

unread,
Sep 13, 2011, 12:53:46 PM9/13/11
to Elaine, ggplot2
Not a bug. Been there.
Stating the y aesthetic with the identity stat makes no sense if you
think about it...
So this is a no-no:

> ggplot(data=df, aes(x=x, y=y)) + geom_bar(stat='identity')
But this is the way to go:
> ggplot(data=df, aes(x=x)) + geom_bar(stat='identity')
or this one if you got both dimensions:
> ggplot(data=df, aes(x=x, y=y)) + geom_bar()
Building graphs using a grammar provides powerful semantics, but I
sometimes miss the compiler with the good error messages (pun intended
;-) ).
Hope it helps, keep kicking.
Cheers,
jcb!_______________________
http://twitter.com/jcborras

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

--
Cheers,
jcb!
_______________________
http://twitter.com/jcborras

Elaine

unread,
Sep 13, 2011, 1:39:09 PM9/13/11
to ggplot2
Hi Juan. I appreciate the reply, but I think I disagree.

For one, I get an error when I run your second and third examples (if
you don't then this would be an interesting version-based difference)
-- the Error in pmin(y, 0) : object 'y' not found error I was
referring to.

Second, I think the first example, with y=y and stat=identity does
make sense if y represents the value you want the bar to indicate (a
mean, for example). It would be simpler to use weight=y instead, but
I don't necessarily thing there is any reason that using stat=identity
should be avoided. I generally disagree with using barcharts for this
purpose but certainly it is very common. In this case I'm more
interested in the principles and best practices for ggplot and whether
I understand them than the specific use of geom_bar.

-Elaine

On Sep 13, 12:53 pm, Juan Carlos Borrás <jcbor...@gmail.com> wrote:
> Not a bug. Been there.
> Stating the y aesthetic with the identity stat makes no sense if you
> think about it...
> So this is a no-no:> ggplot(data=df, aes(x=x, y=y)) + geom_bar(stat='identity')
>
> But this is the way to go:> ggplot(data=df, aes(x=x)) + geom_bar(stat='identity')
>
> or this one if you got both dimensions:> ggplot(data=df, aes(x=x, y=y)) + geom_bar()
>
> Building graphs using a grammar provides powerful semantics, but I
> sometimes miss the compiler with the good error messages (pun intended
> ;-) ).
> Hope it helps, keep kicking.
> Cheers,
> jcb!_______________________http://twitter.com/jcborras
> _______________________http://twitter.com/jcborras- Hide quoted text -
>
> - Show quoted text -

Juan Carlos Borrás

unread,
Sep 13, 2011, 5:22:50 PM9/13/11
to Elaine, ggplot2
I'm going to find a hole to bury myself out of embarrassment.
An apology is owed.
Sorry.
Let me at least suggest a properly constructed (and tested) example
where the y and weight params in an aes() call do not produce an
error:

> ggplot(data=df, aes(x=x, y=y, weight=y)) + geom_bar(stat='identity', binwidth=1)

While this one will produce a warning about stacking:
> ggplot(data=df, aes(x=x, y=-y, weight=y)) + geom_bar(stat='identity', binwidth=1)

Mixing both y= and weight= in an aes() call is something that keeps me
scratching my head. After all neither of the subsequent (in this
discussion) geoms can do much with both of them when stated together.

But I'll be more than glad to be proved again today dead wrong.
And now if you excuse me this one over here looks deep and dark enough.....
Cheers,
jcb!

> df=data.frame(x=seq(1,10), y=seq(10,1,-1))
> ggplot(data=df, aes(x=x, weight=y)) + geom_histogram(binwidth=1)

Hadley Wickham

unread,
Sep 14, 2011, 9:20:01 AM9/14/11
to Elaine, ggplot2
On Tue, Sep 13, 2011 at 10:57 AM, Elaine <elaine...@gmail.com> wrote:
> My general understanding in ggplot is that the aes() statement in the
> original ggplot() can include any mappings that might be used in the
> subsequent layers, and any that are not needed for a particular layer
> will be ignored.  However, this assumption is broken with geom_bar as
> explained below -- this is either a bug or it change my understanding
> of ggplot.
>
> df <- data.frame(x=1:5, y=6:10)         11:21:17 AM
>
> This works:
> ggplot(data=df, aes(x=x, weight=y)) + geom_bar()
>
> So does this one -- in this case it uses the default geom_bin
> behvavior and plots counts when no weight is specified:
> ggplot(data=df, aes(x=x)) + geom_bar()  11:22:03 AM
>
> This also works, and makes the same type of summary as the first one:
> ggplot(data=df, aes(x=x, y=y)) + geom_bar(stat='identity')
>
> But this gives an error: "Error in pmin(y, 0) : object 'y' not
> found".  I would have expected it to make the same plot as it would if
> the y=y mapping were removed.
> ggplot(data=df, aes(x=x, weight=y, y=y)) + geom_bar()
>
> I assume this is a bug caused by some sub-call in which weight would
> be passed to y but gets confused with the mapped y, etc. (though I
> can't figure out from looking at the source code exactly where).

Yes, this is definitely a bug, but one that is hard to fix in the
short-term. It's due to the way that stat_bin uses weight and
geom_bar uses y, and needs a rather large rewrite of the geom/stat
system to fix - it's on my radar, but it's unlikely that I'll have the
time to tackle this before next summer.

Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages