rnaturalearth is a source library for data various types of global mapping data.
12.1 Call Libraries
Show the code
library(rnaturalearth) # natural earth datalibrary(ggplot2) # beautiful mapslibrary(dplyr) # data wranglinglibrary(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 scalereturnclass ="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 mappinggeom_sf() # the geometry I am using
12.3.2 More Complicated Map
Show the code
ggplot(mapdata) +# the sf data that I am mappinggeom_sf(aes(fill = income_grp)) +# what goes on the map: FILLscale_fill_viridis_d(name ="Income Group", # beautiful colorsoption ="viridis") +labs(title ="Countries of the World") +# labelstheme_minimal() # minimal theme