Paul Lasky's nice function needs a comma after "col" and after "qL3))"
to prevent an error message
plotobj <- xyplot( y ~ x, data=den, xlab = "Points", type = "l", main =
main2, col = col,
panel.info = list( quant = quantile ( obj $ score, Q ), qlabel = c(
qL1,"Median",qL3 ) ),
best wishes, Ronald
Paul Lasky wrote:
> 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 = den, (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 = den,
> (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=den, 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
>
>
Ronald W.H. Verwer
Netherlands Institute for Neurosciences
Meibergdreef 33, 1105 AZ Amsterdam
The Netherlands
tel: +31 (20) 5665518
fax: +31 (20) 6961006
e-mail: r.verwer@nin.knaw.nl
web-page: http://www.nin.knaw.nl/~verwer
|