Skip to content

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

9 messages · Yolande Tra, R. Michael Weylandt, Rui Barradas

#
Hello,

You could use dput(), it's not your first post...


last <- structure(list(date = c("8/6/2008", "8/8/2008", "8/11/2008",
"8/13/2008", "8/14/2008", "8/18/2008", "8/20/2008", "8/27/2008",
"8/28/2008", "8/31/2008", "9/2/2008", "9/3/2008", "9/4/2008",
"9/5/2008", "9/8/2008", "9/11/2008", "10/12/2009", "10/14/2009",
"10/19/2009", "10/21/2009", "10/26/2009", "10/29/2009", "10/30/2009",
"11/2/2009", "11/3/2009", "11/4/2009", "11/11/2009", "11/13/2009"
), r_wvht = c(0.9766667, 0.7733333, 1.4833333, 1.5766667, 1.39,
0.78, 0.8383333, 1.77, 1.295, 2.41, 1.3166667, 1.3075, 1.39,
1.6333333, 1.2416667, 1.395, 0.8633333, 2.79, 1.0325, 1.965,
1.78, 1.5666667, 1.05, 1.4633333, 1.24, 1.0075, 1.605, 1.8475
)), .Names = c("date", "r_wvht"), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24",
"25", "26", "27", "28"))

last$date <- as.Date(last$date, format="%m/%d/%Y")

plot(r_wvht ~ date, data = last)

(You were forgetting that last$date was a character vector, not class Date.)

Hope this helps,

Rui Barradas

Em 31-07-2012 18:02, Yolande Tra escreveu:
#
On Tue, Jul 31, 2012 at 12:02 PM, Yolande Tra <yolande.tra at gmail.com> wrote:
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().
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
#
Hello,

Sorry, I forgot the "time series" part of your question. You could use 
instead one of

# 1.  type = "l" gives a line plot
plot(r_wvht ~ date, data = last, type="l")

# 2. use time series object plot
library(zoo)
z <- zoo(last$r_wvht, order.by=last$date)
plot(z)

Rui Barradas

Em 31-07-2012 18:27, Rui Barradas escreveu:
#
Hello,

It only gives that error if you don't

last$date <- as.Date(last$date, format="%m/%d/%Y")

You must have dates, not character values.
Try it, then make a zoo object, then plot it.

Rui Barradas

Em 31-07-2012 18:54, Yolande Tra escreveu:
#
Hello,

Adapted from the help page for plot.zoo

#library(zoo)
#z <- zoo(last$r_wvht, order.by=last$date)
plot(z, xaxt = "n")
tt <- time(z)
ix <- seq(1, length(tt), length.out=8)
axis(side = 1, at = tt[ix], labels = FALSE)
labs <- format(tt, "%Y-%b-%d")
axis(side = 1, at = tt[ix], labels = labs[ix], tcl = -0.7, cex.axis = 
0.7, las=2)


It looks better, but I wouldn't risk "good".

Rui Barradas

Em 31-07-2012 19:10, Yolande Tra escreveu: