diff --git a/DESCRIPTION b/DESCRIPTION index 59dccca..52e4bdc 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: broman -Version: 0.83-1 -Date: 2024-05-07 +Version: 0.83-2 +Date: 2024-05-08 Title: Karl Broman's R Code Description: Miscellaneous R functions, including functions related to graphics (mostly for base graphics), permutation tests, running diff --git a/NEWS.md b/NEWS.md index 149fb57..fa7588f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,12 +1,12 @@ Revision history for the R/broman package ----------------------------------------- -## Version 0.83, 2024-05-07 +## Version 0.83-2, 2024-05-08 - Allow `NA`s in `runningmean()` and `runningratio()`. - Add `scale` argument to `time_axis()` to allow control over time - labels it gives. + labels it gives, and `format` argument for customized labels. ## Version 0.82, 2024-05-05 diff --git a/R/time_axis.R b/R/time_axis.R index b760186..3181693 100644 --- a/R/time_axis.R +++ b/R/time_axis.R @@ -10,6 +10,8 @@ #' `"sec"`, `"min"`, `"hr"`, or `"day"`. If NULL, scale is chosen #' based on the `times`. #' +#' @param format If provided, used in place of `scale` for formating the times. + #' @return A data frame with the numeric values to plot plus labels to use. #' #' @export @@ -30,15 +32,19 @@ #' 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) time_axis <- - function(times, n=8, scale=NULL) + function(times, n=8, scale=NULL, format=NULL) { if(!("POSIXct" %in% class(times) || "POSIXt" %in% class(times))) { stop("times should be a vector of date/times") @@ -46,6 +52,11 @@ time_axis <- prettyx <- pretty(times, n=n) + if(!is.null(format)) { # use custom format, ignoring scale + return(data.frame(x=prettyx, + labels=format(prettyx, format))) + } + r <- range(as.numeric(times)) dr <- diff(r) # determine range diff --git a/man/time_axis.Rd b/man/time_axis.Rd index a120b18..2f7ff0a 100644 --- a/man/time_axis.Rd +++ b/man/time_axis.Rd @@ -4,7 +4,7 @@ \alias{time_axis} \title{Set up a time-based axis} \usage{ -time_axis(times, n = 8, scale = NULL) +time_axis(times, n = 8, scale = NULL, format = NULL) } \arguments{ \item{times}{A vector of date/times that will be plotted} @@ -14,6 +14,8 @@ time_axis(times, n = 8, scale = NULL) \item{scale}{Forced choice of scale for axis labels: \code{"sec"}, \code{"min"}, \code{"hr"}, or \code{"day"}. If NULL, scale is chosen based on the \code{times}.} + +\item{format}{If provided, used in place of \code{scale} for formating the times.} } \value{ A data frame with the numeric values to plot plus labels to use. @@ -37,10 +39,14 @@ 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) }