[R] Time axis formatting.

Jim Lemon jim at bitwrit.com.au
Mon Aug 26 01:06:26 CEST 2013


On 08/26/2013 01:09 AM, Sudheer Joseph wrote:
> Attached is a plot with a time series. If I have a time series object in R.
> How do I get the plot in the attached format of time axis?. When I issue
> plot(ts) I get a time series plot with tic only for years.
>
>
Hi Sudheer,
If your problem is similar to that answered last week "X axis label as 
months", you can follow the suggestion by the other Jim and then add a 
custom x axis. The problem is getting the axis labels aligned, and this 
function:

tsxpos<-function(x) {
   # make sure that there is a graphics device open
   if(dev.cur() == 1) stop("A graphics device must be open")
   plotlim<-par("usr")
   divlim<-ifelse(par("xaxs") == "r",0.04,0)
   xrange<-(plotlim[2]-plotlim[1])/(1+2*divlim)
   xstart<-plotlim[1]+xrange*divlim
   nint<-length(x)-1
   return(cumsum(c(xstart,rep(xrange/nint,nint))))
}

when called after creating the original time series plot will return the 
x positions for the time series (the "x" argument is for the time series 
object). You can then pass these values to the axis function in the "at" 
argument with your desired labels in the "labels" argument. This 
function has been added to the plotrix package and will appear in the 
next version.

Jim



More information about the R-help mailing list