[R] xyplot key using grid.text

From: Nitin Jain <nj7w_at_yahoo.com>
Date: Tue, 15 May 2007 08:36:23 -0700 (PDT)


Hello,

I am trying to print the lm
coefficients using grid graphs (code below), and am getting all the coefficients on top of each other.  

My aim is to put them as legend,
showing both coefficients and mean values separated by colors as shown in xyplot.  

testData <- data.frame(f1 =
factor(rep(LETTERS[1:5], each = 20)),

                       f2 = 

factor(rep(letters[6:7], 50)),
                       x1 = 

rnorm(100, mean = 2, sd = 1),
                       x2 = 

rnorm(100, mean = 10, sd = 4),
                       x3 = 

rnorm(100, mean = 4, sd = 3),
                       y = (1:50) + 

rnorm(50, sd = 3) + rep(c(-2, 2), 50))    

grid.newpage()

grps <- factor(testData[,
"f2"])

xyplot( y ~
x1|f1,        

groups=grps,

       panel=function(x, y, ...)
{

         panel.superpose(x, y,
...)        

},

       panel.groups = function(x, y,
...) {

         fit.lm <-
lm(y~x)

         coef.fit <-
round(coef(fit.lm)[-1], 2)

         avg.y <- round(mean(y),
2)          

panel.abline(fit.lm)

         panel.xyplot(x, y,
...)

         ## Add lm
coefficients:

         grid.text(x= unit(0.1,
"npc"),

                   y= unit(seq(0.1, 

length= length(levels(grps)), by =0.1),                      

"npc"),

                   label = 

paste(expression(beta), "=", coef.fit)                 

   ),
## Add mean values: (note that "mu" is not shown as symbol)

grid.text(x= unit(0.1,
"npc"),

                   y= unit(seq(0.9, 

length= length(levels(grps)), by =-0.1),                      

"npc"),

                   label = 

paste(expression(mu), "=", avg.y)                 

   ),        

},

       ## Key

       key =
simpleKey(levels(grps),

         columns =
2,

         space =
"bottom",

         points =
TRUE,          lines =
TRUE           ),

       ##
grid.legend()

       data=
testData

     )  

Thanks.

-NJ



Park yourself in front of a world of choices in alternative vehicles. Visit the Yahoo! Auto Green Center.

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. Received on Tue 15 May 2007 - 15:42:36 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 15 May 2007 - 16:31:33 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.