----------------------- *** Disclaimer *** -----------------------
This e-mail and its contents are subject to the SA Reserve Bank's
Disclaimer and Confidentiality Clause, which can be viewed at:
http://www.reservebank.co.za/disclaimer
Should you be unable to access the link provided, please send a
blank e-mail to Disclaimer@resbank.co.za 
----------------------- *** Disclaimer *** -----------------------


Dear list,

Following the grid package vignette as well as slides by Paul Murrell, I have tried to implement a boxplot using the graphical primitives from the grid package.

I, however, always seem to have problems with the scale or rather, the data do not match the axis. Below is the function and code run. I am using R2.9.0

# Function
bxplt <- function(x, range = 2){
  require(grid)
  bxp <- boxplot(x, range = range, plot = FALSE)
  O <- bxp$out
  On <- length(O)
  grid.yaxis()
  grid.lines(
   rep(unit(0.5, "npc"), 2),
   c(unit(bxp$stats[1], "native"),
   unit(bxp$stats[5], "native")),
   name = "whiskers"
   )
  grid.rect(x = unit(0.5, "npc"),
   y = unit(0.5, "npc"),
   just = c("centre", "bottom"),
   height = unit(bxp$stats[4], "native") - unit(bxp$stats[2], "native"),
   width = unit(0.5, "npc"),
   gp = gpar(fill = "grey"),
   name = "box"
   )
  grid.lines(c(unit(0.25, "npc"), unit(0.75, "npc")),
   rep(unit(bxp$stats[3], "native"), 2),
   gp = gpar(lwd = 2),
   name = "median"
   )
  if(On != 0){
   grid.points(x = rep(unit(0.5, "npc")), y = O)
   }
  }
# Code run
x <- rnorm(100)
pushViewport(plotViewport())
pushViewport(dataViewport(yData = x, xscale = c(0, 1)))
grid.yaxis()
bxplt(x)

Additionally, some people might be wondering why I am reimplementing the bocplo using grid. The reason is that I need to annotate the plot with something a little more elegant than arrows... I wrote the following function, which seems to work, but I'm not even sure how I'll get it to work with the above.

# Bracket function
Brack <- function(label, x = 0.5, y = 0.5){
  require(grid)
  llab <- stringWidth(label)
  vpBrack <- viewport(x = x, y = y, height = unit(1, "npc"), width = llab + unit(4, "char"))
  pushViewport(vpBrack)
  grid.curve(unit(0, "char"),
     unit(0, "npc"),
     unit(4, "char"),
     unit(0.5, "npc"),
     inflect = TRUE, curvature = 1)
  grid.curve(unit(0, "char"),
     unit(1, "npc"),
     unit(4, "char"),
     unit(0.5, "npc"),
     inflect = TRUE, curvature = -1)
  grid.text(label, x = unit(6, "char"), y = unit(0.5, "npc"), just = "left", hjust = "center")
  #popViewport()
  upViewport()
  }

Any suggestions whatsoever are most welcome.

Kind regards,

Stefan Janse van Rensburg

	[[alternative HTML version deleted]]

