Incorrect results in R 2.15

128 views
Skip to first unread message

Kasi

unread,
Apr 5, 2012, 6:04:55 PM4/5/12
to ggp...@googlegroups.com
Hi,

  I just updated to 2.15 and reinstalled the ggplot library. Now facet_grid() displays incorrect data. Does anyone else have this problem? Just running the first example listed here (http://had.co.nz/ggplot2/facet_grid.html) already shows a slightly different graph.

I use ggplot2_0.9.0.

Thanks.

Tyler Rinker

unread,
Apr 5, 2012, 6:13:50 PM4/5/12
to pop...@gmx.net, ggplot
Without any code to reproduce your problem I'm only guessing at the solution.  It really is very helpful if you display some sample data and code that recreates the error.

I think this may be of use:

Tyler


Date: Thu, 5 Apr 2012 15:04:55 -0700
From: pop...@gmx.net
To: ggp...@googlegroups.com
Subject: Incorrect results in R 2.15
--
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

Kasi

unread,
Apr 5, 2012, 6:55:29 PM4/5/12
to ggp...@googlegroups.com, pop...@gmx.net
It's very simple to reproduce. Run this:

library("ggplot2")
p <- ggplot(diamonds, aes(carat, ..density..)) + geom_histogram(binwidth = 0.2)
p + facet_grid(. ~ cut)

And compare it to the first graph on this page:

http://had.co.nz/ggplot2/facet_grid.html

If they are not the same, then either the diamonds data set has changed or somewhere the is a bug.

Tyler Rinker

unread,
Apr 5, 2012, 7:14:27 PM4/5/12
to pop...@gmx.net, ggplot
I run exactly what you have and get this:


I don't see a difference between this and the link you supplied.  Unless there's something I'm missing I'd suggest you try rebooting R as perhaps you've overwritten something that's related/necessary for this plot.

Date: Thu, 5 Apr 2012 15:55:29 -0700
From: pop...@gmx.net
To: ggp...@googlegroups.com
CC: pop...@gmx.net
Subject: Re: Incorrect results in R 2.15

Tyler Rinker

unread,
Apr 5, 2012, 7:28:24 PM4/5/12
to pop...@gmx.net, ggplot
I just happened to notice that there are slight differences in the histograms I posted and the one from the website you posted.  This may be due to a slight change in binwidth or the graphics device used (mine is a screen shot of Windows R gui).   It is hard to know what your problem is as you haven't been explicit in what you believe is incorrect.  Could you explain a bit more about the discrepancy you're seeing?

Tyler


From: tyler_...@hotmail.com
To: pop...@gmx.net; ggp...@googlegroups.com
Subject: RE: Incorrect results in R 2.15
Date: Thu, 5 Apr 2012 19:14:27 -0400

Jean-Olivier Irisson

unread,
Apr 6, 2012, 2:43:30 AM4/6/12
to Tyler Rinker, ggplot2
On 2012-Apr-06, at 01:28 , Tyler Rinker wrote:
>
> I just happened to notice that there are slight differences in the histograms I posted and the one from the website you posted. This may be due to a slight change in binwidth or the graphics device used (mine is a screen shot of Windows R gui). It is hard to know what your problem is as you haven't been explicit in what you believe is incorrect. Could you explain a bit more about the discrepancy you're seeing?

I also see differences in R 2.14 so I think the change comes from ggplot 0.9 vs 0.8.9 rather than from R 2.14 vs R 2.15.
It's not a graphic device or platform thing either (and it shouldn't be) since I see the same graph Tyler posted but I am on OS X with a quartz device.

I suspected stat_bin changed the way the bounds of the successive bins are included/excluded. And indeed there is this in the commit log:

commit bb4ec84bbedadd95647760fe586f64d0d6216abb
Author: hadley <h.wi...@gmail.com>
Date: Wed Dec 21 08:12:24 2011 -0600

Default to right-open, left-closed intervals in hist (Fixes #194)

So I checked :

library("devtools")
# install old version of plyr, compatible with the old ggplot2
install_github("plyr", branch="plyr-1.6")
# install previous commit
install_github("ggplot2", branch="1bfbee0fc791de8bfef901c156ebd04c4765b194")
library("ggplot2")
ggplot(diamonds, aes(carat, ..density..)) + geom_histogram(binwidth = 0.2) + facet_grid(.~cut)
# install suspected commit
install_github("ggplot2", branch="bb4ec84bbedadd95647760fe586f64d0d6216abb")
ggplot(diamonds, aes(carat, ..density..)) + geom_histogram(binwidth = 0.2) + facet_grid(.~cut)

and indeed, this commits changes the aspect of the histogram. It fixes a bug (nº 194 as mentionned in the commit message).

However, one thing that might be considered a drawback is that geom_histogram is now different from base::hist even when the breaks are the same:

d <- diamonds[diamonds$cut=="Fair",]
install_github("ggplot2", branch="1bfbee0fc791de8bfef901c156ebd04c4765b194")
ggplot(d, aes(carat)) + geom_histogram(binwidth = 0.2)
hist(d$carat, breaks=ggplot2:::fullseq(range(d$carat), size=0.2, pad=TRUE), freq=TRUE)
# -> same plot

ggplot(d, aes(carat, ..density..)) + geom_histogram(binwidth = 0.2)
hist(d$carat, breaks=ggplot2:::fullseq(range(d$carat), size=0.2, pad=TRUE), freq=FALSE)
# -> same plot

install_github("ggplot2", branch="bb4ec84bbedadd95647760fe586f64d0d6216abb")
ggplot(d, aes(carat)) + geom_histogram(binwidth = 0.2)
hist(d$carat, breaks=ggplot2:::fullseq(range(d$carat), size=0.2, pad=TRUE), freq=TRUE)
# -> different plot

Is this worth a "bug" report?


NB: after all this, if you don't want to be stuck in the past, redo
install.packages(c("plyr", "ggplot2"))
and start a new R session.

Jean-Olivier Irisson
---
Observatoire Océanologique
Station Zoologique, B.P. 28, Chemin du Lazaret
06230 Villefranche-sur-Mer
Tel: +33 04 93 76 38 04
Mob: +33 06 21 05 19 90
http://jo.irisson.com/

Winston Chang

unread,
Apr 6, 2012, 3:40:41 AM4/6/12
to Jean-Olivier Irisson, Tyler Rinker, ggplot2
Nice sleuthing! But doesn't the right=TRUE argument make it work the old way? (If you look at ?stat_bin, it describes 'right', though it would also help if there was an example in geom_histogram.)

ggplot(diamonds, aes(carat, ..density..)) + 
  geom_histogram(binwidth = 0.2, right = TRUE) +
  facet_grid(.~cut)

-Winston

Jean-Olivier Irisson

unread,
Apr 6, 2012, 8:30:24 AM4/6/12
to Winston Chang, Tyler Rinker, ggplot2
On 2012-Apr-06, at 09:40 , Winston Chang wrote:
>
> Nice sleuthing! But doesn't the right=TRUE argument make it work the old way? (If you look at ?stat_bin, it describes 'right', though it would also help if there was an example in geom_histogram.)

It does and the commit that I highlighted in the previous email basically changes right from TRUE to FALSE. What I am saying is that it might be better to default to what hist() does, to emphasize the fact that ggplot is a (better!) alternative to the regular plotting system of R.

If people get a different result for something as simple as an histogram, they might start to question the validity of gglot (even though both plots are mathematically valid and just use a different point of view). There was a similar debate a few months ago regarding qqnorm and stat_qq and it closed by basing the behaviour of stat_qq on qqnorm I think. And indeed the results are now the same:

set.seed(123)
x <- rnorm(15)
qqnorm(x)
library("ggplot2")
qplot(sample=x)

Winston Chang

unread,
Apr 5, 2012, 10:54:19 PM4/5/12
to Tyler Rinker, pop...@gmx.net, ggplot
It looks like there are slight differences when I compare ggplot2 0.8.9 to 0.9.0. (These are both run on R 2.14.2 on a Mac -- I'm not comparing 2.14 vs 2.15.)

I ran the following code with 0.8.9 and 0.9.0 (and I changed the filenames appropriately).

p <- ggplot(diamonds, aes(carat, ..count..)) + geom_histogram(binwidth = 0.2)

png('0.8.9-facet-grid.png', width=600, height=400)
p + facet_grid(. ~ cut)
dev.off()

png('0.8.9-facet-wrap.png', width=600, height=400)
p + facet_wrap( ~ cut, ncol=5)
dev.off()


I also tested against facet_wrap, since there is an existing bug with facet_grid where it drops duplicate data (), but that doesn't seem to be the issue here.

The results are attached. It looks like something has changed...

-Winston
0.8.9-facet-grid.png
0.8.9-facet-wrap.png
0.9.0-facet-grid.png
0.9.0-facet-wrap.png
Reply all
Reply to author
Forward
0 new messages