s-news
[Top] [All Lists]

Calling xyplot from Inside a Function

To: "S-News" <s-news@lists.biostat.wustl.edu>
Subject: Calling xyplot from Inside a Function
From: "Paul Lasky" <phlasky@earthlink.net>
Date: Thu, 6 Jul 2006 10:52:08 -0700
Domainkey-signature: a=rsa-sha1; q=dns; c=nofws; s=dk20050327; d=earthlink.net; b=WKnEwjlZmnhluJODvTaQzqaiKgWme0gvOIwjbGUojfeemTTYZtCzssRznBpjqPZT; h=Received:From:To:Subject:Date:MIME-Version:Content-Type:X-Mailer:Thread-Index:X-MimeOLE:Message-ID:X-ELNK-Trace:X-Originating-IP;
Thread-index: AcahJOZ6/rkgcevNSXumM9iQXrP/cA==
   Many thanks to all that replied,  especially to Matt Austin of Amgen and Bill Dunlap of Insightful  
who pointed the way and fully explained  the cryptic vague warning offered by  Ripley and  Venables
 [refering to panel functions]: "First the computations will occur inside a deeply nested set of function calls, so care is needed to ensure that the data are visible."   (The next edition of MASS could very well spend some paragraphs to
explain this and  advise how to write and use panel functions. Also a page or three to show how to generally comply with  the arcane scoping rules of R and Splus.)
 
 Bill Dunlap pointed out that a simple change of coding avoids scoping problems and is good practice:
 
  use something like:
   
myFunction = function(score, ... )   
   . . .
   den = density(obj$score, ... )  
   . . .
   xyplot(y ~ x, data = "" (other arguments) )
 
 rather than:
 
  . . .
 xyplot (den$y ~ den$x, (other arguments) )
 
Also on some machines  Thom Burnett, David Parkhurst, Shawn Boles and  Chuck Taylor of Insightful pointed that you may need to enclose the xyplot statement in a print  statement: viz print ( xyplot( y ~ x, data = "" (other arguments) ) ), or, alternatively, use  plot.obj = xyplot( ...) followed by print( plot.obj) . However on my system ( Windows XP professional, SPlus 6.1 or SPlus 7.0 )  it is not necessary to use the print wrapper.
 
Matt Austin solved the scoping riddle with an elegant and simple script that is generally applicable to many situations other than panel functions.  You send down all the info down to the panel function in a single list.  Incorporating all the suggestions the following script works very nicely:
 
myFunction = function( obj , col = col, Q = c ( 0.25, 0.5, 0.75 ) ) {

den = density( obj$score, n = 100, window ="g", bandwith = 29, from = 0, to = 100 )

main2 = "My Stuff"

qL1 = paste( Q[1], "%")

qL3 = paste( Q[3], "%")

plotobj <- xyplot( y ~ x, data="" class=050362216-06072006> xlab = "Points", type = "l", main = main2, col = col

panel.info = list( quant = quantile ( obj $ score, Q ), qlabel = c( qL1,"Median",qL3 ) )

  # NOTE:  Splus and R REQUIRE a list for panel.info , a simple vector won't work.

panel = function( x, y, ..., panel.info )  {    # NOTE: do NOT leave out the ellipsis, "  ..."  !

panel.xyplot(x,y, ... ) # MUST have the ellipsis here too.

#  print the 3 vertical quantile lines.

panel.abline( v = panel.info $ quant, lty = 2, col = 8 )  # extract the quant from the panel.info list for use here.

text( x = panel.info $ quant,y= 0.001, labels = panel.info $ qlabel ) # extract the qlabel from panel.info for use here.

#  Yes ! you can use the old reliable version 3 text function to label the vertical quantile lines.They work just like their use # in plot   functions. As an alternative the following mtext function also works,

# mtext ( panel.info$qlabel, side=1,at=panel.info$qlabel, line=-1 )

axis( side = 1,at = seq ( from=0, to=100, by=10 ) ) #  Once again an old reliable ver 3 function works here to give me the     #  correct spacing for the x axis labels.

axis (side = 2,labels = F ) # Here I want to eliminate the meaningless y axis (prob density) labels. But although this           #  function doesn't destroy the plot, it also doesn't eliminate the labels like it does in plot. Why? Go figure.

 }  # end panel function 

)  # end xyplot

print ( plot.obj )  # May be required on some systems to cause printing from myFunction.

} # end myFunction

One last point.  Several people pointed out that it is just easier to use densityplot which codes all this stuff for you. But, densityplot produces an ugly plot with some awful dot stuff over the x-axis.  Editing the function to eliminate this is not easy.  I STRONGLY recommend that you use xyplot as above to get the kind of plots that you want, especially when you publish. Either that or use the old, easy to use,  ver 3 plot function which  I have found many editors will accept, but which is generally not as pretty. Paul Lasky P & B Consultants 
<Prev in Thread] Current Thread [Next in Thread>