R/scale-seconds.R

Defines functions seconds_trans scale_y_seconds scale_x_seconds

Documented in scale_x_seconds scale_y_seconds

#' @rdname time_scales
#' @export
scale_x_seconds <- function(name = "Time",
                          breaks = waiver(),
                          minor_breaks = waiver(),
                          labels = waiver(),
                          limits = NULL,
                          expand = waiver(),
                          oob = scales::censor,
                          na.value = NA_real_,
                          position = "bottom",
                          time_wrap = NULL,
                          unit = "s",
                          log = FALSE) {
  name <- sprintf("%s (%s)", name, unit)
  scale_x_continuous(
    name = name,
    breaks = breaks,
    labels = labels,
    minor_breaks = minor_breaks,
    limits = limits,
    expand = expand,
    oob = oob,
    na.value = na.value,
    position = position,
    trans = seconds_trans(time_wrap, log_tr = log)
  )
}


#' @rdname time_scales
#' @export
scale_y_seconds <- function(name = "Time",
                            breaks = waiver(),
                            minor_breaks = waiver(),
                            labels = waiver(),
                            limits = NULL,
                            expand = waiver(),
                            oob = scales::censor,
                            na.value = NA_real_,
                            position = "left",
                            time_wrap = NULL,
                            unit="s",
                            log = FALSE) {
  name <- sprintf("%s (%s)", name, unit)
  scale_y_continuous(
    name = name,
    breaks = breaks,
    labels = labels,
    minor_breaks = minor_breaks,
    limits = limits,
    expand = expand,
    oob = oob,
    na.value = na.value,
    position = position,
    trans = seconds_trans(time_wrap, log_tr = log)
  )
}


seconds_trans <- function(time_wrap = NULL, log_tr = FALSE) {
  if(is.null(time_wrap))
    formater <- function(x)format(as.numeric(x))
  else
    formater <- function(x)format((as.numeric(x) %% time_wrap))
  foo <- ifelse(log_tr,log10, identity)
  foo_inv <- ifelse(log_tr,function(x){10^x}, identity)

  scales::trans_new(
    "seconds",
    transform = function(x){structure(foo(as.numeric(x)), names = names(x))},
    inverse = function(x){foo_inv(as.numeric(x)) },
    format = formater
  )
}

Try the ggetho package in your browser

Any scripts or data that you put into this service are public.

ggetho documentation built on Nov. 24, 2022, 5:09 p.m.