country_format {passport}R Documentation

Construct formatter function to format country codes as country names

Description

country_format is a constructor function that returns a function to format country codes as country names suitable for passing to ggplot2's scale functions' label parameters.

Usage

country_format(
  from = "iso2c",
  to = "en",
  short = TRUE,
  variant = FALSE,
  factor
)

Arguments

from

Code format from which to convert. Defaults to "iso2c"; see codes for more options.

to

Language code of country names desired. Defaults to "en"; see codes for more options.

short

Whether to use short alternative name when available. Can be length 1 or the same length as x.

variant

Whether to use variant alternative name when available. Can be length 1 or the same length as x.

factor

If TRUE, returns factor instead of character vector. If not supplied, defaults to is.factor(x)

Details

A frequent reason to convert country codes back to country names is to make data visualizations more readable. While both a code and name could be stored in a data frame, the computation and extra storage required can be avoided by transforming codes to names directly within the visualization via a formatter function. as_country_name() could be used without parentheses to format ISO 2-character codes as English names, but format_country allows greater flexibility, returning a formatter function with the specified parameters set.

Value

A function that accepts a vector of country codes and returns them as country names.

See Also

For controlling the order of a discrete scale, pass the results of order_countries() to limits.

Examples

if (require(ggplot2, quietly = TRUE)) {
    ggplot(data.frame(country = c("KOR", "MMR", "TWN", "COG"),
                      y = 1:4),
           aes(x = country, y = y)) +
        geom_col() +
        scale_x_discrete(labels = country_format(from = "iso3c"))
}


[Package passport version 0.3.0 Index]