Learn R Programming

broman (version 0.84)

time_axis: 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)

Value

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

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.

See Also

timeplot()

Examples

Run this code
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)

Run the code above in your browser using DataLab