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]]
I do not think any one has replied to this. Having 2 keys I think comes under the bit in the help under legend. I have not got time at the moment but see ?xyplot and have a look at https://stat.ethz.ch/pipermail/r-help/2005-February/066264.html and the following Regards Duncan Duncan Mackay Department of Agronomy and Soil Science University of New England Armidale NSW 2351 Email: home: mackay at northnet.com.au -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Naresh Gurbuxani Sent: Saturday, 17 January 2015 23:08 To: r-help at r-project.org Subject: [R] Evaluated expression in lattice key 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.
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. >
Chee Hee, This is indeed what I was looking for. Thank you for introducing me to panel.text, Naresh> Date: Mon, 19 Jan 2015 21:17:18 -0600 > From: chl948 at mail.usask.ca > Subject: Re: [R] Evaluated expression in lattice key > To: naresh_gurbuxani at hotmail.com; r-help at r-project.org > > 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. > >[[alternative HTML version deleted]]