Re: [R] Lattice graphics: adding lines to a plot

From: Deepayan Sarkar <deepayan_at_stat.wisc.edu>
Date: Thu 29 Jul 2004 - 23:19:53 EST

On Thursday 29 July 2004 06:37, Chuck Cleland wrote:
> How about this?
>
> library(lattice)
> xyplot(y ~ x, panel = function(x, y, ...){
> panel.xyplot(x, y, ...)
> panel.lmline(x, y, type="l")
> })

That should do it, except the type="l" is not required (panel.lmline always draws a line anyway). There's also a shortcut for this, namely,

xyplot(y ~ x, type = c("p", "r"))

Depending on how you would want to generalize this to more than one panel, you could also try

g <- lm( y ~ x )
xyplot(y ~ x,

       panel = function(x, y, ...){
           panel.xyplot(x, y, ...)
           panel.abline(g, ...)
       })

which draws the same 'global' fit in all panels.

Deepayan

> Matthew Walker wrote:
> > I am trying to use the Lattice package to produce the same result
> > as I can with the base graphics.
> >
> > With base graphics I can type:
> >
> > x <- 1:100
> > y <- x+rnorm(length(x))
> > g <- lm( y ~ x )
> > model.y <- g$coef[1]+g$coef[2]*x
> >
> > plot(x,y) # a plot of the data
> > lines( x, model.y ) # a plot of the model, superimposed on the
> > first plot
> >
> >
> > With Lattice graphics I've got this far:
> >
> > library(lattice)
> > plot1 <- xyplot(y ~ x)
> > plot2 <- xyplot(model.y ~ x, panel = function(x, y) { llines(x=x,
> > y=y) }) print(plot1, more=TRUE, position=c(0,0.5,1,1))
> > print(plot2, position=c(0,0,1,0.5))
> >
> > But this just produces two graphs, one above the other. How do I
> > overlay them? What should I be doing in order to get the same
> > result as with the base graphics?



R-help@stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Jul 29 23:25:17 2004

This archive was generated by hypermail 2.1.8 : Fri 18 Mar 2005 - 02:41:16 EST