breaks_divMult: Compute breaks for ratio scale

View source: R/ratioScales.R

breaks_divMultR Documentation

Compute breaks for ratio scale

Description

Compute breaks for ratio scale

Usage

breaks_divMult(n = 6, nmin = 5, anchor = TRUE, splits = 5, base = exp(1))

Arguments

n

Scalar, target number of breaks

nmin

Scalar, forced minimum number of breaks

anchor

NULL or scalar, value to include as a reference point (usually 1)

splits

Integer, one of c(1,2,3). How many tick marks per "decade?"

base

a positive or complex number: the base with respect to which logarithms are computed. Defaults to e=exp(1).

Value

Vector of values to generate axis breaks

See Also

Other breaking: limit_breaks(), limitimil(), split_decades()

Examples

y <- exp(seq(-2,5, length.out = 10))
v <- log(y) # log data or data range
n <- 5

# axisTicks takes giant steps, returns values way beyond data
grDevices::axisTicks(nint = n, log = TRUE, usr = range(v))
# breaks_divMult gives ~n breaks evenly within the data
breaks_divMult(n = n)(v = y)

# if 1 is lower limit, only positive log(breaks)
breaks_divMult()(c(1, 11))
# ditto, only negative log(breaks) if 1 is upper limit
breaks_divMult()(c(0.04, 1))

# expanding range on one side of 1 doesn't leave the other side behind
breaks_divMult()(c(0.04, 2.2))
breaks_divMult()(c(0.04, 220))
breaks_divMult()(c(0.04, 2200))

x <- 1:10
dat <- data.frame(x, y)
dat %>% ggplot2::ggplot(ggplot2::aes(x, y))+
     ggplot2::geom_point()+
     ggplot2::geom_hline(yintercept = 1, linewidth = 0.2) +
     ggplot2::scale_y_continuous(
     transform = "log"
     , breaks = breaks_divMult()
     , labels = label_divMult()
     )

# custom breaks might still be needed when y-range is small
y2 <- seq(0.68, 2.2, length.out = 10)

dat2 <- data.frame(x, y2)

dat2 %>% ggplot2::ggplot(ggplot2::aes(x, y2))+
     ggplot2::geom_point()+
     ggplot2::geom_hline(yintercept = 1, linewidth = 0.2) +
     ggplot2::scale_y_continuous(
     transform = "log"
    # , breaks = breaks_divMult()
    , breaks = c(seq(0.4, 2.2, by = 0.2))
     , labels = label_divMult()
     )



mikeroswell/ratioScales documentation built on July 18, 2024, 6:36 p.m.