Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting time-series with Date labels on x-axis

Tags:

plot

r

I know that this question might be a cliche, but I'm having hard time doing it.

I've data set in the following format:

     Date            Visits      11/1/2010       696537     11/2/2010       718748     11/3/2010       799355     11/4/2010       805800     11/5/2010       701262     11/6/2010       531579     11/7/2010       690068     11/8/2010       756947     11/9/2010       718757     11/10/2010      701768     11/11/2010      820113     11/12/2010      645259 

I want to create a time-series plot, with x-axis representing time & y-axis vists. Also, I want to mark the x-axis with date. The code I was using is the following:

dm$newday = as.POSIXct(strptime(dm$Day, format="%Y-%m-%d")) plot(as.Date(dm$day),dm$visits) axis.Date(1,Day,at=seq(as.Date("2010/10/30"), as.Date("2011/01/29"),by="days")) 
like image 406
user538233 Avatar asked Jan 30 '11 16:01

user538233


People also ask

What is taken on x-axis while using time series?

A time series plot is a graph where some measure of time is the unit on the x-axis. In fact, we label the x-axis the time-axis. The y-axis is for the variable that is being measured. Data points are plotted and generally connected with straight lines, which allow for the analysis of the graph generated.


2 Answers

1) Since the times are dates be sure to use "Date" class, not "POSIXct" or "POSIXlt". See R News 4/1 for advice and try this where Lines is defined in the Note at the end. No packages are used here.

dm <- read.table(text = Lines, header = TRUE) dm$Date <- as.Date(dm$Date, "%m/%d/%Y") plot(Visits ~ Date, dm, xaxt = "n", type = "l") axis(1, dm$Date, format(dm$Date, "%b %d"), cex.axis = .7) 

The use of text = Lines is just to keep the example self-contained and in reality it would be replaced with something like "myfile.dat" . (continued after image)

screenshot

2) Since this is a time series you may wish to use a time series representation giving slightly simpler code:

library(zoo)  z <- read.zoo(text = Lines, header = TRUE, format = "%m/%d/%Y") plot(z, xaxt = "n") axis(1, dm$Date, format(dm$Date, "%b %d"), cex.axis = .7) 

Depending on what you want the plot to look like it may be sufficient just to use plot(Visits ~ Date, dm) in the first case or plot(z) in the second case suppressing the axis command entirely. It could also be done using xyplot.zoo

library(lattice) xyplot(z) 

or autoplot.zoo:

library(ggplot2) autoplot(z) 

Note:

Lines <- "Date            Visits 11/1/2010   696537 11/2/2010   718748 11/3/2010   799355 11/4/2010   805800 11/5/2010   701262 11/6/2010   531579 11/7/2010   690068 11/8/2010   756947 11/9/2010   718757 11/10/2010  701768 11/11/2010  820113 11/12/2010  645259" 
like image 67
G. Grothendieck Avatar answered Oct 12 '22 02:10

G. Grothendieck


I like using the ggplot2 for this sort of thing:

df$Date <- as.Date( df$Date, '%m/%d/%Y') require(ggplot2) ggplot( data = df, aes( Date, Visits )) + geom_line()  

enter image description here

like image 41
Prasad Chalasani Avatar answered Oct 12 '22 02:10

Prasad Chalasani