different loess fit in ggplot2 and lattice

483 views
Skip to first unread message

Xiaoyu Yan

unread,
May 8, 2011, 9:43:23 PM5/8/11
to ggp...@googlegroups.com
Hello,

I was trying to use ggplot2 to reproduce one of the graph I produced with lattice. It is scatter plot fit with loess smooth curve. The code for lattice is as follows:

xyplot( DV ~ IPRE, subset = (CMT==7 & MDV == 0 & DRUG==1), data = dv_ipred_pred,
xlab = "Individual fit ", ylab = "Observation ",
scales=list(x=list(relation = "same",tck=c(1,0)), y = list(relation = "same",tck=c(1,0))),
xlim=c(0,350),
ylim=c(0,350),
main = "Individual fits vs. Observation Plot (linear scale)\nRET CMT: 7",
strip=function(...) strip.default(..., strip.names=T,
strip.levels=T, style=3, sep = ": "),
layout = c(1,1),
as.table=T,
key = key.content,
panel = function(x, y, ...) {
    panel.xyplot(x,y, col="gray65", pch=1.1, cex=1.1)
    panel.abline(0,1, col="darkgray", lwd=2,lty=2)
    panel.loess(x, y, span = 1, col="black", lwd=3)
} )

The code using ggplot2 is :

ggplot(data=EPO_RET2, aes(IPRE,DV))+
geom_point(colour="grey65",size=4,shape=1)+
geom_abline(intercept=0,slope=1,linetype=2,color="darkgray",size=1.5)+
coord_cartesian(xlim=c(0,350),ylim=c(1,350))+
opts(panel.grid.minor=theme_blank())+opts(panel.grid.major=theme_blank())+
geom_smooth(method = "loess",span = 1 ,color="red",size = 1.5)


The problem is that they look different as shown in the pics. The one generated with ggplot2 exhibit more curvature and it has a shaded band around the loess curve.
I am wondering why is that and how I can produce the similar graph as done in the lattice. Thanks

Xiaoyu



 

lattice.png
ggplot2.png

Kohske Takahashi

unread,
May 8, 2011, 10:34:04 PM5/8/11
to Xiaoyu Yan, ggp...@googlegroups.com
Hi

probably it is because ggplot2 uses defaults loess params, while
lattice changes some settings:

see definition of
> panel.loess
and
> StatSmooth$with(calculate)

and maybe this code helps:

d <- data.frame(x=rnorm(100), y=rnorm(100))

dev.new()
xyplot( y~x, data = d, panel = function(x, y, ...) {


panel.xyplot(x,y, col="gray65", pch=1.1, cex=1.1)

panel.loess(x, y, span = 1, degree=1, family="gaussian", col="black", lwd=3)
} )

dev.new()
ggplot(d, aes(x,y)) + stat_smooth(span=1, degree=1, family="gaussian")
+ geom_point()

--
Kohske Takahashi <takahash...@gmail.com>

Research Center for Advanced Science and Technology,
The University of  Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

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

Tom Hopper

unread,
May 9, 2011, 9:51:03 AM5/9/11
to Xiaoyu Yan, ggp...@googlegroups.com
Xiaoyu,

Regarding the gray band in the ggplot2 graph: that is the 95% confidence band on the estimate of the loess curve. It can be turned off via parameter se=FALSE to qplot(); I'm not sure of the corresponding parameter for ggplot() and related functions.

Also, Hadley's book indicates that loess is used on data sets less than 1000 points, and an alternative smoother is used on larger data sets. It looks like you might be exceeding that limit, so that you could be comparing slightly different smoothers.

- Tom

pharmpk

unread,
May 11, 2011, 11:50:46 AM5/11/11
to ggplot2
Hey,

Thanks a lot for your guys' input.

Xiaoyu

On May 9, 9:51 am, Tom Hopper <tomhop...@gmail.com> wrote:
> Xiaoyu,
>
> Regarding the gray band in the ggplot2 graph: that is the 95% confidence
> band on the estimate of the loess curve. It can be turned off via parameter
> se=FALSE to qplot(); I'm not sure of the corresponding parameter for
> ggplot() and related functions.
>
> Also, Hadley's book indicates that loess is used on data sets less than 1000
> points, and an alternative smoother is used on larger data sets. It looks
> like you might be exceeding that limit, so that you could be comparing
> slightly different smoothers.
>
> - Tom
>
> On Mon, May 9, 2011 at 04:34, Kohske Takahashi
> <takahashi.koh...@gmail.com>wrote:
>
>
>
>
>
>
>
> > Hi
>
> > probably it is because ggplot2 uses defaults loess params, while
> > lattice changes some settings:
>
> > see definition of
> > > panel.loess
> > and
> > > StatSmooth$with(calculate)
>
> > and maybe this code helps:
>
> > d <- data.frame(x=rnorm(100), y=rnorm(100))
>
> > dev.new()
> > xyplot( y~x, data = d, panel = function(x, y, ...) {
> >  panel.xyplot(x,y, col="gray65", pch=1.1, cex=1.1)
> >  panel.loess(x, y, span = 1, degree=1, family="gaussian", col="black",
> > lwd=3)
> > } )
>
> > dev.new()
> > ggplot(d, aes(x,y)) + stat_smooth(span=1, degree=1, family="gaussian")
> > + geom_point()
>
> > --
> > Kohske Takahashi <takahashi.koh...@gmail.com>
>
> > Research Center for Advanced Science and Technology,
> > The University of  Tokyo, Japan.
> >http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html
>
> > On Mon, May 9, 2011 at 10:43 AM, Xiaoyu Yan <yanxiaoyu...@gmail.com>
Reply all
Reply to author
Forward
0 new messages