Skip to contents

Compute breaks for ratio scale

Usage

breaks_divMult(n = 6, nmin = 5, anchor = TRUE, splits = 3, 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))
#> [1] 1e-02 1e-01 1e+00 1e+01 1e+02 1e+03 1e+04 1e+05
# breaks_divMult gives ~n breaks evenly within the data
breaks_divMult(n = n)(v = y)
#>  [1]   0.1   0.2   0.5   1.0   2.0   5.0  10.0  20.0  50.0 100.0 200.0

# if 1 is lower limit, only positive log(breaks)
breaks_divMult()(c(1, 11))
#> [1]  1  2  5 10 20
# ditto, only negative log(breaks) if 1 is upper limit
breaks_divMult()(c(0.04, 1))
#> [1] 1.00 0.20 0.10 0.02

# expanding range on one side of 1 doesn't leave the other side behind
breaks_divMult()(c(0.04, 2.2))
#> [1] 0.02 0.10 0.20 1.00 2.00 3.00
breaks_divMult()(c(0.04, 220))
#>  [1] 2e-02 1e-01 2e-01 1e+00 2e+00 5e+00 1e+01 2e+01 5e+01 1e+02 2e+02 5e+02
breaks_divMult()(c(0.04, 2200))
#>  [1] 2e-02 1e-01 2e-01 1e+00 2e+00 5e+00 1e+01 2e+01 5e+01 1e+02 2e+02 5e+02
#> [13] 1e+03 2e+03 5e+03

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(
     trans = "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(
     trans = "log"
    # , breaks = breaks_divMult()
    , breaks = c(seq(0.4, 2.2, by = 0.2))
     , labels = label_divMult()
     )