[R] Evaluated expression in lattice key

Naresh Gurbuxani naresh_gurbuxani at hotmail.com
Sat Jan 17 14:08:02 CET 2015


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]]



More information about the R-help mailing list