time_axis: Set up a time-based axis

View source: R/time_axis.R

time_axisR Documentation

Set up a time-based axis

Description

Set up a time-based axis for base graphics

Usage

time_axis(times, n = 8, scale = NULL, format = NULL)

Arguments

times

A vector of date/times that will be plotted

n

Number of values to use in axis

scale

Forced choice of scale for axis labels: "sec", "min", "hr", or "day". If NULL, scale is chosen based on the times.

format

If provided, used in place of scale for formating the times.

Value

A data frame with the numeric values to plot plus labels to use.

See Also

timeplot()

Examples

n <- 100
y <- rnorm(n)

# labels as days
x <- seq(as.POSIXct("2024-05-01 11:23"), as.POSIXct("2024-05-07 14:50"), length.out=n)
xax <- time_axis(x)
grayplot(x, y, xat=NA, vlines=xax$x)
axis(side=1, at=xax$x, labels=xax$label, mgp=c(2.1, 0.5, 0), tick=FALSE)

# labels as HH:MM
x <- seq(as.POSIXct("2024-05-01 11:23"), as.POSIXct("2024-05-01 14:50"), length.out=n)
xax <- time_axis(x)
grayplot(x, y, xat=NA, vlines=xax$x)
axis(side=1, at=xax$x, labels=xax$label, mgp=c(2.1, 0.5, 0), tick=FALSE)

# labels as seconds
x <- seq(as.POSIXct("2024-05-01 11:23:05.3"), as.POSIXct("2024-05-01 11:23:55.7"), length.out=n)
xax <- time_axis(x)
grayplot(x, y, xat=NA, vlines=xax$x)
axis(side=1, at=xax$x, labels=xax$label, mgp=c(2.1, 0.5, 0), tick=FALSE)

# custom time format
xax <- time_axis(x, format="%H:%M:%S")
grayplot(x, y, xat=NA, vlines=xax$x)
axis(side=1, at=xax$x, labels=xax$label, mgp=c(2.1, 0.5, 0), tick=FALSE)

broman documentation built on May 29, 2024, 7:18 a.m.

Related to time_axis in broman...