[R] Graphics function question

Marc Schwartz marc_schwartz at comcast.net
Tue Jul 29 20:45:12 CEST 2008


Just a quick follow up to my own post here. In offline exchanges, Peter 
noted a typo in the call to plot(), where I was missing the final ")". 
Also, I had the order of the unnamed arguments to segments() off.

Here is the corrected code, with some additional teaks:


plotdiffs <- function(x, y, startage, endage, decline)
{
   startvar <- x - y
   endvar <- x

   s1 <- seq(47, 89, length = 10)

   ymin <- min(startvar, endvar, na.rm = TRUE)
   ymax <- max(startvar, endvar, na.rm = TRUE)

   s2 <- seq(ymin, ymax, length = 10)

   label <- gsub("^.+\\$", "", deparse(substitute(x)))

   plot(s1, s2, type = "n",  xlab = "Age",  ylab = label,
        main = paste("Age, decline and", label))

   segments(startage, startvar, endage, endvar,  col = decline)

   legend("topleft", legend = c("Stable", "Decline"), lty = 1,
          col = c(1, 2))
}



Marc



More information about the R-help mailing list