Skip to content

xyplot: discrete points + continuous curve per panel

5 messages · RMan54, PIKAL Petr, Rene Braeckman +1 more

#
I have a number of x, y observations (Time, Conc) for a number of Subjects
(with subject number Subj) and Doses. I can plot the individual points with
xyplot fine:

xyplot(Conc ~ Time | Subj,
         Groups=Dose,
         data=myData,
         panel =  function(x,y) { 
              panel.xyplot(x, y) 
              panel.superpose(???) # Needs more here 
         } 
) 

I also like to plot on each panel (there is one Subj per panel) a continuous
curve with predictions that I can calculate from a rather complicated
function:

myPred <- (time, subj, dose) {
       returns predicted value of Conc for a given time, subj and dose
}

The predicted curves are different for each panel.

How do I plot the predictions? I have tried to add panel.superinpose in the
xyplot portion but can't link to the myPred function. I also know about
panel.curve but couldn't make it work.

My attempt is to calculate the predictions on the fly. Is this possible? Or
do I need to calculate all predictions first and put the results in a data
frame.
Thanks for any help,
Rene
#
Hi

there is probably better solution but you can try to fiidle with this 
idea, which adds stight lines to each panel one after another. 

# based on Gabor Grothendieck's code suggestion
# adds straight lines to panels in lattice plots

addLine<- function(...) {
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(...)
        trellis.unfocus()
        }
}

You need to change panel.abline(...) part maybe to panel.curve or 
panel.segments or?

HTH
Petr
On 13 Dec 2006 at 23:22, RMan54 wrote:
Date sent:      	Wed, 13 Dec 2006 23:22:41 -0800 (PST)
From:           	RMan54 <RMan54 at cox.net>
To:             	r-help at stat.math.ethz.ch
Subject:        	[R] xyplot: discrete points + continuous curve per panel
Petr Pikal
petr.pikal at precheza.cz
#
I will give this a try. However, this is based on row and columns of the
panels and not on the SUBJ and DOSE information that I need to calculate the
continuous curve.
Rene 

-----Original Message-----
From: Petr Pikal [mailto:petr.pikal at precheza.cz] 
Sent: Thursday, December 14, 2006 1:18 AM
To: RMan54; r-help at stat.math.ethz.ch
Subject: Re: [R] xyplot: discrete points + continuous curve per panel

Hi

there is probably better solution but you can try to fiidle with this idea,
which adds stight lines to each panel one after another. 

# based on Gabor Grothendieck's code suggestion # adds straight lines to
panels in lattice plots

addLine<- function(...) {
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(...)
        trellis.unfocus()
        }
}

You need to change panel.abline(...) part maybe to panel.curve or
panel.segments or?

HTH
Petr
On 13 Dec 2006 at 23:22, RMan54 wrote:
Date sent:      	Wed, 13 Dec 2006 23:22:41 -0800 (PST)
From:           	RMan54 <RMan54 at cox.net>
To:             	r-help at stat.math.ethz.ch
Subject:        	[R] xyplot: discrete points + continuous curve per
panel
Petr Pikal
petr.pikal at precheza.cz
#
On 12/13/06, RMan54 <RMan54 at cox.net> wrote:
Depends on how much work you are willing to do. There is no reason for
panel.curve to not work, provided you give it a "curve" to plot. This
is normally done in the form of a vectorized function of one variable,
which will be called with a vector of values spanning the x-axis of
your plot. It is your responsibility to construct such a function
inside each panel (presumably it would involve your myPred function).

The easy way, that generally works well for longitudinal data (with
increasing x values within a panel), is to add a column of predicted
values to your data frame. For most model fitting routines in R, the
paradigm is:

fm <- some.model(y ~ whatever, data = mydata, ...)
mydata$fit <- fitted(fm)

xyplot(y + fit ~ whatever,
       type = list("p", "l"), distribute.type = TRUE)

A real example being:

library(lme4)
data(Oxboys, package = "nlme")
Oxboys$fit <- fitted(lmer(height ~ age + (1|Subject), data = Oxboys))
xyplot(height + fit ~ age | Subject, Oxboys,
       type = c("p", "l"), distribute.type = TRUE,
       aspect = "xy")


Things will be more complicated if you already have a grouping
variable (the solution is to pass down the vector of fitted values to
the panel function and use 'subscripts' to retrieve the ones that
belong in the panel).

-Deepayan
#
Great. I will be trying to use panel.curve and pass a custom curve function
as first argument (called test() below). I can use which. packet to get
access to the panel number to produce the correct curve for each panel but
what I really need is the active Subj (actSubj) for each panel. Not sure but
it seems that Subj is passed on to the functions but in replicates. Here is
what I came up with to eliminate the replication and to calculate activeSubj
for each panel in test(). Is this the correct way? How can  I pass on Subj
and Dose directly to test()? Thanks, Rene

test <-function(x) {
    activeSubj <- unique(Subj)[which.packet()]
    x          # returns y=x for testing only
}
      
xyplot(Conc ~ Time | Subj,
       groups=Dose,
       data = mydata,
       as.table=T,
       panel = function(x,y) {
           panel.xyplot(x,y)
           panel.curve(test, n=2)
           }
       )
Deepayan Sarkar wrote: