order_countries {passport}R Documentation

Order a vector of countries

Description

order_countries reorders a vector of countries, returning a result useful for passing to ggplot2's scale functions' limits parameters.

Usage

order_countries(
  x,
  by,
  ...,
  from = "iso2c",
  short = TRUE,
  variant = FALSE,
  factor = is.factor(x)
)

Arguments

x

A character, factor, or numeric vector of country codes or names

by

Either a length-one country code from codes or a vector the same length as x by which to order x

...

Parameters passed on to order(), including addition vectors by which to sort, decreasing, and na.last.

from

Code format from which to convert. Defaults to "iso2c"; 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

order_countries orders a vector of countries by

Value

The original vector of countries, ordered according to the parameters passed. Note that factors are not releveled, but are reordered. To relevel, pass the results to levels<-()

See Also

To change labels of a discrete scale, pass the results of country_format() to the labels parameter.

Examples

countries <- c("FR", "CP", "UZ", "BH", "BR")

order_countries(countries)

order_countries(countries, "ja")

order_countries(countries, rnorm(5))

order_countries(countries, grepl("F", countries), 1:5, decreasing = TRUE)

if (require(ggplot2, quietly = TRUE)) {
    df_countries <- data.frame(country = countries,
                               y = exp(1:5))

    ggplot(df_countries, aes(country, y)) +
        geom_col() +
        scale_x_discrete(
            limits = order_countries(df_countries$country,
                                     df_countries$y)[df_countries$y > 5],
            labels = country_format(to = "en-cia-local")
        )
}


[Package passport version 0.3.0 Index]