Question on p. 40 of book - adding layer to p

1 view
Skip to first unread message

Peter Flom

unread,
Mar 9, 2009, 10:24:14 AM3/9/09
to ggplot2group
Good morning

I tried this (from page 40, just changing object name)

p <- ggplot(diamonds, aes(carat, price, color = cut))
p2 <- p + layer(geom = "point")

p3 <- p + layer( geom = "bar",
geom_params = list(fill = "steelblue"),
stat = "bin",
stat_params = list(binwidth = 2))

p, as appropriate, gives an error of no layer
p2 works fine
p3 gives an error:

in eval(expr, envir, enclos) : object "y" not found


which is not clear, at least, not to me ....


TIA

Peter

Peter L. Flom, PhD
Statistical Consultant
www DOT peterflomconsulting DOT com

hadley wickham

unread,
Mar 10, 2009, 8:59:56 AM3/10/09
to Peter Flom, ggplot2group
On Mon, Mar 9, 2009 at 8:24 AM, Peter Flom
<peterflom...@mindspring.com> wrote:
>
> Good morning
>
> I tried this (from page 40, just changing object name)
>
> p <- ggplot(diamonds, aes(carat, price, color = cut))
> p2 <- p + layer(geom = "point")
>
> p3 <- p + layer( geom = "bar",
>                geom_params = list(fill = "steelblue"),
>                stat = "bin",
>                stat_params = list(binwidth = 2))
>
> p, as appropriate, gives an error of no layer
> p2 works fine
> p3 gives an error:
>
>  in eval(expr, envir, enclos) : object "y" not found
>
>
> which is not clear, at least, not to me ....

This is a little complicated to explain - but it's due to the
interaction of the statistics and the geom. The bin statistic
basically ignores the y value in the original dataset, and returns a
data frame with columns x, width, count and density (plus a few
others). Usually, the y mapping defaults to the ..count.. (the dots
meaning to use the count variable from the statistic, not the original
data), but you've overridden that so the y value is not mapped to
anything after the stat is run, and you get the error. Does that make
sense?

Hadley

--
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages