[R] abline and plot(augPred) help

Petr Pikal petr.pikal at precheza.cz
Tue Sep 5 10:59:31 CEST 2006


I would like to thank to Gabor Grothendieck, Paul Murrel and Maria 
Gabriela Cendoya for their helpful answers.

Based on Gabors code here is a solution for adding lines to lattice 
plots which works smoothly on augPred plots.

addLine<- function(a, b=NULL, v = NULL, h = NULL, ...) {
tcL <- trellis.currentLayout()
for(i in 1:nrow(tcL))
  for(j in 1:ncol(tcL))
    if (tcL[i,j] > 0) {
        trellis.focus("panel", j, i, highlight = FALSE)
        panel.abline(a=a, b=b, v=v, h=h, ...)
        trellis.unfocus()
        }
}

Best regards.
Petr Pikal


On 4 Sep 2006 at 16:37, Gabor Grothendieck wrote:

Date sent:      	Mon, 4 Sep 2006 16:37:00 -0400
From:           	"Gabor Grothendieck" <ggrothendieck at gmail.com>
To:             	"Paul Murrell" <p.murrell at auckland.ac.nz>
Copies to:      	Petr Pikal <petr.pikal at precheza.cz>, r-help at stat.math.ethz.ch
Subject:        	Re: [R] abline and plot(augPred) help

> On 9/4/06, Paul Murrell <p.murrell at auckland.ac.nz> wrote:
> > Hi
> >
> >
> > Petr Pikal wrote:
> > > Dear all
> > >
> > > as I did not get any response on my post about abline and
> > > plot(augPred)) I try again. I hope I do not break some posting
> > > guide rules. I would try to contact package maintainer directly
> > > but there is stated to be R-core people, so I feel R-help list
> > > shall be OK.
> > >
> > > I need to draw straight lines through augPred plotted panels
> > > (vertical or horizontal) at specified point. I know I shall
> > > probably use panel.abline but I am missing correct syntax. Below
> > > you can see my attempts together with results. I hope somebody can
> > > point me to right direction.
> > >
> > > I am probably somewhere close but I have no clue, which parameter
> > > I shall modify to get measured points, fitted lines and vertical
> > > lines in panels together.
> >
> >
> > The problem is that you do not know about the default panel function
> > that nlme:::plot.augPred() uses, so your panel functions are not
> > replicating all of the default behaviour as well as adding your
> > vertical lines.  Some possible solutons suggested below ...
> >
> >
> > > fm1 <- lme(Orthodont)
> > >
> > > # standard plot
> > > plot(augPred(fm1, level = 0:1, length.out = 2))
> > >
> > > #plot with vertical but without points and fitted lines
> > > plot(augPred(fm1, level = 0:1, length.out = 2),
> > > panel=function(v,...) {
> > > panel.abline(v=10)}
> > > )
> > >
> > > # plot with vertical but without fitted lines
> > > plot(augPred(fm1, level = 0:1, length.out=2),
> > > panel=function(x,y,...) {
> > > panel.xyplot(x,y,...)
> > > panel.abline(v=10)}
> > > )
> > >
> > > # plot with vertical and with all points (fitted lines are drawn
> > > # as
> > > points)
> > > plot(augPred(fm1, level = 0:1),
> > > panel=function(x,y,...) {
> > > panel.xyplot(x,y,...)
> > > panel.abline(v=10)}
> > > )
> >
> > One option is to take a sneak a peek at nlme:::plot.augPred() to see
> > what the default panel function is doing.  Here I have replicated
> > the default panel function and added a call to panel.abline().
> >
> > plot(augPred(fm1, level = 0:1, length.out = 2),
> >  panel=function(x, y, subscripts, groups, ...) {
> >                orig <- groups[subscripts] == "original"
> >                panel.xyplot(x[orig], y[orig], ...)
> >                panel.superpose(x[!orig], y[!orig],
> >                subscripts[!orig],
> >                                groups, ..., type = "l")
> >                panel.abline(v=10)
> >  })
> >
> > The problem with this approach is that you need to crawl around in
> > the code of nlme:::plot.augPred().  An alternative approach is to
> > annotate the plot after-the-fact.  This is shown below.
> >
> > plot(augPred(fm1, level = 0:1, length.out = 2))
> > for (i in 1:5) {
> >  for (j in 1:6) {
> >    if (i < 5 || j < 4) {
> >      trellis.focus("panel", j, i, highlight=FALSE)
> >      panel.abline(v=10)
> >    }
> >  }
> > }
> >
> > This avoids crawling around in code, but the problem with this is
> > knowing how many rows and columns of panels there are.  If you
> > explicitly controlled the 'layout' of the original plot, you could
> > guarantee that your annotation works properly.
> >
> 
> You can find that out with trellis.currentLayout:
> 
> tcL <- trellis.currentLayout()
> for(i in 1:nrow(tcL))
>   for(j in 1:ncol(tcL))
>     if (tcL[i,j] > 0) {
>         trellis.focus("panel", j, i, highlight = FALSE)
>         panel.abline(v = 10)
>         trellis.unfocus()
>     }
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html and provide commented,
> minimal, self-contained, reproducible code.

Petr Pikal
petr.pikal at precheza.cz



More information about the R-help mailing list