Reputation: 313
I want to create 27 histograms of the variable "billed" (numeric, x-axis) for each level of a factor variable "zip" (y-axis is "count"). "zip" has 27 levels.
Is there a way to display 27 histograms on one graph (3X9), no over-laid?
I tried this using ggplot2:
p<-ggplot(dat,aes(x=billed))+geom_histogram(aes(fill=zip),binwidth=1.5)
+facet_wrap(~zip,ncol=9)
The new problem is all these histograms have the same scale. But my data's y-axis/x-axis vary a lot among different zips. Is there a way to create these histograms based on their own scales?
I don't mind using regular r function if this could also be realized by hist(), since the aesthetic features in ggplot2 are not useful for my case.
Upvotes: 1
Views: 4340
Reputation: 263381
require(lattice)
histogram( ~ billed | zip , data=dat,
layout=c(3,9) , scales= list(y=list(relation="free"),
x=list(relation="free") ) )
#worked example from ?histogram page:
histogram( ~ height | voice.part, data = singer,
layout = c(2,4), scales=list(y=list(relation="free"),
x=list(relation="free") ) )
Upvotes: 1