[R] Error in plot.window(...) : need finite 'ylim' values

William Dunlap wdunlap at tibco.com
Thu Jan 5 18:39:36 CET 2012


Most plot methods die with an error like this
if all the values in y (or x) are NA.  E.g.,
you get the same problem with
   plot(rep(NA, 10))
Remove the columns that are all NA's before trying to
plot them.

It might be nice if the fancier plot methods could
trap such errors and perhaps put some text like
"(No Data)" where the plot should have been.

An ugly way of avoiding the error that stops you from
seeing the non-missing columns of the dataset is to
replace plot.window with a version that assigns an
arbitrary value to xlim or ylim if they contain illegal
values: 

  > plot.window.orig <- plot.window
  > plot.window <- function(xlim, ylim, log="", asp=NA, ...) {
  +    if (!all(is.finite(xlim))) xlim <- c(0,1)
  +    if (!all(is.finite(ylim))) ylim <- c(0,1)
  +    plot.window.orig(xlim, ylim, log="", asp=NA, ...)
  + }
  > assignInNamespace("plot.window", plot.window, "graphics")

and then you can do things like
  d <- data.frame(one=1:10, two=rep(NA,10), three=sin(1:10))
  plot(d)
  library(zoo)
  plot(zoo(d))

(You should not call plot.zoo(yourData) since
plot(yourData) invokes plot.zoo if yourData is
a zoo object and plot.someOtherClass if it has
someOtherClass.)

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Rich Shepard
> Sent: Thursday, January 05, 2012 9:08 AM
> To: r-help at r-project.org
> Subject: Re: [R] Error in plot.window(...) : need finite 'ylim' values
> 
> On Thu, 5 Jan 2012, R. Michael Weylandt wrote:
> 
> > My initial guess is because you don't have any complete cases: cf.
> > this behavior plot(c(NA, NA))
> 
> Michael,
> 
>    I may be creating the plots incorrectly: using plot() rather than
> plot.zoo(). When I compare the plot of the zoo object to a lattice xyplot()
> of points there are major discrepancies.
> 
>    Let me read more on plot.zoo() and try that before taking your time with
> this.
> 
> Regards,
> 
> Rich
> 
> ______________________________________________
> 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.



More information about the R-help mailing list