I've noticed some bug-like behavior in simple usage of geom_smooth() where it seems that the choice of xlim and ylim can affect the computation of smoothed means. A simple example is given below:
library(ggplot2)
set.seed(2)
N <- 100
x <- runif(N)
y <- 0.2 * x + rnorm(N)
qplot(x,y) + geom_smooth(method="loess",lwd=2)
quartz() ## change quartz() to x11(), etc. as needed
qplot(x,y,ylim=c(min(y),max(y)) + c(-1,1) * 0.3 * (max(y)-min(y))) + geom_smooth(method="loess",lwd=2) ## expanding range doesn't change curve shape
quartz()
qplot(x,y,ylim=c(min(y),max(y)) + c(1,-1) * 0.3 * (max(y)-min(y))) + geom_smooth(method="loess",lwd=2) ## shrinking range and cutting off points does change curve shape
quartz()
qplot(x,y, xlim=c(min(x),max(x)) + c(1,-1) * 0.3 * (max(x)-min(x))) + geom_smooth(method="loess",lwd=2) ## same with xlim -- note that we're now below y=0 at x=0.6
It looks to me like the values of the dataset that don't fall in the given xlim and ylim ranges are simply omitted when the smoothed means are computed.
Best
Roger
--
Roger Levy Email: rl...@ucsd.edu
Assistant Professor Phone: 858-534-7219
Department of Linguistics Fax: 858-534-4789
UC San Diego Web: http://idiom.ucsd.edu/~rlevy
You are exactly correct that points falling outside the limits are
excluded from the smooth calculation; however, this is expected
behavior. ggplot2 is more than just a way to blot some ink on a page,
it is an entire way of relating data to a visualization. xlim and
ylim do not "zoom" in or out when used in qplot, they change your
data, and because of the integrated nature of ggplot2, that (the
change in data) changes your graph.
If you want to zoom, use coord_cartesian(). Here is an example:
require(ggplot2)
set.seed(2)
dat <- data.frame(x = runif(100), y = rnorm(100))
qplot(x, y, data = dat) + geom_smooth()
dev.new() # start new default device
qplot(x, y, data = dat) + geom_smooth() +
coord_cartesian(xlim = c(.2, .6))
HTH,
Josh
> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/
Best
Roger