Error when using facet_wrap on geom_histogram

265 views
Skip to first unread message

Spekolator

unread,
Apr 17, 2012, 8:29:45 AM4/17/12
to ggp...@googlegroups.com
Hi 

Just updated to ggplot 0.9.0 and found what I believe is a bug with 
the facet_wrap. 

simple example: 

t <- data.frame(num = c(1,1,2,2), let=c('a','a','b','b')) 

first I look at the percentages of 1 and 2 in my data set. 

ggplot(t, aes(num))+geom_histogram(aes(y = ..count../ 
sum(..count..)*100)) 

not surprisingly i see that 50% is in each bin 

Now I want to facet on the 'let' parameter ('a' and 'b'.) 

ggplot(t, aes(x))+geom_histogram(aes(y = ..count../sum(..count..)*100)) 
+facet_wrap(~let)

In 0.8.9 100% of the observations are in the single bin for each facet 
(as they should), but with 0.9.0 I get 50% in each graph (like the 
sum(..count..) is made for all data and not the part that I facet on)  

Have anyone had the same experience and is there a work around this?

Winston Chang

unread,
Apr 19, 2012, 2:17:31 PM4/19/12
to Spekolator, ggp...@googlegroups.com
Hm, I'm not sure what the correct behavior should be. I don't know if this is exactly how it worked, but it seems that in 0.8.9, it was dividing up into facets before doing the count, and in 0.9.0, it's doing the count before the facets.

-Winston

--
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

hist-facets-0.8.9.png
hist-facets-0.9.0.png

Spekolator

unread,
Apr 20, 2012, 3:09:32 AM4/20/12
to ggp...@googlegroups.com, Spekolator
Hi Winston

Thanks for your reply.

I am pretty sure that the correct behavior is like it was in 0.8.9. Otherwise there is no point in faceting out in this example. 

Do you have a workaround that could sove this?

Sincerely

Rasmus Engholm


On Thursday, April 19, 2012 8:17:31 PM UTC+2, winston wrote:
Hm, I'm not sure what the correct behavior should be. I don't know if this is exactly how it worked, but it seems that in 0.8.9, it was dividing up into facets before doing the count, and in 0.9.0, it's doing the count before the facets.

-Winston

On Tue, Apr 17, 2012 at 7:29 AM, Spekolator wrote:
Hi 

Just updated to ggplot 0.9.0 and found what I believe is a bug with 
the facet_wrap. 

simple example: 

t <- data.frame(num = c(1,1,2,2), let=c('a','a','b','b')) 

first I look at the percentages of 1 and 2 in my data set. 

ggplot(t, aes(num))+geom_histogram(aes(y = ..count../ 
sum(..count..)*100)) 

not surprisingly i see that 50% is in each bin 

Now I want to facet on the 'let' parameter ('a' and 'b'.) 

ggplot(t, aes(x))+geom_histogram(aes(y = ..count../sum(..count..)*100)) 
+facet_wrap(~let)

In 0.8.9 100% of the observations are in the single bin for each facet 
(as they should), but with 0.9.0 I get 50% in each graph (like the 
sum(..count..) is made for all data and not the part that I facet on)  

Have anyone had the same experience and is there a work around this?

--
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

Winston Chang

unread,
Apr 23, 2012, 3:54:39 AM4/23/12
to Spekolator, ggp...@googlegroups.com
I'm still not sure that the "correct" behavior should be for the bars in each facet to add up to 100. In the current version, the behavior of facets is the same as the behavior of using an aesthetic like 'fill': the sum of all the bars is 100. I can see why it would also make sense for each facet to add to 100, though; it would be like treating each facet as its own graph.

Anyway, I tried a few different things but I couldn't come up with a simple way to make it work the you want. Maybe the best thing to do is bin and scale the data yourself. It can be done by using ddply() and hist():

library(plyr)
set.seed(204)
dat <- data.frame(val = rnorm(30), g = sample(letters[1:3], 30, replace=T))

# This generates a data frame of counts for a histogram
myhist <- function(df, colname, breaks) {
  h <- hist(df[[colname]], breaks = breaks, plot = F)
  xmin <- h$breaks[-length(h$breaks)]  # Min value for each bin
  xmax <- h$breaks[-1]                 # Max value for each bin
  data.frame(xmin, xmax, count = h$counts, total = sum(h$counts))
}

# Demostration of myhist: this returns a data frame of the counts for
# the whole data frame
myhist(dat, "val", seq(min(dat$val), max(dat$val), length.out=5))
#       xmin       xmax count total
# -3.3133854 -1.9180915     2    30
# -1.9180915 -0.5227977     9    30
# -0.5227977  0.8724962    17    30
#  0.8724962  2.2677900     2    30

# Now do the same thing, but split by group
hdat <- ddply(dat, .(g), myhist, "val", breaks = seq(min(dat$val), max(dat$val), 
          length.out=5))
# g       xmin       xmax count total
# a -3.3133854 -1.9180915     1     6
# a -1.9180915 -0.5227977     1     6
# a -0.5227977  0.8724962     3     6
# a  0.8724962  2.2677900     1     6
# b -3.3133854 -1.9180915     0    10
# b -1.9180915 -0.5227977     4    10
# b -0.5227977  0.8724962     6    10
# b  0.8724962  2.2677900     0    10
# c -3.3133854 -1.9180915     1    14
# c -1.9180915 -0.5227977     4    14
# c -0.5227977  0.8724962     8    14
# c  0.8724962  2.2677900     1    14

hdat$countscaled <- hdat$count / hdat$total
# g       xmin       xmax count total countscaled
# a -3.3133854 -1.9180915     1     6  0.16666667
# a -1.9180915 -0.5227977     1     6  0.16666667
# a -0.5227977  0.8724962     3     6  0.50000000
# a  0.8724962  2.2677900     1     6  0.16666667
# b -3.3133854 -1.9180915     0    10  0.00000000
# b -1.9180915 -0.5227977     4    10  0.40000000
# b -0.5227977  0.8724962     6    10  0.60000000
# b  0.8724962  2.2677900     0    10  0.00000000
# c -3.3133854 -1.9180915     1    14  0.07142857
# c -1.9180915 -0.5227977     4    14  0.28571429
# c -0.5227977  0.8724962     8    14  0.57142857
# c  0.8724962  2.2677900     1    14  0.07142857

ggplot(hdat) + 
  geom_rect(aes(xmin=xmin, xmax=xmax, ymin=0, ymax=countscaled * 100),
    colour="black", fill="white") +
  facet_grid(g ~ .)


-Winston

hist.png
Reply all
Reply to author
Forward
0 new messages