[R] Histograms per coding variable - labelling the plots

Kaspar Pflugshaupt pflugshaupt at geobot.umnw.ethz.ch
Fri Dec 7 12:40:01 CET 2001

On 06.12.2001 16:22 Uhr, Smit, A, Albertus, Dr wrote:

>> With some tricks, you can even have a title with the respective label over
>> each plot... but I forgot how I did this.
> Does anyone know how to do this?

Well, i dug it up again. At that time, I really tried to avoid for() loops,
no matter how complicated the resulting code :-) .
Here goes (a simplified example):

par(mfrow=c(2, 1))
d.f <- data.frame(l=rep(c("A", "B"), 4), a=1:8)
labels <- levels(d.f$l)
nlab <- length(labels)
# and the "piece de resistance":
sapply(1:nlab, function(x) hist(d.f$a[d.f$l==labels[x]], main=labels[x]))

This also prints out hist()'s textual output. If you don't want that, put
invisible() around the whole line.

On the whole, the solution with for() that Michael Miller posted is more




Kaspar Pflugshaupt
Geobotanisches Institut
Zuerichbergstr. 38
CH-8044 Zuerich

Tel. ++41 1 632 43 19
Fax  ++41 1 632 12 15

mailto:pflugshaupt at geobot.umnw.ethz.ch
privat:pflugshaupt at mails.ch

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