[R] Changing x-axis when dealing with time

Ken katakagi at bu.edu
Mon May 16 17:33:31 CEST 2011


Pablo Rosado <pablojrosado <at> lbl.gov> writes:

> 
> Hi,
> 
> I am plotting data in which the x values are a timestamp.  I am trying to
> change the x-ticks so they will be in specified hours of the day, but it
> always start from hour 4 of the day.  And I need it to start from the
> beginning of the axis (at x=0) and then each tick be on the interval I
> specify.
> 
> Here is my plotting script:
> 
> *##-- ROOF COMPARISON - SOUTH--
> win.graph()
> plot(x=tiempo,y=timeframe[,8],xlab="",ylab="",**xaxt='n',**
> yaxt='n',type="l",col="blue",lwd=3,ylim=c(0,80),font=2,las=1)
> axis(side=1,at=seq(0,24,3))
> axis(side=2,at=seq(0,80,10),line=NA, tcl = -0.5,font=2,las=1)
> lines(x=tiempo,y=timeframe[,47],col="red",lwd=3)
> title(xlab=paste("Local Standard Time (",date), cex.lab=1,font.lab=2)
> title(ylab="Temperature (°C)", cex.lab=1,font.lab=2)
> legend("topleft",legend=c("cool", "standard"),col=c("blue",
> "red"),lwd=c(4,4),bty="n",cex=1.25)
> grid(nx=NULL,ny=NULL,col = "gray", lty = "dotted",lwd = 1)*
> 
> But with the axis(side=1...) nothing appears.
> 
> I have been practicing with the following code to change the xaxis but it
> says that x and y lengths differ:
> 
> *## TIMESTAMP IS A ONE COLUMN VECTOR IN WHICH I SAVED THE TIMESTAMP FROM MY
> FILE
> string.to.time <-
>     function(timestamp)
>         strptime(x=timestamp, format="%Y-%m-%d %H:%M:%S")
> 
> decimal.day <-
>     function(timestamp) {
>         y <- as.POSIXlt(**timestamp**)
>         y$day + y$hour/24 + y$min/(24*60) + y$sec/(24*60*60)
>     }
> 
> decimal.hour <-
>     function(**timestamp**) {
>         dd <- decimal.day(**timestamp**)
>         24 * (dd - floor(dd))
>     }
> 
>  ##create some data to plot
> t.start <- string.to.time("2011-05-02 00:00:00")
> t.start
> t.vector <- t.start + (0:24)*3600
> t.vector
> z.vector <- 1:length(t.vector)
> z.vector
> 
> ##vector of decimal hours
> dh.vector <- decimal.hour(t.vector)
> dh.vector
> 
> plot(x=dh.vector, y=z.vector, xlim=c(0,24), xaxt="n", xlab="Hour of day",
> ylab="Some property")
>  *
> 
> Thank You so much and have a great weekend.
> 

Hi Pablo,

I'm a big fan of the "chron" package for plotting dates on axis.  Not sure if
this is what you are looking for,but might be helpful.  Here is a simple example
using the chron package and plotting dates on x-axis.  You can alter the text
labels on x-axis using substr() on the date vector. 

# create "chron"  time vector
library(chron)
excel.dates <- seq(40179.0 + 1/6, 40180.0 + 1/6, 1/6)
orig <- chron("12/30/1899")
date.time <- orig + excel.dates;
time.only <- substr(date, 11, 18)

# Y data
y.dat = rnorm(7, 10, 3)

# Plot it up!  Don't add annotations or axes for now
plot(date.time, y.dat, type="n", ann=F, axes=F)

# Add data
lines(date.time, y.dat, lwd = 2, col = "black")
points(date.time, y.dat, pch = 20, col = "magenta")
box()  # add box around plot

# Add X-Axis and label
axis(1, at=c(seq(date.time[1], date.time[length(date.time)],1/6)), tck = +0.01,
labels = F)
#label=date.time # prints date and time
label = time.only # only prints time
x.len=length(label)
text(x=date.time[1:x.len],  par("usr")[3]-.135, srt = 45, adj = 1,
          labels = label, xpd = T)
mtext(side = 1, "Time", line = 3.2)  #X-axis label

HTH,
Ken



More information about the R-help mailing list