Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to create an irregular time series graph (R? GGPLOT? ITS?)

I'm a graphic designer who is trying to use R to create graphs that are too complicated for Excel. I'm specifically trying to create an irregular time series step chart. I've had no problems creating a regular time series chart, but for some reason, the irregular dates are throwing everything off.

I'm starting with a basic text file with two columns of data:

01-04-1940    4
05-29-1963    35
12-02-2002    24

I've loaded the data using

d <- read.delim("file.txt", header = TRUE)

and I've converted the first column in Unix time using

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

But at this point, I can't find any more information anywhere on how to proceed. I've seen the R package "ITS," But I cannot find any documentation on it beyond technical descriptions of the classes involved.

I'd much appreciate it if someone with some experience in R could point out the few lines of code I need to create this graph. Thanks!

like image 502
Alex Avatar asked Jun 30 '11 19:06

Alex


People also ask

How do you visualize time series data in R?

We can use the geom_line() function to visualize the time-series data using a line plot. Parameter: dataframe: determines the dataframe variable for plotting chart. x: determines the time variable vector.

How do you construct a time series plot?

To create the time series plot, start off by labeling the time-axis in chronological order. Then, label the vertical axis. Once the time and vertical axes are labeled, plot the points given in the data set.

How do you plot a variable over time in R?

Activate a time series plot by setting the x-variable to a variable of R type Date , which is true of the variable date in this data set. Can also plot a time series by passing a time series object, created with the base R function ts() as the variable to plot.


2 Answers

I would use xts/zoo. They both handle irregular time series easily.

z <- zoo(d[,2], d[,1])
plot(z)
plot(z, type="s")
like image 41
Joshua Ulrich Avatar answered Sep 21 '22 08:09

Joshua Ulrich


ggplot deals quite nicely with data in date format. Here are some suggestions:

d <- data.frame(
    date = c("01-04-1940", "05-29-1963", "12-02-2002"),
    value = c(4, 35, 24)
)

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

ggplot(d, aes(x=date, y=value)) + geom_step(colour="blue")

enter image description here

ggplot(d, aes(x=date, y=value)) + geom_line(colour="red")

enter image description here

like image 65
Andrie Avatar answered Sep 25 '22 08:09

Andrie