numform (version 0.5.0)

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

Description

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

Usage

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.

Examples

Run this code
# NOT RUN {
## 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(.))
# }

Run the code above in your browser using DataCamp Workspace