Description Usage Arguments Details Value See Also Examples
View source: R/country_format.R
order_countries
reorders a vector of countries, returning a result useful
for passing to ggplot2's scale functions' limits
parameters.
1 2 3 4 5 6 7 8 9 |
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 |
order_countries
orders a vector of countries by
itself converted to a country code or name if by
is a code from codes
to which to convert
a sortable vector if by
is a vector of the same length as x
x
itself if neither is supplied.
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<-()
To change labels of a discrete scale, pass the results of
country_format()
to the labels
parameter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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")
)
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.