Changing Y axis also changes stat_smooth fit lines

327 views
Skip to first unread message

James Porter

unread,
Oct 18, 2010, 1:33:55 PM10/18/10
to ggplot2
I've run across a curious problem with stat_smooth(). I've found that
the manner in which the function fits a curve to the data points is
somehow dependent upon the limits placed upon the Y axis (changes to
the X axis have no effect). I have three sets of code below, and the
only difference is in ylim(). However, instead of just "zooming in/
out," the fit lines and the standard error shadings change quite
dramatically. It seems that the way they are drawn is dependent upon
the data points that are visible, rather than the full data set fed
into ggplot(data=). My first hope was that fullrange=T would fix it,
but no. I haven't been able to find any parameters that make the issue
go away or attenuate. Could you offer other avenues that I might
explore to solve this riddle?

The plots in question can be found here:
https://netfiles.umn.edu/xythoswfs/webui/_xy-16968792_1-t_CKvWxdm8

Thanks,
Jim

########################################
########################################
g1 <- ggplot(data=ST.diff.plot.long, aes(x=TR, y=SigChng, shape=HiLo,
colour=HiLo)) + geom_point()
g2 <- g1 + stat_smooth(method="loess", span=.3, se=T, lwd=2)
g3 <- g2 + scale_colour_manual("HiLo", 3:4) + opts(title="Medial OFC
psplot of [(S hi-lo) - (T hi-lo)] -- Split by HiLo ALT") +
ylim(c(-13,13)) + xlim(c(1,11)) + ylab("Standardized Signal Change")
print(g3)

g1 <- ggplot(data=ST.diff.plot.long, aes(x=TR, y=SigChng, shape=HiLo,
colour=HiLo)) + geom_point()
g2 <- g1 + stat_smooth(method="loess", span=.3, se=T, lwd=2)
g3 <- g2 + scale_colour_manual("HiLo", 3:4) + opts(title="Medial OFC
psplot of [(S hi-lo) - (T hi-lo)] -- Split by HiLo ALT") +
ylim(c(-3,3)) + xlim(c(1,11)) + ylab("Standardized Signal Change")
print(g3)

g1 <- ggplot(data=ST.diff.plot.long, aes(x=TR, y=SigChng, shape=HiLo,
colour=HiLo)) + geom_point()
g2 <- g1 + stat_smooth(method="loess", span=.3, se=T, lwd=2)
g3 <- g2 + scale_colour_manual("HiLo", 3:4) + opts(title="Medial OFC
psplot of [(S hi-lo) - (T hi-lo)] -- Split by HiLo ALT") + ylim(c(-.
3,.3)) + xlim(c(1,11)) + ylab("Standardized Signal Change")
print(g3)
########################################
########################################

Brandon Hurr

unread,
Oct 18, 2010, 1:46:07 PM10/18/10
to James Porter, ggplot2
When you set the limits on a scale you are essentially taking a cut of the data before plotting, and the cut data is the data that is fed to stat_smooth()

Instead of using limits, using coord_cartesian() to "zoom" in. 

Examples, here:

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

Reply all
Reply to author
Forward
0 new messages