12  Using Data From rnaturalearth

rnaturalearth is a source library for data various types of global mapping data.

12.1 Call Libraries

Show the code
library(rnaturalearth) # natural earth data

library(ggplot2) # beautiful maps

library(dplyr) # data wrangling

library(sf) # simple (spatial) features

12.2 Get mapdata As sf

ne_countries stands for Natural Earth Countries.

I use ne_countries() to create the mapdata dataset as an sf object (Chapter 7).

Show the code
mapdata <- ne_countries(scale = "medium", # medium scale
                        returnclass = "sf") # as sf object

12.3 Map

12.3.1 Simple Basic Map

I make a simple map of this sf object with ggplot (Chapter 14).

Show the code
ggplot(mapdata) + # the data I am mapping
  geom_sf() # the geometry I am using

12.3.2 More Complicated Map

Show the code
ggplot(mapdata) + # the sf data that I am mapping
  geom_sf(aes(fill = income_grp)) + # what goes on the map: FILL
  scale_fill_viridis_d(name = "Income Group", # beautiful colors
                       option = "viridis") +
  labs(title = "Countries of the World") + # labels
  theme_minimal() # minimal theme