简体   繁体   中英

Plotting Time-series with POSIXct Date and Time Labels on x-axis

I have data in the following format:

my.XYZ$datetime<- as.POSIXct(myXYZ$datetime, format = "%Y-%m-%d %H:%M:%S")

head(my.XYZ)

units            datetime
1     1 0012-08-03 07:49:02
2     1 0012-08-05 12:27:21
3     1 0012-08-17 06:55:07
4     1 0012-08-20 10:56:49
5     1 0012-08-08 10:14:36
6     1 0012-08-20 16:12:26

I want to create a time-series plot, with x-axis representing time&date and y-axis units. This is what I did so far:

> # make it a zoo object
> zoo.XYZ<- zoo(my.XYZ)

> # make it a ts
> ts.XYZ<- ts(zoo.XYZ)

> #plotting the ts object
> plot(ts.XYZ, plot.type="single")

>Warning messages:
> 1: In xy.coords(x = matrix(rep.int(tx, k), ncol = k), y = x, log = log) :
> NAs introduced by coercion
> 2: In xy.coords(x, y) : NAs introduced by coercion

Here I do get the right graph but my x-axis is not given by date but by variable's number (variable 1, 2, 3... 430)

I also tried the following:

># plot as plot.ts
> plot.ts(ts.XYZ)

>Error in plot.window(...) : need finite 'ylim' 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.ts(ts.XYZ, xaxt = "n")

>Error in plot.window(...) : need finite 'ylim' 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

> axis(1, my.XYZ$datetime, format(my.XYZ$datetime, "%Y-%m-%d %H:%M:%S"), cex.axis = .7)

Again, the graph is correct but the x-axis is not.

Would somebody know how to handle this?

Thank you.

Giulia

When you create your zoo() object, you need to specify which column has the date/time value with the order.by= parameter. Try

zoo.XYZ <- zoo(my.XYZ[,-2], order.by=my.XYZ[,2])

Also it looks like your format string for the days may not be right. I doubt your observations occurred in 12 AD. Change your string to %y-%m-%d %H:%M:%S" if you only have two digit years to interpret those values as 2012.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM