[R] making a Time of Day axis

From: Alan Jackson <Rhelp_at_ajackson.org>
Date: Tue, 19 Jun 2007 20:37:49 -0500

I am wrestling with time and date data. I came up with a way to plot and label a histogram with "time of day" on the x-axis, but it seemed like a lot more work than should be necessary. Is there a better way to do what I am trying to do?

# read input data

data = read.table("input.dat", header=T)
# Create date-time and chron objects

datetime = strptime(paste(data[,3], data[,2]),format="%m/%d/%y %H:%M") time = times(paste(as.vector(data[,2]), ":00",sep=""))
# Put it all into a data frame
data = data.frame(time, datetime, data$Trip) names(data) = c("Time","DateTime","Trip") attach(data)
# Create time of day array

times = as.numeric(chron(times = Time))
tod = subset(times, Trip=='m');
# Plot base histogram

hist(tod, axes=F, main="Morning Bus Arrival Times", xlab="Time", col="blue"); axis(2);
# where are the tics to be?

tics = seq(min(tod), max(tod), (max(tod)-min(tod))/5);
# build a labeled x-axis for the plot

axis(1, tics, labels=sub(":00$","",as.character(chron(times=tics, out.format="h:m:s"))));

# cleanup


| Alan K. Jackson            | To see a World in a Grain of Sand      |
| alan_at_ajackson.org          | And a Heaven in a Wild Flower,         |
| www.ajackson.org           | Hold Infinity in the palm of your hand |
| Houston, Texas             | And Eternity in an hour. - Blake       |

R-help_at_stat.math.ethz.ch mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Wed 20 Jun 2007 - 01:42:47 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 20 Jun 2007 - 06:32:34 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.