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.

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

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

...

Additional arguments passed to the scale_fill function

Value

A ggplot2 object containing a country map.

Examples

country_map
#> function (data = NULL, country = NULL, variable = NULL, variable_label = NULL, #> trans = "identity", fill_labels = NULL, scale_fill = NULL, #> ...) #> { #> if (is.null(variable_label)) { #> variable_label <- variable #> } #> country <- rnaturalearth::ne_countries(scale = "large", country = country, #> returnclass = "sf") #> regions <- rnaturalearth::ne_states(country, returnclass = "sf") #> data <- data.table::as.data.table(date)[, `:=`(provnum_ne, #> region_code)] #> regions_with_data <- merge(regions, data, by = c("provnum_ne"), #> all.x = TRUE) #> if (is.null(fill_labels)) { #> fill_labels <- ggplot2::waiver() #> } #> map <- ggplot(regions_with_data) + ggplot2::geom_sf(aes(fill = .data[[variable]]), #> col = "white", alpha = 0.8, size = 0.2) + ggplot2::geom_sf(data = country, #> col = "darkgrey", fill = NA, alpha = 1, size = 0.4) #> map <- EpiNow::theme_map(map, continuous = is.numeric(regions_with_data[[variable]]), #> variable_label = variable_label, trans = trans, fill_labels = fill_labels, #> scale_fill = NULL, breaks = levels(regions_with_data[[variable]]), #> ...) #> return(map) #> } #> <bytecode: 0x10f1682b8> #> <environment: namespace:EpiNow>