[R] scatter.smooth() line colour

Martin Maechler maechler at stat.math.ethz.ch
Fri Jun 22 19:50:57 CEST 2012


> It's really not that difficult to fix with some simple changes to the  
> code:

> scatter.smooth.col <-
> function (x, y = NULL, span = 2/3, degree = 1, family = c("symmetric",
>      "gaussian"), xlab = NULL, ylab = NULL, ylim = range(y, pred$y,
>      na.rm = TRUE), evaluation = 50, ..., lcol, llty, llwd)
> {
>      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
>      pred <- loess.smooth(x, y, span, degree, family, evaluation)
>      plot(x, y, ylim = ylim, xlab = xlab, ylab = ylab, ...)
>      lines(pred$x, pred$y, col=lcol, lty=llty, lwd=llwd)
>      invisible()
> }

> require(graphics)  # Not really sure why this is needed but it's in  
> the help page

because the function builds on loess() and loess() is part of
'stats' and not 'graphics' (and because all of this is
"historical" ..)

> with(cars, scatter.smooth.col(speed, dist, lcol="red", llwd=3, llty=3))
> # dotted thick smoothed line results

Indeed, this is really trivial and one could argue 
"every" R user should be able to do something like the above.

A slightly nicer solution --- which not every R user will be able to do,
but hopefully (;-) everyone reading R-help for a little while ---
is to allow a *list* of more arguments, all passed to lines(),
actually as Mark proposed {at the very end}, and to use 
do.call(lines, *)

As a little exercise for you to see,
I'll add  that to R-devel .. even though I wonder if its worth
the extra work {{but then, as otherwise someone gets the idea to
    	         file it in the bug tracker ..}}
soon to be visible at
   https://svn.r-project.org/R/trunk/src/library/stats/R/loess.R

Martin Maechler,
ETH Zurich  (and R core)


> -- 
> David


> On Jun 22, 2012, at 10:51 AM, Mark Payne wrote:

> > Hi David,
> >
> > Thanks for the reply. I agree - it just seemed that it was something
> > fairly obvious that was missing from an otherwise handy little
> > function.... Would it be appropriate to file it as a "request for
> > improvement" in the bug tracking system?
> >
> > Mark
> >
> > On 22 June 2012 16:30, David L Carlson - dcarlson at tamu.edu
> > <+r-help+trevva+3b274928d0.dcarlson#tamu.edu at spamgourmet.com> wrote:
> >> You are correct about scatter.smooth, but loess.smooth
> >> (listed along with scatter.smooth on the same
> >> help page) gives you the way to get what you want:
> >>
> >> x <- rnorm(25)
> >> y <- rnorm(25)
> >> plot(x, y)
> >> lines(loess.smooth(x,y), col="red", lty=2, lwd=2)
> >>
> >> ----------------------------------------------
> >> David L Carlson
> >> Associate Professor of Anthropology
> >> Texas A&M University
> >> College Station, TX 77843-4352
> >>
> >>
> >>> -----Original Message-----
> >>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> >>> project.org] On Behalf Of r-help.20.trevva at spamgourmet.com
> >>> Sent: Friday, June 22, 2012 6:04 AM
> >>> To: r-help at r-project.org
> >>> Subject: [R] scatter.smooth() line colour
> >>>
> >>> Hi,
> >>>
> >>> I really like the scatter.smooth() function - its extremeley useful.
> >>> However, as far as I understand it, there is no way to change the
> >>> properties of the smoothing line e.g. col, lty, lwd. The
> >>> scatter.smooth function accepts a ... argument, but these are not
> >>> passed to the plotting function that actually plots the smoother -
> >>> only to the function that plots the points. Could I please therefore
> >>> request that an argument be added to this function to give easier
> >>> control over line properties? e.g. line.par=list()
> >>>
> >>> Best wishes,
> >>>
> >>> Mark Payne
> >>>

> David Winsemius, MD
> West Hartford, CT



More information about the R-help mailing list