Learn R Programming

EpiNow2 (version 1.1.0)

country_map: Generate a country map for a single variable.

Description

This general purpose function can be used to generate a country map for a single variable. It has few defaults but the data supplied must contain a region_code variable for linking to mapping data. This function requires the installation of the rnaturalearth package.

Usage

country_map(
  data = NULL,
  country = NULL,
  variable = NULL,
  variable_label = NULL,
  trans = "identity",
  fill_labels = NULL,
  scale_fill = NULL,
  region_col_ne = "provnum_ne",
  ...
)

Arguments

data

Dataframe containing variables to be mapped. Must contain a region_code variable.

country

Character string indicating the name of the country to be mapped.

variable

A character string indicating the variable to map data for. This must be supplied.

variable_label

A character string indicating the variable label to use. If not supplied then the underlying variable name is used.

trans

A character string specifying the transform to use on the specified metric. Defaults to no transform ("identity"). Other options include log scaling ("log") and log base 10 scaling ("log10"). For a complete list of options see ggplot2::continous_scale.

fill_labels

A function to use to allocate legend labels. An example (used below) is scales::percent, which can be used for percentage data.

scale_fill

Function to use for scaling the fill. Defaults to a custom ggplot2::scale_fill_manual, which expects the possible values to be "Increasing", "Likely increasing", "Likely decreasing", "Decreasing" or "Unsure".

region_col_ne

Character string indicating the name of a column in the data returned by rnaturalearth::ne_states() that data$region_code corresponds to. Possibilities include provnum_ne, name, fips and others and will depend on which country you are mapping.

...

Additional arguments passed to the scale_fill function

Value

A ggplot2 object containing a country map.

Examples

Run this code
# NOT RUN {
if(requireNamespace("rnaturalearth") & requireNamespace("scales")){
# Example 1
# If you know the provnum_ne codes you can use them directly
eg_data <- data.table::data.table(variable = c("Increasing", 
                                               "Decreasing", 
                                               "Unsure", 
                                               "Likely decreasing",
                                               "Likely increasing"),
                                  region_code = c(5, 7, 6, 8, 9))
# Make variable a factor so the ordering is sensible
eg_data$variable <- factor(eg_data$variable, levels = c("Decreasing", "Likely decreasing",
                                                        "Unsure", "Likely increasing",
                                                        "Increasing"))

country_map(data = eg_data, country = "Australia", variable = "variable")


# Example 2
# Sometimes it will be more convenient to join your data by name than provnum_ne code:
us_data <- data.table::data.table(variable = c("Increasing", 
                                               "Decreasing", 
                                               "Unsure", 
                                               "Likely decreasing",
                                               "Likely increasing"),
                                  region_code = c("California",
                                                  "Texas",
                                                  "Florida",
                                                  "Arizona",
                                                  "New York"))
# Make variable a factor so the ordering is sensible in the legend
us_data$variable <- factor(us_data$variable, levels = c("Decreasing", "Likely decreasing",
                                                        "Unsure", "Likely increasing",
                                                        "Increasing"))

country_map(data = us_data, country = "United States of America",
            variable = "variable", region_col_ne = "name")

}
# }

Run the code above in your browser using DataLab