2

I'm trying to smooth points from a scatterplot. There are two groups, and I want to do the smoothing on each of them separately.

In order to illustrate the problem that is happening, I generate data according to

set.seed(400)
x=seq(0,2,0.01)
Data=data.frame(x=rep(x,2),y=c(rnorm(5*x,0,0.05),rnorm(5*x+0.5,0,0.05)),z=c(rep("1",length(x)),rep("2",length(x))))

Here, z represents the groups.

If I plot the smoothed curves using

ggplot(Data, aes(x,y)) + geom_smooth(aes(color=z,linetype=z),method="gam",size=2,se=FALSE)+scale_y_continuous(limits=c(-0.01,0.01))

I get

enter image description here

However, if I change the y axis limits, I get a different result:

ggplot(Data, aes(x,y)) + geom_smooth(aes(color=z,linetype=z),method="gam",size=2,se=FALSE)+scale_y_continuous(limits=c(-0.005,0.005))

enter image description here

In other words: the only thing I've changed are the y-axis limits, however I get different regression estimates.

Any ideas of why this happens and how I can correct it?

Thanks!

madness
  • 371
  • 1
  • 3
  • 14
  • 2
    I think your code subsets the observations, in contrast to limiting y by using `coord_cartesian(ylim=c(...))`. – lukeA Feb 28 '15 at 17:23

0 Answers0