s-news
[Top] [All Lists]

FW: Calling xyplot from inside a function.

To: Thom Burnett <Thom.Burnett@cognigencorp.com>, Paul Lasky <phlasky@earthlink.net>, S-News <s-news@lists.biostat.wustl.edu>
Subject: FW: Calling xyplot from inside a function.
From: "Austin, Matt" <maustin@amgen.com>
Date: Wed, 5 Jul 2006 11:26:00 -0700
 



You can use the regular text command inside a function, but you have to make
the data you want to use available inside the panel. 

Here is an example:

x <- list()
x$score <- rnorm(1000)

myFunction = function( obj ) {
     
     den = density( obj$score )
     main2 = "My Stuff"
      
     t1 <- xyplot( y ~ x, 
                   data=den,
                   xlab="Points", 
                   type = "l",
                   main = main2, 
                   panelInfo = list(quant=quantile( obj$score, c( 0.25, 0.5,
0.75 ) )),
                   col  = 1,
                   panel = function(x, y,  ..., panelInfo){
                           panel.xyplot(x,y,...)
                      panel.abline( v  = panelInfo$quant, lty = 2 )
                      text(x=panelInfo$quant,
y=par('usr')[3]+.5*par('cxy')[2], c("Q1", "Q2","Q3"))
                   })  
     print(t1)
   }

myFunction(x)

Why not just use densityplot() instead of using xyplot()?

--Matt

-----Original Message-----
From: s-news-owner@lists.biostat.wustl.edu
[mailto:s-news-owner@lists.biostat.wustl.edu] On Behalf Of Thom Burnett
Sent: Tuesday, July 04, 2006 10:00 AM
To: Paul Lasky; S-News
Subject: Re: [S] Calling xyplot from inside a function.

I'm pretty sure that you need to wrap the xyplot() in print()
     print(xyplot( den$y ~ den$x, xlab="Points", type = "l",main = main2,
col  = 1, ... )  )

I don't know the labeling you want off the top of my head. You might be able
to do it with the axes() function or you may need to use text() but I think
you know as much about them and using them in a panel function as I do.



Paul Lasky wrote:

>  I know this is another tiresome "scoping" question, but I can't seem 
> to get a Trellis plot with panel functions to work when called from a
> function:
>  
>   What I want to do is graph prob density, put some quantile vertical 
> lines on the graph, and place some text on the graph labeling the 
> vertical lines; the following code (without attempting the labeling) 
> doesn't work:
>  
>   myFunction = function( obj ) {
>      #
>      den = density( obj$score, n=100, width = 29 )
>      quant = quantile( obj$score, c( 0.1, 0.5, 0.9 ) )
>      main2 = "My Stuff"
>      #
>       xyplot( den$y ~ den$x, xlab="Points", type = "l",main = main2, 
> col  = 1,
>         panel = function(x, y) {
>            panel.abline( v  = quant, lty = 2 )
>         }
>      )   
>   }
>    Also what is the code for Labeling the vertical  quantile lines 
> with text at the bottom such as " 10%", "Median", "90%"?
>   A panel.text function isn't available.
>  
> Producing this plot within a function is easy to do with the 
> old-fashioned plot function. But Trellis plots seem to be so difficult 
> to work with that, frankly, most of the time they aren't worth the 
> effort.  In this instance, however it would be desirable from a 
> publication standpoint to produce a Trellis plot.
>  
> Paul Lasky
> P & B Consultants.
>  
>  

--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with the
BODY of the message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>