[Questioning] 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. Status of this function is currently questioning as it is uncertain if it is in use. Future releases may depreciate it.

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 "Stable".

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

# \donttest{
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",
      "Stable",
      "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",
    "Stable", "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",
      "Stable",
      "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",
    "Stable", "Likely increasing",
    "Increasing"
  ))

  country_map(
    data = us_data, country = "United States of America",
    variable = "variable", region_col_ne = "name"
  )
}
#> Loading required namespace: rnaturalearth
#> The rnaturalearthhires package needs to be installed.
#> Installing the rnaturalearthhires package.
#> Installing package into ‘/Users/runner/work/_temp/Library’
#> (as ‘lib’ is unspecified)
#> Error in value[[3L]](cond): Failed to install the rnaturalearthhires package.
#>   Please try installing the package for yourself using the following command: 
#>      install.packages("rnaturalearthhires", repos = "http://packages.ropensci.org", type = "source")
# }