d3_format: d3.format

Description Usage Arguments Value Examples

View source: R/d3_format.R

Description

Format numbers for human consumption with d3.format.

Usage

1
2
3
d3_format(specifier, prefix = "", suffix = "", locale = "en-US")

d3.format(specifier, prefix = "", suffix = "", locale = "en-US")

Arguments

specifier

A specifier to format value(s), see https://github.com/d3/d3-format#format for details.

prefix

Character string to append before formatted value.

suffix

Character string to append after formatted value.

locale

Locale to use, for example "fr-FR" for french, see possible values here: https://github.com/d3/d3-format/tree/master/locale.

Value

a character vector

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
x <- c(123456.789, 987654.321)
d3_format(",.2s")(x)


# Percentage
d3_format(".0%")(0.123)
d3_format(".1%")(0.123)
d3_format(".2%")(0.123)


# Fixed width
d3_format("20")(c(1, 10, 100))

# Grouped thousands with two significant digits
d3_format(",.2r")(c(4223, 8445, 24556))


# Use a locale
d3_format(",.2r", locale = "fr-FR")(c(4223, 8445, 24556))



# Use with ggplot2

library(ggplot2)

dat <- data.frame(
  x = c("format", "numeric", "values", "with ease"),
  y = c(1233172L, 1467863L, 1953877L, 1382088L)
)

ggplot(dat) +
  geom_col(aes(x, y)) +
  scale_y_continuous(labels = d3_format("$,.3~s"))

dreamRs/d3.format documentation built on June 7, 2020, 5:15 a.m.