Learn R Programming

gghdx (version 0.1.4)

label_number_hdx: Format and labels numbers in HDX key figures style

Description

Use format_number_hdx() to directly format numeric vectors, and use label_number_hdx() in the same way as the scales:: family of label functions. The return value of label_number_hdx() is a function, based on the additional_prefix. So you should pass it in to scales_...() labels parameter in the same way as scales_...()

Usage

label_number_hdx(additional_prefix = "")

format_number_hdx(x, additional_prefix = "")

Value

label_number_hdx(): "labelling" function, in the same way as scales::label_...()

functions work, i.e. a function that takes x and returns a labelled character vector of length(x).

format_number_hdx(): Formatted character vector of number strings

Arguments

additional_prefix

Additional prefix to add to string, that will come between sign_prefix and the number. For example, "$" could produce a return value of -$1.1K.

x

Numeric vector to format

Details

Numeric vectors are formatted in the HDX style for key figures, which abbreviates numbers 1,000 and above to X.YK, 10,000 and above to XYK, 100,000 and above to XYZK, and the same for 1,000,000 and above, replacing the K with an M, and the same for B. Details of the data viz style can be found in the visualization guidelines

Just for continuity, values are labeled with T for trillion, and that is the maximum formatting available, anything above the trillions will continue to be truncated to report in the trillions.

Deals with negative values in case those ever need to be formatted in similar manners. Also ensures that rounding is performed so numbers look correct. Not to be used for percents, which should just use scales::label_percent().

Examples

Run this code
# label_number_hdx()

library(ggplot2)

# discrete scaling
p <- ggplot(txhousing) +
  geom_point(
    aes(
      x = median,
      y = volume
    )
  )

p

p +
  scale_x_continuous(
    labels = label_number_hdx("$")
  ) +
  scale_y_continuous(
    labels = label_number_hdx()
  )

# number_hdx()

x <- c(1234, 7654321)
format_number_hdx(x)
format_number_hdx(x, "$")

Run the code above in your browser using DataLab