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 |
... |
Parameters passed on to |
from |
Code format from which to convert. 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
order_countries
orders a vector of countries by
itself converted to a country code or name if
by
is a code fromcodes
to which to converta sortable vector if
by
is a vector of the same length asx
-
x
itself if neither is supplied.
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")
)
}