Making Maps

Published

October 1, 2025

Note: This is based off Sarah’s Notes

Making Maps in R

Let’s plot the global human development index (HDI). The HDI metric from the United Nations Development Program (UNDP) is a summary measure of the average achievement of a country in key dimensions of human development: a long and healthy life, being knowledgeable, and a decent standard of living (value ranges from 0 to 1, higher = better).

Load Libraries

# install.packages("maps")
# install.packages("countrycode")
library(countrycode)
library(dplyr)
library(stringr)
library(ggplot2)
library(maps)

The maps package contains outlines of several continents, countries, and states (examples: world, usa, state) that have been with R for a long time. maps has its own plotting function, but we will use the map_data() function of ggplot2 to make a data frame that ggplot2 can operate on.

Create a data frame from map outlines

world <- map_data("world")

The new world data frame has the following variables: long for longitude, lat for latitude, group tells which adjacent points to connect, order refers to the sequence by which the points should be connected, and region and subregion annotate the area surrounded by a set of points.

Make a simple world map

geom_polygon() draws maps with gray fill by default and coord_fixed() specifies the aspect ratio of the plot (every y unit is 1.3 times longer than the x unit).

world <- map_data("world")
worldplot <- ggplot() +
  geom_polygon(data = world, aes(x=long, y = lat, group = group)) + 
  coord_fixed(1.3)
worldplot

Download a file from the internet directly into R

# this grabs the file and stores in your current directory
# curious where this is?  getwd() to find out where
download.file(
  url = "https://euclid.nmu.edu/~joshthom/teaching/dat309/week6/hdi.data",
  destfile = "hdi.data")

# this data was a tibble in R that I saved using
# write_rds(HDI,"hdi.data")
hdi <- read_rds("hdi.data")

load the global climate event data

library(tidyverse)
# load the global climate event data
gce <- read_csv("~/g/teaching/dat309/week6/global_climate_events_economic_impact_2020_2025.csv")

Prepare to merge data

library(tidyverse)
# add variable in world data that is commonly used for merging
world <- world |> mutate(cname = countrycode(
  region, 
  origin = 'country.name.en', 
  destination = 'genc3c'))

gce <- gce |> mutate(cname = countrycode(
  country, 
  origin = 'country.name.en', 
  destination = 'genc3c'))

world_gce <- left_join(gce, world,by="cname")

final plotting

gce_plot <- ggplot(data = world_gce, mapping = aes(x = long, y = lat, group = group)) + 
  coord_fixed(1.3) +
  geom_polygon(aes(fill = impact_per_capita)) +
  scale_fill_distiller(palette ="RdBu", direction = -1) + # or direction=1
  ggtitle("Global Events") +
  theme_minimal()

gce_plot