[R] Plotting an array of histograms

Mark Myatt mark at myatt.demon.co.uk
Mon Jul 30 12:59:55 CEST 2001

Ted.Harding at nessie.mcc.ac.uk writes:
>I have a dataset (of say values of X), each value associated
>with a level of a factor F (25 levels in all), in a dataframe.
>I want to make a single plot which contains 25 histograms of X,
>one for each level of F and each one appropriately labelled.
>(And, since there are 25 levels, a 5x5 array would be nice).
>What's the trick in R, please?

The trick is to the par() function. Here is an example ...

        # generate some dummy data
        x <- rnorm(n = 1000, mean = 12, sd = 1)
        f <- round(runif(n = 1000, min = 1, max = 25))

        # set device for 5 * 5 array of charts
        par(mfrow = c(5,5))

        # plot histograms
        tapply(x, f, hist, ylab="n", xlab="x")

I think you'll need a for() loop to get better control over the plots.
Something like:

        for(i in min(f):max(f))
          hist(x[f==i], xlim=range(x), ylab="n", xlab="x", main = i)

But I might be wrong.

I hope that helps.


Mark Myatt

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list