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 |
to |
Language code of country names desired. Defaults to |
short |
Whether to use short alternative name when available. Can be
length 1 or the same length as |
variant |
Whether to use variant alternative name when available. Can
be length 1 or the same length as |
factor |
If |
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"))
}