1

I have a data matrix with approximately one hundred variables and I want to do box plots of these variables. Doing them one by one is possible, but tedious. The code I use for my box plots is:

    boxplot(myVar ~ Group*Trt*Time,data=exp,col=c('red','blue'),frame.plot=T,las=2, ylab='Counts', at=c(1,2,3,4,6,7,8,9,11,12,13,14,16,17,18,19))

I started doing them one by one, but realized there must be better options. So, the boxplot call will take only one variable at at time (I may be wrong), so I am looking for a way to get it done in one go. A for loop? Next, I would like to print the name of the current variable (= the colName) on the plot in order to keep them apart.

Appreciate suggestions. Thank you. jd

SigneMaten
  • 493
  • 1
  • 6
  • 13

2 Answers2

1

Why not try the following:

data(something)
panel.bxp <- function(x, ...)
{
    a <- par("a"); on.exit(par(a))
    par(a = c(0, 2, a[3:4]))
    boxplot(x, add=TRUE)
}

Then, to run the function, you can try something like the following:

pairs(something, diag.panel = panel.bxp, text.panel = function(...){})

EDIT: There is also a nice link to an article here on R-bloggers which you might want to have a look at.

Nathaniel Payne
  • 2,749
  • 1
  • 28
  • 32
0

Being very new to R, I've tried to follow my 'old' thinking - making a for-loop. Here is what I came up with. Probably very primitive, and therefore, I'd appreciate comments/suggestions. Anyway: the loop:

    for (i in 1:ncol(final)) {
      #print(i)
      c <- colnames(final)[i]
      #print(c)
      b <- final[,i]
      #b <- t(b)
      #dim(b)
      #print(b)
      exp <- data.frame(Group,Trt,Time,b)
      #dim(exp)
      #print(exp)
      boxplot(b ~ Group*Trt*Time,data=exp,col=c('red','blue'),frame.plot=T, las=2, ylab='Counts',main=c, at=c(1,2,3,4,6,7,8,9,11,12,13,14,16,17,18,19))

    }

The loop runs through the data matrix 'final', (48rows x 67cols). Picks up the column header, c, which is used in the boxplot call as main title. Picks up the data column, b. Sets up the experiment using the Group, Trt, and Time factors established outside the loop, and calls the boxplot. This seem to do what I want. Oddly, Rstudio does not allow more than 25 (approx) plots to be stored in the plots console, so I have to run this loop in a couple of rounds. Anyway, sorry for answering my own question. Better solutions are greatly appreciated since my way is pretty amateourish, I suspect.

SigneMaten
  • 493
  • 1
  • 6
  • 13
  • Don't be sorry for answering your own question. That is [perfectly fine](http://stackoverflow.com/help/self-answer). I believe a major reason for lack of response on your question was that you didn't provide a **minimal, self contained example**. Please check these links for general ideas, and how to do it in R: [**here**](http://stackoverflow.com/help/mcve), [**here**](http://www.sscce.org/), and [**here**](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610), for next time. Cheers. – Henrik Apr 08 '14 at 20:48