Skip to content

Commit

Permalink
Add function timeplot(), like grayplot() but with x-axis as date/time
Browse files Browse the repository at this point in the history
  • Loading branch information
kbroman committed May 9, 2024
1 parent f7e9982 commit c25aafb
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export(strwidth2xlim)
export(switchv)
export(theme_karl)
export(time_axis)
export(timeplot)
export(trap)
export(triarrow)
export(trigrid)
Expand Down
49 changes: 49 additions & 0 deletions R/timeplot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#' Scatterplot with date/times on the x-axis
#'
#' Like the [grayplot()] function, but with the x-axis having date/times
#'
#' @param x X-axis coordinates of points for the plot (must be date/time values)
#'
#' @param y Y-axis coordinates of points for the plot
#'
#' @param ... Optional graphics arguments passed to [grayplot()]
#'
#' @param n Approximate number of x-axis labels (passed to [base::pretty()]).
#'
#' @param scale Passed to [time_axis()] for defining the x-axis labels
#'
#' @param format Passed to [time_axis()] for defining the x-axis labels
#'
#' @return None.
#'
#' @export
#'
#' @importFrom graphics axis
#'
#' @examples
#' n <- 100
#' y <- rnorm(n)
#' x <- seq(as.POSIXct("2024-05-01 11:23"), as.POSIXct("2024-05-01 14:50"), length.out=n)
#' timeplot(x, y)

timeplot <-
function(x, y, ..., n=5, scale=NULL, format=NULL)
{
xax <- time_axis(x, n, scale, format)

timeplot_internal <-
function(x, y, vlines=NULL, mgp=c(2, 0.5, 0), mgp.x=NULL, mgp.y=NULL, ...)
{
if(is.null(vlines)) vlines <- xax$x

if(is.null(mgp.x)) mgp.x <- mgp
if(is.null(mgp.y)) mgp.y <- mgp

grayplot(x, y, xat=NA, vlines=vlines, mgp=mgp, mgp.x=mgp.x, mgp.y=mgp.y, ...)

axis(side=1, at=xax$x, labels=xax$labels, tick=FALSE, mgp=mgp.x)
}

timeplot_internal(x, y, ...)
invisible()
}
33 changes: 33 additions & 0 deletions man/timeplot.Rd

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

0 comments on commit c25aafb

Please sign in to comment.