From kwright at eskimo.com  Mon May  2 19:07:27 2005
From: kwright at eskimo.com (kwright@eskimo.com)
Date: Mon May  2 19:07:38 2005
Subject: [Rd] Revised scatter.smooth using xy.coords
Message-ID: <14309.170.54.59.167.1115053647.squirrel@170.54.59.167>


This started when I discovered that scatter.smooth (R 2.1.0) has an
undocumented ability to use a formula as the first argument.  Then I
discovered the xy.coords function, which seems like a natural fit and a
way to unify scatter.smooth with other plotting functions, so I offer here
a revised version of scatter.smooth that uses the xy.coords function.


scatter.smooth <- function (x, y=NULL, span = 2/3, degree = 1,
                            family = c("symmetric", "gaussian"),
                            xlab = NULL,
                            ylab = NULL,
                            ylim = range(y, prediction$y, na.rm = TRUE),
                            evaluation = 50, ...) {
  xlabel <- if (!missing(x))
    deparse(substitute(x))
  ylabel <- if (!missing(y))
    deparse(substitute(y))
  xy <- xy.coords(x, y, xlabel, ylabel)

  x <- xy$x
  y <- xy$y
  xlab <- if(is.null(xlab)) xy$xlab
  else xlab
  ylab <- if(is.null(ylab)) xy$ylab
  else ylab

  #  if (inherits(x, "formula")) {
  #    if (length(x) < 3)
  #      stop("need response in formula")
  #    thiscall <- match.call()
  #    thiscall$x <- x[[3]]
  #    thiscall$y <- x[[2]]
  #    return(invisible(eval(thiscall, sys.parent())))
  #  }
  prediction <- loess.smooth(x, y, span, degree, family, evaluation)
  plot(x, y, ylim = ylim, xlab = xlab, ylab = ylab, ...)
  lines(prediction)
  invisible()
}

1. I commented out some existing code and my revisions appear before the
commented block.

2. The argument list has changed slightly.  The original arguments were:
y
xlab=deparse(substitute(x))
ylab=deparse(substitute(y))

3.  With the suggested change, the following examples now work as one
would expect

attach(cars)
scatter.smooth(speed,dist,main="dist,speed")
scatter.smooth(speed,dist,main="dist,speed",xlab="",ylab="")
scatter.smooth(cars,main="cars")
scatter.smooth(cars,main="cars",xlab="Speed",ylab="Distance")
scatter.smooth(dist~speed,main="dist~speed")
scatter.smooth(dist~speed,main="dist~speed",xlab="")
scatter.smooth(dist~speed,main="dist~speed",ylab="")

4. If this revision is accepted, the help page for scatter.smooth should
probably be updated, perhaps using the same definition of the x,y
arguments in the plot.default help page.

Best,

Kevin Wright


[Rd] Revised scatter.smooth using xy.coords
kwright@eskimo.com