Learn R Programming

qPRAentry (version 0.1.0)

plot_countries: Plot values on a map at country level

Description

Plots country values on a map using data provided and allows customisation of various aesthetics, such as colors, legend title, and title.

Usage

plot_countries(
  data,
  iso_col,
  values_col,
  colors = NULL,
  na_value = "grey",
  title = NULL,
  legend_title = NULL
)

Value

A ggplot object with the plotted countries.

Arguments

data

A data frame containing the values to be plotted on the map.

iso_col

A string specifying the column name in data with the ISO 3166-1 (alpha-2) country codes. See ISO 3166 Maintenance Agency for details on country codes.

values_col

A string specifying the column name in data with the values to be plotted.

colors

Optional vector of colors used in the gradient scale.

na_value

Color for missing values (default is "grey").

title

A title for the plot (default is NULL).

legend_title

A title for the legend. Default NULL, name in the values_col.

Details

Extracts an sf object from the giscoR package. It uses the ggplot2 package for the representation. Also, it supports the addition of other ggplot2 options (see examples).

Examples

Run this code
# Simulated data trade in Northern America
data("datatrade_NorthAm")
# Mean of internal production for each country
library(dplyr)
data_plot <- datatrade_NorthAm$internal_production %>% 
  group_by(reporter) %>% 
  summarise(mean_value = mean(value))

head(data_plot)

#Plot
pl <- plot_countries(data = data_plot,
                     iso_col = "reporter",
                     values_col = "mean_value")
pl

# Changing colors and adding other ggplot2 options
library(ggplot2)
pl <- plot_countries(data = data_plot,
                     iso_col = "reporter",
                     values_col = "mean_value",
                     colors = c("white", "lightblue", "darkblue"),
                     title = "Plot internal production",
                     legend_title = "units")
pl + 
  xlim(-170, -20) + ylim(10, 90) +
  theme_bw()
               

Run the code above in your browser using DataLab