smartTimeAxis: Produces a time axis with smart spacing

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/smartTimeAxis.R

Description

Produces a time axis on a plot with interval spacing units that are intuitive. It is intended to be applied to periods of time that do not exceed 24 hours (i.e. it does not produce a date stamp in the time axis).

Usage

1
2
smartTimeAxis(time.vec, nticks = 15, side = 1, time.format = c("hh:mm",
  "hh:mm:sspm", "hh:mm:ss pm", "hh:mm:ss", "hh:mmpm", "hh:mm pm"))

Arguments

time.vec

A time object (vector) that was used to construct the plot, presumed to be ordered chronologically

nticks

The target number of ticks to use in the axis

side

Same as the side argument in axis

time.format

Character string indicting the time format to display on the axis. The choices are displayed in the Usage. Defaults to hh:mm.

Details

smartTimeAxis attempts to choose a "natural" spacing for the time axis ticks that results in the number of ticks being as close as possible to nticks. Possibilities for natural spacings include 1, 5, 10, 15 seconds, etc., or 1, 2, 5, 10, minutes etc., or 0.5, 1, 1.5 hours, etc.

Value

Places the axis on the plot.

Author(s)

Landon Sego

See Also

axis.POSIXct

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Get data and set the options to the horizontal axis labels will be
# oriented vertically
data(timeData)
op <- par(las = 2, mfrow = c(3, 1), mar = c(4, 4, 2, 0.5))

# Make the default plot
plot(timeData, xlab = "", main = "Default intervals")

# Make the plot with specialized time axis
plot(timeData, axes = FALSE, frame.plot = TRUE, xlab = "", main = "10 minute intervals")

# Add y-axis
axis(2)

# Add the time axis
smartTimeAxis(timeData$time, nticks = 10)

# Only look at a small portion of the data with a different time format
par(mar = c(7, 4, 2, 0.5))

plot(timeData[200:237,], type = "b", axes = FALSE, frame.plot = TRUE,
     xlab = "", main = "15 second intervals")

axis(2)

smartTimeAxis(timeData[200:237,"time"], nticks = 20, time.format = "hh:mm:ss pm")

# Restore the original par settings
par(op)

Smisc documentation built on May 2, 2019, 2:46 a.m.