[R] scatter.smooth() line colour

David Winsemius dwinsemius at comcast.net
Fri Jun 22 18:28:22 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

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

-- 
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