How to merge tapply() and hist() in R?

1.7k views Asked by At

How does one appropriately merge the hist() and tapply() functions in R, in order to get histograms of data subsets? When I attempt this using the standard tapply() formula tapply(X, INDEX, FUN), I successfully get value outputs for each data subset, but I only get one histogram plot with the title 'Histogram of X[[4L]]'. How can I get separate histograms for each subset? Thanks,

1

There are 1 answers

0
G. Grothendieck On BEST ANSWER

Try this:

par(mfrow = c(2, 2))
tapply(iris$Sepal.Length, iris$Species, hist)

enter image description here

however, for multi-panel plots you might find the lattice or ggplot2 plackages more suitable.

library(lattice)
histogram(~ Sepal.Length | Species, iris)

enter image description here

library(ggplot2)
ggplot(iris, aes(Sepal.Length)) + geom_histogram() + facet_wrap(~ Species)

enter image description here