Skip to content

Changing x-axis when dealing with time

3 messages · Pablo Rosado, Kenneth Takagi

2 days later
#
Pablo Rosado <pablojrosado <at> lbl.gov> writes:
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
#
Ken <katakagi <at> bu.edu> writes:
Found one error in my script above.  Correction:

time.only <- substr(date.time, 11, 18)

Ken