Code from Elegant Graphics for Data Analysis throws error

334 views
Skip to first unread message

Pierre Massat

unread,
Jul 25, 2016, 8:05:55 AM7/25/16
to ggplot2
Dear all,

This is is my first post on this list. I'm studying ggplot2 with the book, and I'm stuck with an error with the code on page 61 :

# Three variations on the histogram. (Left) A frequency polygon;
# (middle) a scatterplot with both size and height mapped to frequency;
# (right) a heatmap representing frequency with colour.
d <- ggplot(diamonds, aes(carat)) + xlim(0, 3)
d + stat_bin(aes(ymax = ..count..), binwidth = 0.1, geom = "area")
d + stat_bin(aes(size = ..density..), binwidth = 0.1, geom = "point", position="identity")
d + stat_bin(aes(y=1, fill = ..count..), binwidth = 0.1, geom = "tile", position="identity") + scale_y_continuous("")

The last chart doesn't work? I get the following error : "stat_bin() must not be used with a y aesthetic".

I suppose ggplot2 has changed a bit since the book was published 7 years ago. I'm using ggplot2 2.1.0.

Thanks in advance for your help.

Roman Luštrik

unread,
Jul 25, 2016, 8:49:31 AM7/25/16
to Pierre Massat, ggplot2
I placed `y = 1` outside the `aes()` call which seems to be used for calculating the values for `stat_bin`.

d + stat_bin(aes(fill = ..count..), y = 1, binwidth = 0.1, geom = "raster", position="identity") + scale_y_continuous("")

This seems very similar to what is visible at http://rstudio-pubs-static.s3.amazonaws.com/3355_d3f08cb2f71f44f2bbec8b52f0e5b5e7.html (towards the bottom of the page). Is this it?

Cheers,
Roman

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

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
In God we trust, all others bring data.

Brandon Hurr

unread,
Jul 25, 2016, 8:51:04 PM7/25/16
to Roman Luštrik, Pierre Massat, ggplot2
I will add that if you're using the old book, you and others will be in for a lot of these issues. In the last two months Hadley released the second version of this book. If you can afford to pick it up (currently $40-50 on Amazon), I would highly recommend it. Even Winston Chang's R Graphics Cookbook (based upon ggplot2) is out of date due to more recent changes to the 2.0.0 milestone. 

HTH, B

Pierre Massat

unread,
Jul 26, 2016, 3:13:09 AM7/26/16
to Brandon Hurr, Roman Luštrik, ggplot2
Hi Brandon,

Bummer, I bought mine from Amazon just about a month ago. So I got a new copy of the old book. Is there any place on the web where I can see what's changed in the new ggplot2 ? Perhaps that can help me.

Thanks for your help,

Pierre.

Felipe Carrillo

unread,
Jul 26, 2016, 9:48:03 AM7/26/16
to Pierre Massat, Brandon Hurr, Roman Luštrik, ggplot2
I ordered mine in January and it just arrived about two weeks ago. Quite a few changes and nice improvements.

Reply all
Reply to author
Forward
0 new messages