[R] plots on log="y" scale with smooths

Michael Friendly friendly at yorku.ca
Wed Sep 17 22:34:18 CEST 2014

In the following example, I am trying to plot a response on a log scale, 
and add one or more smoothed
curves, e.g., lowess and abline.  In base graphics, I'd like to do this 
using log="y", so that the Y axis is
spaced on the log scale, but labeled as the actual response values. 
Using ggplot2, I'm using
scale_y_log10 to achieve the same purpose.  However, my attempts to add 
the smooths differ
considerably, so I must be missing something.

Here's the data I'm working with for one example:

data("CodParasites", package = "countreg")
## omit NAs in response & predictor
CodParasites <- subset(CodParasites, !(is.na(intensity) | is.na(length)))
## plot only positive values of intensity
CPpos <- subset(CodParasites, intensity>0)

Here's the base graphics plot.  The abline() is clearly wrong and the 
lowess smooth looks too low.
How does one meld plots using log="y" with such additional plot 
annotations ?

plot(jitter(intensity) ~ length, data = CPpos, log = "y")
with(CPpos, lines(lowess(length, log(intensity)), col="red", lwd=2) )
abline(lm(log(intensity) ~ length, data=CPpos))

Here's an attempt at a ggplot2 version, that actually looks more 
reasonable, but I'm not sure that it
is correct:

ggplot(CPpos, aes(x=length, y=intensity)) +
     geom_jitter(position=position_jitter(height=.1), alpha=0.25) +
     scale_y_log10(breaks=c(1,2,5,10,20,50,100, 200)) +
     stat_smooth(method="loess", color="red", size=1.5) +

Michael Friendly     Email: friendly AT yorku DOT ca
Professor, Psychology Dept. & Chair, Quantitative Methods
York University      Voice: 416 736-2100 x66249 Fax: 416 736-5814
4700 Keele Street    Web:http://www.datavis.ca
Toronto, ONT  M3J 1P3 CANADA

More information about the R-help mailing list