R/log10_minor_breaks.R

#' Add log10 minor breaks to a ggplot with ggplot2::scale_x/y_log10() activated.
#'
#' @param ... Added via a `+` in a `ggplot2` chain.
#'
#' @return A log10 plot with minor breaks.
#' @export
#'
#' @examples
#' ggplot2::ggplot() + ggplot2::scale_x_log10('x', minor_breaks = log10_minor_breaks())
log10_minor_breaks <- function (...) {
  function(x) {
    minx <- floor(min(log10(x), na.rm = T)) - 1
    maxx <- ceiling(max(log10(x), na.rm = T)) + 1
    n_major <- maxx - minx + 1
    major_breaks <- seq(minx, maxx, by = 1)
    minor_breaks <-
      rep(log10(seq(1, 9, by = 1)), times = n_major) +
      rep(major_breaks, each = 9)

    return(10 ^ (minor_breaks))
  }
}
ConGibbs10/utilsGibbs documentation built on Jan. 7, 2022, 1:03 p.m.