[R] offset labeling boxplots

Paul Murrell p.murrell at auckland.ac.nz
Wed Jun 12 04:53:43 CEST 2002


Hi


> often I plot boxplots with different number of boxes (up to 200 boxes).
> I'd like to give every box a readable label on the x-axis. Therefore, I
> decrease the fontsize of the names and plot them vertical.
> But if you zoom into the plot (pdf) you will find an offset between the
> tickmark and the label - the label is shifted to the right.
> 
> If you vary the box.count in the following example (without changing
> the fontsize) you will find every time this right shifted labels.
> (Finaly, for large box.counts the label will be shifted to the next right
> label!)
> 
> Question 1: How can I avoid the right shift of the x-axis label?
> Question 2: Is it possible to scale the fontsize depending of the box.count
>          (So that I can read every single label)?


... and here's yet another approach, this time using the grid package to
draw the entire plot from scratch.  Its a bit lengthy because I had to
create functions to draw a basic boxplot, but it might provide a
sufficient framework for you to build on.  It is also specific to your
problem (although with a few parameters it could be generalised
further).

Paul

## grid boxplot code
library(grid)

grid.bp <- function(stats, width, out) {
  grid.rect(y=unit(stats[2], "native"),
            height=unit(stats[3] - stats[2], "native"),
            width=width,
            just=c("centre", "bottom"))
  grid.rect(y=unit(stats[3], "native"),
            height=unit(stats[4] - stats[3], "native"),
            width=width,
            just=c("centre", "bottom"))
  grid.segments(x0=.5, x1=.5,
                y0=unit(stats[c(1, 4)], "native"),
                y1=unit(stats[c(2, 5)], "native"),
                gp=gpar(lty="dashed"))
  grid.segments(y0=unit(stats[c(1, 5)], "native"),
                y1=unit(stats[c(1, 5)], "native"),
                x0=unit(.5, "npc") - unit(2, "mm"),
                x1=unit(.5, "npc") + unit(2, "mm"))
  if (length(out) > 0)
    grid.points(x=unit(rep(.5, length(out)), "npc"),
                y=unit(out, "native"))
}

grid.bxp <- function(boxplot) {
  nbp <- ncol(boxplot$stats)
  scalevp <- dataViewport(xscale=c(0, 1),
                          y=c(boxplot$stats, boxplot$out))
  push.viewport(scalevp)
  grid.yaxis()
  grid.rect()
  push.viewport(viewport(layout=grid.layout(1, nbp)))
  for (i in 1:nbp) {
    push.viewport(viewport(layout.pos.col=i))
    push.viewport(scalevp)
    grid.bp(boxplot$stats[,i], unit(.8, "npc"),
            boxplot$out[boxplot$group == i])
    grid.lines(y=unit(c(0, -.5), "lines"),
               x=rep(.5, 2))
    grid.text(boxplot$names[i], y=unit(-1, "lines"),
              rot=90, just=c("right", "centre"),
              gp=gpar(fontsize=6))
    grid.text(boxplot$names[i], y=unit(-2, "lines"),
              rot=90, just=c("right", "centre"))
    grid.text(boxplot$names[i], y=unit(-3, "lines"),
              rot=90, just=c("right", "centre"),
              gp=gpar(fontsize=18))
    pop.viewport(2)
  }
}

b <- boxplot(list(rnorm(10), rnorm(10), rnorm(10), rnorm(10), 	
                  rnorm(10), rnorm(10), rnorm(10), rnorm(10), 
                  rnorm(10), rnorm(10)),
             plot=FALSE)
push.viewport(plotViewport(c(5, 4, 4, 2)))
grid.bxp(b)
pop.viewport()
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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