Skip to content

Commit

Permalink
Add format arg to time_axis()
Browse files Browse the repository at this point in the history
  • Loading branch information
kbroman committed May 9, 2024
1 parent c86a049 commit f7e9982
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 13 additions & 2 deletions R/time_axis.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,22 +32,31 @@
#' 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")
}

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
Expand Down
10 changes: 8 additions & 2 deletions man/time_axis.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f7e9982

Please sign in to comment.