Learn R Programming

cheapr (version 1.1.0)

get_breaks: Pretty break-points for continuous (numeric) data

Description

The distances between break-points are always equal in this implementation.

Usage

get_breaks(x, n = 10, ...)

# S3 method for default get_breaks(x, n = 10, ...)

# S3 method for numeric get_breaks( x, n = 10, pretty = TRUE, expand_min = FALSE, expand_max = pretty, ... )

# S3 method for integer64 get_breaks(x, n = 10, ...)

Value

A numeric vector of break-points.

Arguments

x

A numeric vector.

n

Number of breakpoints. You may get less or more than requested.

...

Extra arguments passed onto methods.

pretty

Should pretty break-points be prioritised? Default is TRUE. If FALSE bin-widths will be calculated as diff(range(x)) / n.

expand_min

Should smallest break be extended beyond the minimum of the data? Default is FALSE. If TRUE then min(get_breaks(x)) is ensured to be less than min(x).

expand_max

Should largest break be extended beyond the maximum of the data? Default is TRUE. If TRUE then max(get_breaks(x)) is ensured to be greater than max(x).

See Also

bin as_discrete

Examples

Run this code
library(cheapr)

set.seed(123)
ages <- sample(0:80, 100, TRUE)

# Pretty
get_breaks(ages, n = 10)
# Not-pretty
# bin-width is diff(range(ages)) / n_breaks
get_breaks(ages, n = 10, pretty = FALSE)

# `get_breaks()` is left-biased in a sense, meaning that
# the first break is always <= `min(x)` but the last break
# may be < `max(x)`

# To get right-biased breaks we can use a helper like so..

right_breaks <- function(x, ...){
  -get_breaks(-x, ...)
}

get_breaks(4:24, 10)
right_breaks(4:24, 10)

# Use `rev()` to ensure they are in ascending order
rev(right_breaks(4:24, 10))

Run the code above in your browser using DataLab