Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R plot with an x time axis: how to force the ticks labels to be the days?

Tags:

plot

r

I have this file in csv format:

timestamp,pages
2011-12-09T11:20:50.33,4
2012-01-23T17:44:02.71,132
2012-01-28T15:07:59.34,168

The first column is a timestamp, the second one is a page count. I need to plot the page count on the vertical axis and the timestamp on the horizontal axis.

The timestamps are not regularly spaced, I have one day in december ant two close days in january.

I tried this code

df = read.csv("my_data.csv")
df$timestamp = strptime(df$timestamp, "%Y-%m-%dT%H:%M:%S")
plot(df$timestamp,df$pages)

and I got a plot with just one tick on the middle of the x axis and with the label "Jan": it's not wrong but I would like to have three ticks with just the day number and the month.

I tried

plot(df$timestamp,df$pages,xaxt="n")
axis.Date(1,df$timestamp,"days")

but no x axis is plotted. Any idea? Thank you

like image 473
Alessandro Jacopson Avatar asked Jan 29 '12 13:01

Alessandro Jacopson


People also ask

How do you change the X axis ticks in R?

Option 1. Set xaxt = "n" and yaxt = "n" to remove the tick labels of the plot and add the new labels with the axis function. Note that the at argument sets where to show the tick marks.

How do I add a tick to a label in R?

The following steps can be used : Hide x and y axis. Add tick marks using the axis() R function. Add tick mark labels using the text() function.


2 Answers

I hope this can help. I made this function that allows adding a fixed number of equidistant time ticks. By setting first.day=T, the function puts the tick to the 1st of each month. Of course, the plot must be created with xaxt="n" argument. By default, it adds 10 ticks (ticks.n=10) with a dd/mm format (format.x="%d/%m"), no first day of the month, and horizontal orientation of the labels (las=1).

axis.time=function(time.x=Sys.time(),ticks.n=10,format.x="%d/%m",first.day=F,las=1){
  if (first.day){
    time.x=seq(time.x[1],time.x[length(time.x)],60*60*24/2)
    time.x=time.x[which(diff(as.numeric(format(time.x,"%d")))<0)+1]
  } else {
    time.x=seq(time.x[1],time.x[length(time.x)],length.out=ticks.n)  
  }
  axis.POSIXct(side = 1,x = time.x,at = time.x,format = format.x,las=las)
}

Suppose you have a data frame:

df1=data.frame(time=seq(Sys.time()-1e8,Sys.time(),length.out = 100),Y=runif(100))

a plot with plot(df1) will put the X-axis ticks only at the beginning of each year. If you plot as plot(df1,xaxt="n") you can use the axis.time function:

axis.time(time.x = df1$time,first.day = T,las=2,format.x = "%m-%y")

to get a tick on the first day of each month and with a different format and alignment.

enter image description here

like image 101
Giancarlo Tamburello Avatar answered Sep 18 '22 03:09

Giancarlo Tamburello


I would as.Date() your timestamp like this:

df$timestamp = as.Date(strptime(df$timestamp, "%Y-%m-%dT%H:%M:%S"))

This works then:

plot(df$timestamp,df$pages,xaxt="n")
axis.Date(1,at=df$timestamp,labels=format(df$timestamp,"%b-%d"),las=2)

enter image description here

like image 25
Pierre Lapointe Avatar answered Sep 22 '22 03:09

Pierre Lapointe