f_bin: Convert Binned Intervals to Readable Form

Description Usage Arguments Value Examples

View source: R/f_bin.R

Description

f_bin - Convert binned intervals to symbol form (e.g., "1 < x <= 3").

f_bin_text - Convert binned intervals to text form (e.g., "Greater than or equal to 1 to less than 3").

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
f_bin(x, l = "<", le = "<=", parse = FALSE, ...)

f_bin_text(
  x,
  greater = "Greater than",
  middle = "to",
  less = "less than",
  equal = "or equal to",
  ...
)

f_bin_text_right(x, l = "up to", le = "to", equal.digits = FALSE, ...)

f_bin_right(x, l = "<", le = "<=", equal.digits = FALSE, parse = FALSE, ...)

ff_bin(l = "<", le = "<=", parse = TRUE, ...)

ff_bin_text(
  greater = "Greater than",
  middle = "to",
  less = "less than",
  equal = "or equal to",
  ...
)

ff_bin_right(l = "<", le = "<=", equal.digits = FALSE, parse = TRUE, ...)

ff_bin_text_right(l = "up to", le = "to", equal.digits = FALSE, ...)

f_interval(x, l = "<", le = "<=", parse = FALSE, ...)

f_interval_text(
  x,
  greater = "Greater than",
  middle = "to",
  less = "less than",
  equal = "or equal to",
  ...
)

f_interval_text_right(x, l = "up to", le = "to", equal.digits = FALSE, ...)

f_interval_right(
  x,
  l = "<",
  le = "<=",
  equal.digits = FALSE,
  parse = FALSE,
  ...
)

ff_interval(l = "<", le = "<=", parse = TRUE, ...)

ff_interval_text(
  greater = "Greater than",
  middle = "to",
  less = "less than",
  equal = "or equal to",
  ...
)

ff_interval_text_right(l = "up to", le = "to", equal.digits = FALSE, ...)

ff_interval_right(l = "<", le = "<=", equal.digits = FALSE, parse = TRUE, ...)

Arguments

x

A vector of binned numbers from cut.

l

Less than symbol.

le

Less than or equal to symbol.

parse

logical. If TRUE is parsed for ggplot2 facet labels.

greater

String to use for greater.

middle

String to use for middle (defaults to 'to').

less

String to use for less.

equal

String to use for equal to. This is combined with the less or greater.

equal.digits

logical. If TRUE digits are given equal number of decimal places.

...

ignored.

Value

f_bin - Returns human readable intervals in symbol form.

f_bin - Returns human readable intervals in word form.

f_bin_text_right - Returns human readable right hand of intervals in word form.

f_bin_right - Returns human readable right hand intervals in symbol form.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
x <- cut(-1:5, 3, right = FALSE)
y <- cut(-4:10, c(-5, 2, 6, 10), right = TRUE)
z <- cut(-4:10, c(-4, 2, 6, 11), right = FALSE)

f_bin(x)
f_interval(x) #`_interval` and `_bin` are interchangeable aliases in the function names
f_bin(y)
f_bin(z)
## HTML
f_bin(z, le = '&le;')

f_bin_text(x)
f_bin_text(y)
f_bin_text(z)
f_bin_text(x, middle = 'but')
f_bin_text(x, greater = 'Above', middle = '', equal = '', less = 'to')
f_bin_text(z, greater = 'From', middle = '', equal = '', less = 'up to')

f_bin_text_right(x)
f_bin_text_right(y)
f_bin_text_right(cut(-4:10, c(-3, 2, 6, 11)))
f_bin_text_right(x, equal.digits = TRUE)

f_bin_right(x)
f_bin_right(y)
f_bin_right(x, equal.digits = TRUE)
## HTML
f_bin_right(y, le = '&le;')

## Not run: 
library(tidyverse)

mtcars %>%
    mutate(mpg2 = cut(mpg, 3)) %>%
    ggplot(aes(disp, hp)) +
        geom_point() +
        facet_wrap(~ mpg2,
            labeller = ff_bin()
        )

mtcars %>%
    mutate(mpg2 = cut(mpg, 3)) %>%
    ggplot(aes(disp, hp)) +
        geom_point() +
        facet_wrap(~ mpg2,
            labeller = function(x) f_bin_right(x, parse = TRUE)
        )

mtcars %>%
    mutate(mpg2 = cut(mpg, 3, right = FALSE)) %>%
    ggplot(aes(disp, hp)) +
        geom_point() +
        facet_wrap(~ mpg2,
            labeller = function(x) f_bin_right(x, parse = TRUE)
        )

mtcars %>%
    mutate(mpg2 = cut(mpg, 5, right = FALSE)) %>%
    ggplot(aes(mpg2)) +
        geom_bar() +
        scale_x_discrete(labels = ff_bin_text_right(l = 'Up to')) +
        coord_flip()

mtcars %>%
    mutate(mpg2 = cut(mpg, 10, right = FALSE)) %>%
    ggplot(aes(mpg2)) +
        geom_bar(fill = '#33A1DE') +
        scale_x_discrete(labels = function(x) f_wrap(f_bin_text_right(x, l = 'up to'), width = 8)) +
        scale_y_continuous(breaks = seq(0, 14, by = 2), limits = c(0, 7)) +
        theme_minimal() +
        theme(
            panel.grid.major.x = element_blank(),
            axis.text.x = element_text(size = 14, margin = margin(t = -12)),
            axis.text.y = element_text(size = 14),
            plot.title = element_text(hjust = .5)
        ) +
        labs(title = 'Histogram', x = NULL, y = NULL)

## End(Not run)

numform documentation built on Oct. 10, 2021, 1:10 a.m.