Individual binwidths in faceted histogram on ggplot2

ggplot2, r

Solution

This is not optimal but you can do the histogram in different layers:

ggplot(x, aes(x=data)) +
   geom_histogram(data=subset(x, variable=="a"), binwidth=.1) +
   geom_histogram(data=subset(x, variable=="b"), binwidth=.2) +
   geom_histogram(data=subset(x, variable=="c"), binwidth=.5) +
   facet_grid(area~variable, scales="free")

Problem

I do a series of histograms with `facet_grid` and I want every histogram in the grid to have the same number of classes, in the example below e.g. 6 classes. The problem in this example below is that `binwidth = diff(range(x$data))/6)` defines the classes according to the overall range of a, b and c, i.e. defines one binwidth for all three facets. How do I define binwidth individually for the facets a, b and c? ``` require("ggplot2") a <- c(1.21,1.57,1.21,0.29,0.36,0.29,0.93,0.26,0.28,0.48, 0.12,0.38,0.83,0.82,0.41,0.69,0.25,0.98,0.52,0.11) b <- c(0.42,0.65,0.17,0.38,0.44,0.01,0.01,0.03,0.15,0.01) c <- c(1.09,3.55,1.07,4.55,0.55,0.11,0.72,0.66,1.22,3.04, 2.01,0.64,0.47,1.33,3.44) x <- data.frame(data = c(a,b,c), variable = c(rep("a",20),rep("b",10),rep("c",15)),area="random") qplot(data, data = x, geom = "histogram", binwidth = diff(range(x$data))/6) + facet_grid(area~variable, scales = "free") ```

Original source