[R] plot with abscissa in hours/weekdays
Gabor Grothendieck
ggrothendieck at gmail.com
Tue May 16 00:57:20 CEST 2006
On 5/15/06, m p <mzp3769 at yahoo.com> wrote:
> Hello,
> How can I use plot and have abscissa in hours
> say (20:00,22:00,00:00,02:00 etc) and also weekdays
> say (Mon, Tue, Wed, ...) ?
> Is there a command that I can put into
> plot(xvec,yvec...,axes=...) that would enable that?
I assume these are two different questions: (1) hours and (2) weekdays.
Here is some sample code. See R News 4/1 Help Desk article for more
on dates. Hopefully this will give you the idea and you can use this
as a base to make it even more sophisticated if you like.
# 1. chron "times" class test data
library(chron)
x <- times(0:23/24)
y <- (1:24)^2
# plot
plot(x, y, xaxt = "n")
a <- floor(24*min(x)):ceiling(24*max(x))
axis(1, a/24, a, cex.axis = 0.7)
####################
# 2. "Date" class test data
library(chron) # need is.weekend from chron to create data
xx <- chron(1:100)
xx <- as.Date(xx[!is.weekend(xx)])
yy <- seq(length(xx))^2
# plot marking Mondays on the x axis
idx <- 1:length(xx)
plot(idx, yy, xaxt = "n", type = "l")
is.monday <- format(xx, "%a") == "Mon"
lab <- format(xx[is.monday], "%b %d")
axis(1, which(is.monday), lab, cex.axis = 0.6, tcl = -0.6)
axis(1, idx, FALSE, tcl = -0.4)
More information about the R-help
mailing list