On Sep 17, 2014, at 4:34 PM, Michael Friendly wrote:
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))
This is what I would have expected to be a more accurate plot of the
estimated central tendencies:
with(CPpos, lines(lowess(length, exp(log(intensity))), col="red",
lwd=2) )
lines( CPpos$length, exp( lm(log(intensity) ~ length, data=CPpos)
$fitted) )
Just because you have changed the scaling of the plot axis does not
mean that you would plot log()-ed values.
Here's an attempt at a ggplot2 version, that actually looks more
reasonable, but I'm not sure that it
is correct:
library(ggplot2)
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) +
stat_smooth(method="lm")
The loess line is somewhat different but the lm() prediction is the
same as I expected.
--
David.
David Winsemius, MD
Alameda, CA, USA
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.