[R] time series line plot: Error in plot.window(...) : invalid 'xlim' value

R. Michael Weylandt michael.weylandt at gmail.com
Tue Jul 31 19:29:12 CEST 2012


On Tue, Jul 31, 2012 at 12:02 PM, Yolande Tra <yolande.tra at gmail.com> wrote:
> Hello,
>
> This should be pretty simple but I cannot get it right. Please point to the
> right code. Thanks.
>
>> last <- read.csv(file.path(dataDir,"plot1.csv"), as.is=T,stringsAsFactors
> = FALSE)
>> last
>          date    r_wvht
> 1    8/6/2008 0.9766667
> 2    8/8/2008 0.7733333
> 3   8/11/2008 1.4833333
> 4   8/13/2008 1.5766667
> 5   8/14/2008 1.3900000
> 6   8/18/2008 0.7800000
> 7   8/20/2008 0.8383333
> 8   8/27/2008 1.7700000
> 9   8/28/2008 1.2950000
> 10  8/31/2008 2.4100000
> 11   9/2/2008 1.3166667
> 12   9/3/2008 1.3075000
> 13   9/4/2008 1.3900000
> 14   9/5/2008 1.6333333
> 15   9/8/2008 1.2416667
> 16  9/11/2008 1.3950000
> 17 10/12/2009 0.8633333
> 18 10/14/2009 2.7900000
> 19 10/19/2009 1.0325000
> 20 10/21/2009 1.9650000
> 21 10/26/2009 1.7800000
> 22 10/29/2009 1.5666667
> 23 10/30/2009 1.0500000
> 24  11/2/2009 1.4633333
> 25  11/3/2009 1.2400000
> 26  11/4/2009 1.0075000
> 27 11/11/2009 1.6050000
> 28 11/13/2009 1.8475000
>> x<-as.vector(last$date)

Here's your problem -- "x" is all strings so you can't make an x-axis
out of them later. I'll show you how to diagnose that below. You
probably want as.Date() here -- I rarely (never?) have occasion to use
as.vector().

>> x
>  [1] "8/6/2008"   "8/8/2008"   "8/11/2008"  "8/13/2008"  "8/14/2008"
> "8/18/2008"  "8/20/2008"  "8/27/2008"
>  [9] "8/28/2008"  "8/31/2008"  "9/2/2008"   "9/3/2008"   "9/4/2008"
> "9/5/2008"   "9/8/2008"   "9/11/2008"
> [17] "10/12/2009" "10/14/2009" "10/19/2009" "10/21/2009" "10/26/2009"
> "10/29/2009" "10/30/2009" "11/2/2009"
> [25] "11/3/2009"  "11/4/2009"  "11/11/2009" "11/13/2009"
>> y<-as.vector(last$r_wvht)
>> y
>  [1] 0.9766667 0.7733333 1.4833333 1.5766667 1.3900000 0.7800000 0.8383333
> 1.7700000 1.2950000 2.4100000
> [11] 1.3166667 1.3075000 1.3900000 1.6333333 1.2416667 1.3950000 0.8633333
> 2.7900000 1.0325000 1.9650000
> [21] 1.7800000 1.5666667 1.0500000 1.4633333 1.2400000 1.0075000 1.6050000
> 1.8475000
>> plot(x,y)
> Error in plot.window(...) : need finite 'xlim' values
> In addition: Warning messages:
> 1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
> 2: In min(x) : no non-missing arguments to min; returning Inf
> 3: In max(x) : no non-missing arguments to max; returning -Inf
>> plot(x,y,xlim=c("6/8/2008","11/13/2009"))
> Error in plot.window(...) : invalid 'xlim' value

Taking these in order --

1) we see something had to be coerced: I know it was the strings above
and all of them created NA's because they weren't numeric literals.
2) Since all our arguments were NA to min, we have no information
about the min() and return Inf.
3) Ibid, returning -Inf.

giving in turn the error:

x limits from -Inf to Inf don't make a well defined graph.

Big picture, you should probably use a zoo object here instead of a
data frame and awkward column conversion:

library(zoo)
x <- read.zoo( file_path) # Creates a "zoo" object x

plot(x) # Will use the index of x automatically as the x axis and the
values as the y because x is a zoo (read time series) object.

and it should all happen magically.

Michael

> Y
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> 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