[R] Evaluated expression in lattice key

Chel Hee Lee chl948 at mail.usask.ca
Tue Jan 20 04:17:18 CET 2015


I guess that you may need 'panel.text()' to customize panel function. 
Please run the following code and see the output.  You will see 
different value of R-squared in each panel.

xyplot(
   y~x|name, data=df,
   panel = function(x, y, ...) {
     panel.xyplot(x, y, ...)
     panel.abline(lm(y~x), col="red")
     panel.text(1, -1, bquote(R^2 == .(summary(lm(y~x))$r.squared)))
   },
   grid = TRUE
)

Is this what you are looking for?  I hope this helps.

Chel Hee Lee


On 01/17/2015 07:08 AM, Naresh Gurbuxani wrote:
> In a conditional xyplot, I would like to add some numerical results in every panel.  Consider below example.
> df <- data.frame(x = rnorm(100), name = "A")
> df <- within(df, {y <- x + 0.2 * rnorm(100)})
> df2 <- data.frame(x = rnorm(100), y = rnorm(100), name = "B")
> df <- rbind(df, df2)
> rm(df2)
>
>
> with(df, xyplot(y ~ x | name, data = df,
>    panel = function(x, y, ...){
>    	panel.xyplot(x, y)
>    	panel.lmline(x, y, col = "grey")
>    },
>    auto.key = list(corner = c(1,0), x = 0.9, y = 0.1,
>    text = substitute(expression(R^{2} == myval),
>    list(myval = summary(lm(y ~ x))$r.squared)))))
> There are two problems with this plot.  First, there is only one key.  I want two different keys for the two panels.
> Second, R^2 is for all data.  For each panel, I want R^2 calculated using only the data plotted in that panel.
> Please help.
> Thanks,Naresh 		 	   		
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>



More information about the R-help mailing list