[R] Ploting in for() loop

Uwe Ligges ligges at statistik.uni-dortmund.de
Sat May 25 12:52:57 CEST 2002


Ko-Kang Kevin Wang wrote:
> 
> Hi,
> 
> Suppose I have a for() loop that draws 6 boxplots as follows:
>   par(mfrow = c(2, 3))
>   for(i in 2:length(spam.sample)) {
>     boxplot(split(spam.sample[,i], yesno))
>   }
> 
> Where spam.sample is a data frame with 7 columns, and I'm interested in
> plotting column 2 ~ 7 against column 1 (yesno).
> 
> The boxplots appeared fine, however I'm trying to add a meaningful title,
> x and y labels to them.  Is it possible to do this in the loop?

Yes. Why not?

Some obvious solutions:

a) With a vector of names, let's call it main.labels:
  boxplot(..., main = main.labels[i])

b) To take the name of the data in your call:
  boxplot(..., main = deparse(substitute(spam.sample[,i], list(i=i))))

c) Or the column name of the plotted column:
  boxplot(..., main = colnames(spam.sample)[i])

Uwe Ligges
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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