[R] arranging pie charts in a matrix layout with row/col labels

Rajarshi Guha rajarshi.guha at gmail.com
Mon Jan 31 23:34:39 CET 2011

Hi, I have a vector of data, that I group based on two factors via
tapply. For each such grouping I would like to plot  a pie chart. I
can layout these pie charts in a matrix layout, correpsonding to the
levels of the two factors. But I am getting stuck on how to label the
rows and colums. My current approach looks like this:

x <- data.frame(obs=sample(c('low', 'high'),100, replace=TRUE),
                grp1=sample(1:10, 100, replace=TRUE),

cut.grp1 <- cut(x$grp1, 3)
cut.grp2 <- cut(x$grp2, 3)

tapply(x$obs, list(cut.grp1, cut.grp2), function(z) {
  pie(table(z), col=c('red', 'green'))

One possibility is to add an extra row and column to the layout and
loop over the results of the tapply, adding a text label for the row
and column labels.

Is there a better way to achieve this?

Rajarshi Guha
NIH Chemical Genomics Center

More information about the R-help mailing list