R/log_breaks.R

Defines functions log_breaks

Documented in log_breaks

#' Log Breaks
#'
#' This function applies log breaks. It was copied from here:
#' https://stackoverflow.com/questions/30179442/plotting-minor-breaks-on-a-log-scale-with-ggplot
#'
#' @param maj Boolean, major breaks
#' @param radix A number, The radix of the log
#'
#' @examples
#'  log_breaks(TRUE, 10)

log_breaks = function(maj, radix=10) {
  function(x) {
    minx         = floor(min(logb(x,radix), na.rm=T)) - 1
    maxx         = ceiling(max(logb(x,radix), na.rm=T)) + 1
    n_major      = maxx - minx + 1
    major_breaks = seq(minx, maxx, by=1)
    if (maj) {
      breaks = major_breaks
    } else {
      steps = logb(1:(radix-1),radix)
      breaks = rep(steps, times=n_major) +
        rep(major_breaks, each=radix-1)
    }
    radix^breaks
  }
}
LCedrig/torzeug documentation built on Aug. 30, 2020, 12:01 a.m.