f_degree: Format Degrees (e.g., Temperature, Coordinates)

Description Usage Arguments Value Note Examples

View source: R/f_degree.R

Description

Format numbers into degree format for strings, text, titles, and scales.

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
65
f_fahrenheit(
  x,
  digits = getOption("numformdigits"),
  prefix = NULL,
  suffix = TRUE,
  absolute.value = suffix,
  type = "scale",
  symbol = "°",
  ...
)

f_celcius(
  x,
  digits = getOption("numformdigits"),
  prefix = NULL,
  suffix = TRUE,
  absolute.value = suffix,
  type = "scale",
  symbol = "°",
  ...
)

f_longitude(
  x,
  digits = getOption("numformdigits"),
  prefix = NULL,
  suffix = TRUE,
  absolute.value = suffix,
  type = "scale",
  symbol = "°",
  ...
)

f_latitude(
  x,
  digits = getOption("numformdigits"),
  prefix = NULL,
  suffix = TRUE,
  absolute.value = suffix,
  type = "scale",
  symbol = "°",
  ...
)

f_degree(
  x,
  type = c("scale", "text", "scale", "title", "string"),
  digits = getOption("numformdigits"),
  prefix = NULL,
  suffix = TRUE,
  absolute.value = suffix,
  symbol = "°",
  measure = c("fahrenheit", "celcius", "C", "F", "longitude", "latitude"),
  ...
)

ff_degree(...)

ff_celcius(...)

ff_fahrenheit(...)

ff_longitude(...)

ff_latitude(...)

Arguments

x

A vector of values.

digits

The number of digits to use. Defaults to 1. Can be set globally via: options(numformdigits = n) where n is the number of digits beyond the decimal point to include.

prefix

A prefix to use before the parenthesis + units when type = 'title'.

suffix

logical. If TRUE a suffix will be added corresponding to the measure:

celcius

A capital C will be used

fahrenheit

A capital F will be used

longitude

Capital W and E will be used

latitude

Capital S and N will be used

absolute.value

logical. If TRUE the absolute value of x will be used. This is useful for coordinates when E/W or N/S indicate direction.

type

One of c('scale', 'text', 'title', 'string'):

scale

To be used for ggplot2 scales (i.e., axis or legend)

text

To be used for ggplot2 text (i.e., geom_text, annotate; note that parse = TRUE must be set

title

To be used for ggplot2 titles (e.g., main title, axis title, legend title); ignores x values

string

To be used for plain text, especially table formatting and allows control over the degree symbol used

symbol

A symbol to use for degree when type = 'string'.

measure

One of c('fahrenheit', 'celcius', 'C', 'F', 'longitude', 'latitude'). There are functions by these names (e.g., f_celcius) but not C or F. These functions may be clearer than using f_degree and then specifying measure.

...

ignored.

Value

Returns number string(s) with degree symbols.

Note

Note that this function differs a bit from other f_ functions in that in needs a type. This is because other f_ functions return a plain text representation that is generalizable across usages (titles, tables, axis, geom_text, etc). This function has notation that requires special parsing by various usages hence requiring the type argument.

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
## used for ggplot2 axis.text & legend scale
f_celcius(37, type = 'scale')

## used for ggplot2 geom_text
f_celcius(37, type = 'text')

## used for ggplot2 titles
f_celcius(prefix = "My Title",  type = 'title')

## used for table and string formatting
f_celcius(37, type = 'string')
f_celcius(37, type = 'string', symbol = '\\textdegree')  # LaTeX

## Not run: 
if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyverse, maps, viridis, mapproj)

states <- map_data("state")
arrests <- USArrests
names(arrests) <- tolower(names(arrests))
arrests$region <- tolower(rownames(USArrests))
choro <- merge(states, arrests, sort = FALSE, by = "region")
choro <- choro[order(choro$order), ]

ggplot(choro, aes(long, lat)) +
    geom_polygon(aes(group = group, fill = assault)) +
    coord_map("albers",  at0 = 45.5, lat1 = 29.5) +
    scale_y_continuous(labels = f_latitude) +
    scale_x_continuous(labels = f_longitude)

ggplot(choro, aes(long, lat)) +
    geom_polygon(aes(group = group, fill = assault)) +
    coord_map("albers",  at0 = 45.5, lat1 = 29.5) +
    scale_y_continuous(labels = ff_latitude(suffix = FALSE)) +
    scale_x_continuous(labels = ff_longitude(suffix = FALSE))


world <- map_data(map="world")

ggplot(world, aes(map_id = region, x = long, y = lat)) +
    geom_map(map = world, aes(map_id = region), fill = "grey40",
        colour = "grey70", size = 0.25) +
    scale_y_continuous(labels = f_latitude) +
    scale_x_continuous(labels = f_longitude)


data_frame(
    Event = c('freezing water', 'room temp', 'body temp', 'steak\'s done',
        'hamburger\'s done', 'boiling water'),
    F = c(32, 70, 98.6, 145, 160, 212)
) %>%
    mutate(
        C = (F - 32) * (5/9),
        Event = f_title(Event),
        Event = factor(Event, levels = unique(Event))
    ) %>%
    ggplot(aes(Event, F, fill = F)) +
        geom_col() +
        geom_text(aes(y = F + 4, label = f_fahrenheit(F, digits = 1, type = 'text')),
            parse = TRUE, color = 'grey60') +
        scale_y_continuous(
            labels = f_fahrenheit, limits = c(0, 220), expand = c(0, 0),
            sec.axis = sec_axis(trans = ~(. - 32) * (5/9), labels = f_celcius,
            name = f_celcius(prefix = 'Temperature ', type = 'title'))
        ) +
        scale_x_discrete(labels = ff_replace(pattern = ' ', replacement = '\n')) +
        scale_fill_viridis(option =  "magma", labels = f_fahrenheit, name = NULL) +
        theme_bw() +
        labs(
            y = f_fahrenheit(prefix = 'Temperature ', type = 'title'),
            title = f_fahrenheit(prefix = 'Temperature of Common Events ', type = 'title')
        ) +
        theme(
            axis.ticks.x = element_blank(),
            panel.border = element_rect(fill = NA, color = 'grey80'),
            panel.grid.minor.x = element_blank(),
            panel.grid.major.x = element_blank()
        )


data_frame(
    Event = c('freezing water', 'room temp', 'body temp', 'steak\'s done',
        'hamburger\'s done', 'boiling water', 'sun surface', 'lighting'),
    F = c(32, 70, 98.6, 145, 160, 212, 9941, 50000)
) %>%
    mutate(
        Event = f_title(Event),
        C = (F - 32) * (5/9)
    ) %>%
    mutate(
        F = f_degree(F, measure = 'F', type = 'string'),
        C = f_degree(C, measure = 'C', type = 'string', zero = '0.0')
    )  %>%
    data.frame(stringsAsFactors = FALSE, check.names = FALSE) %>%
    pander::pander(split.tables = Inf, justify = alignment(.))

## End(Not run)

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