Histogram binwidth by facet?

Showing 1-5 of 5 messages
Histogram binwidth by facet? skipperhoyer 1/9/13 2:34 AM
Hey there!
I'm trying to produce a plot showing the effect of a tranformation of my response variable. I do this by plotting histograms of the raw and transformed data. The default, range/30 gives too many bins. But setting the binwidth in the geom_histogram call sets a universal binwidth across both facets, which is not the result I want.
How do I set binwidth according to a factor? (In this case, the same factor that I am facetting by.)
Minimal working example, with some silly data:
data <- seq(from=0,to=0.2,length=100) #raw response
trans <- log(data/(1-data)) #logit transformed response
frame <- melt(data.frame(data,trans)) #collect
ggplot(data = frame, aes(x=value)) + geom_histogram() + facet_wrap(~variable, scales = "free") #default - too many bins, but they are nicel adjusted for each facet.
ggplot(data = frame, aes(x=value)) + geom_histogram(binwidth = diff(range(dat$value))/5) + facet_wrap(~variable, scales = "free") #what I want - but I want the binwidth to be 1/5 of the range *within each facet*
Thanks in advance!
Re: Histogram binwidth by facet? Benjamin Høyer 1/9/13 3:18 AM
Hey guys
Sorry, my MWE didn't work - here's an improved one.
data <- seq(from=0.001,to=0.2,length=100)
trans <- log(data/(1-data))
frame <- melt(data.frame(data,trans))

ggplot(data = frame, aes(x=value)) + geom_histogram() + facet_wrap(~variable, scales = "free")
ggplot(data = frame, aes(x=value)) + geom_histogram(binwidth = diff(range(frame$value))/5) + facet_wrap(~variable, scales = "free")

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

Re: Histogram binwidth by facet? Brandon Hurr 1/9/13 6:54 AM
Looks like you can if you precalculate things. 

#The example Takahashi-san made above didn't work because bin() is buried in ggplot2
x <- rnorm(100)
df <- ldply(c(0.1, 0.5, 1), function(bw) data.frame(bw, ggplot2:::bin(x, binwidth = bw)))
ggplot(df) + geom_histogram(aes(x, y = density, width = width), stat =
"identity") + facet_wrap(~bw)
unk...@googlegroups.com 1/10/13 1:21 AM <This message has been deleted.>
Re: Histogram binwidth by facet? Pierre Casadebaig 1/10/13 1:26 AM
Hi Benjamin,

You could find another solution in this subject : multiple calls to the same geom allow the use of different bins.