Adam Zeilinger
2011-Aug-24 19:30 UTC
[R] Suppressing error messages printed in xyplot() with panel function
Hello, I am using the xyplot() function to create a series of scatterplot panels with lines of best fit. To draw the lines of best fit for each panel, I am using a panel function. Here's an example: > species <- as.character(c(rep(list("A", "B", "A"), 10), "B")) > year <- as.character(c(rep(list("2009", "2009", "2010"), 10), "2010")) > x <- rnorm(31, mean = 50, sd = 1) > y <- 3*x + 20 > ex.data <- data.frame(cbind(species, year, x, y)) > ex.data$x <- as.numeric(ex.data$x) > ex.data$y <- as.numeric(ex.data$y) > xyplot(y ~ x|species*year, data = ex.data, + panel = function(x, y) { + panel.xyplot(x, y, pch=16, col="black") + panel.abline(lm(y ~ x))}) With my data set, there are some panels with less than 2 data points. In these panels, an error message is printed in the panel, something like: "Error using packet 4 missing value where TRUE/FALSE needed." In the panels with error messages, I want to keep the panels but suppress the error message, such that the panel is blank or has only one datum. How do I do suppress the printing of the error message? Thanks in advance for your help. Adam -- Adam Zeilinger Conservation Biology Program University of Minnesota
Dennis Murphy
2011-Aug-25 02:50 UTC
[R] Suppressing error messages printed in xyplot() with panel function
Hi: Here's one way out: xyplot(y ~ x|species*year, data = ex.data, type = c('p', 'r')) type = is a very useful argument to know in xyplot(). See p.75 of the Lattice book. HTH, Dennis On Wed, Aug 24, 2011 at 12:30 PM, Adam Zeilinger <zeil0006 at umn.edu> wrote:> Hello, > > I am using the xyplot() function to create a series of scatterplot panels > with lines of best fit. ?To draw the lines of best fit for each panel, I am > using a panel function. ?Here's an example: > >> species <- as.character(c(rep(list("A", "B", "A"), 10), "B")) >> year <- as.character(c(rep(list("2009", "2009", "2010"), 10), "2010")) >> x <- rnorm(31, mean = 50, sd = 1) >> y <- 3*x + 20 >> ex.data <- data.frame(cbind(species, year, x, y)) >> ex.data$x <- as.numeric(ex.data$x) >> ex.data$y <- as.numeric(ex.data$y) >> xyplot(y ~ x|species*year, data = ex.data, > + ? panel = function(x, y) { > + ? panel.xyplot(x, y, pch=16, col="black") > + ? panel.abline(lm(y ~ x))}) > > With my data set, there are some panels with less than 2 data points. ?In > these panels, an error message is printed in the panel, something like: > "Error using packet 4 missing value where TRUE/FALSE needed." > > In the panels with error messages, I want to keep the panels but suppress > the error message, such that the panel is blank or has only one datum. ?How > do I do suppress the printing of the error message? > > Thanks in advance for your help. > Adam > > -- > > Adam Zeilinger > Conservation Biology Program > University of Minnesota > > ______________________________________________ > R-help at r-project.org 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. >
Deepayan Sarkar
2011-Aug-25 04:47 UTC
[R] Suppressing error messages printed in xyplot() with panel function
On Thu, Aug 25, 2011 at 1:00 AM, Adam Zeilinger <zeil0006 at umn.edu> wrote:> Hello, > > I am using the xyplot() function to create a series of scatterplot panels > with lines of best fit. ?To draw the lines of best fit for each panel, I am > using a panel function. ?Here's an example: > >> species <- as.character(c(rep(list("A", "B", "A"), 10), "B")) >> year <- as.character(c(rep(list("2009", "2009", "2010"), 10), "2010")) >> x <- rnorm(31, mean = 50, sd = 1) >> y <- 3*x + 20 >> ex.data <- data.frame(cbind(species, year, x, y)) >> ex.data$x <- as.numeric(ex.data$x) >> ex.data$y <- as.numeric(ex.data$y) >> xyplot(y ~ x|species*year, data = ex.data, > + ? panel = function(x, y) { > + ? panel.xyplot(x, y, pch=16, col="black") > + ? panel.abline(lm(y ~ x))}) > > With my data set, there are some panels with less than 2 data points. ?In > these panels, an error message is printed in the panel, something like: > "Error using packet 4 missing value where TRUE/FALSE needed." > > In the panels with error messages, I want to keep the panels but suppress > the error message, such that the panel is blank or has only one datum. ?How > do I do suppress the printing of the error message?Generally speaking, suppressing error messages should not be your first instinct. If you know why the error is happening, it's better to make sure it never happens, e.g., with something like if (length(x) >= 2) panel.abline(lm(y ~ x)) Of course, as Dennis says, in this case you can take advantage of the 'type' argument, which already implements what you want. -Deepayan
Apparently Analagous Threads
- simultaneously maximizing two independent log likelihood functions using mle2
- lattice, xyplot, using "panel.segments" by just addressing one panel
- Spacing between lattice panels
- panel.abline and xyplot
- distinguish regression lines in grouped, black and white lattice xyplot