World Population

Introduction

Creating maps in R can be an exciting way to visualize data, and there are several powerful libraries that make this task easier. In this guide, we will create a world population map using the leaflet, sf, spData, and tmap packages. We’ll walk through the process step by step, starting from loading the necessary libraries to rendering interactive maps. By the end, you should have a good understanding of how to create your own maps in R.

Setup

First, let’s ensure that our R environment is properly configured. We start by setting up the chunk options to display the code and output.

Load the Libraries

To create maps, we need to load the following libraries:

  • leaflet: For creating interactive maps.
  • sf: For handling spatial data.
  • spData: For accessing spatial datasets.
  • tmap: For creating thematic maps.
## Warning: package 'leaflet' was built under R version 4.4.1
## Warning: package 'sf' was built under R version 4.4.1
## Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE
## Warning: package 'spData' was built under R version 4.4.1
## To access larger datasets in this package, install the spDataLarge
## package with: `install.packages('spDataLarge',
## repos='https://nowosad.github.io/drat/', type='source')`
## Warning: package 'tmap' was built under R version 4.4.1
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')

Load the Data

Next, we load the world population data. The spData package provides a dataset called world, which contains various attributes of countries, including population.

data("world")

Plotting the Data

Basic Plot

We can start by creating a basic plot of the world population using the plot function. This gives us a quick visualization of the data

plot(world['pop'])

### Interactive Map with Leaflet

To create a more interactive map, we use the leaflet package. The following code creates a simple map with the default tile layer and country polygons.

leaflet(world) %>%
  addTiles() %>%
  addPolygons()

Adding a Color Palette

To make the map more informative, we can add a color palette based on the population data. We create a color palette using the colorNumeric function.

pal_world <- colorNumeric(
  palette = "Reds",
  domain = world$pop,
  na.color = "#808080",
  alpha = FALSE,
  reverse = FALSE
)

Enhanced Leaflet Map

Now, we enhance the map by applying the color palette to the polygons and adding a legend.

leaflet(world) %>%
  addTiles() %>%
  addPolygons(color = ~pal_world(pop), fillOpacity = 0.5) %>%
  addLegend(pal = pal_world, title = "Population", values = ~pop, position = "bottomright")

Thematic Mapping with tmap

For a different visualization approach, we use the tmap package, which provides a more thematic mapping experience. We switch to the interactive view mode and create a map that shows both population and area.

tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(world) + tm_polygons(c('pop', 'area_km2')) + tm_facets(sync = TRUE, ncol = 2)

Conclusion

In this guide, we’ve demonstrated how to create both static and interactive maps in R using various libraries. We started with a basic plot and moved on to creating interactive maps with leaflet and thematic maps with tmap. Each of these tools offers unique features and advantages, allowing you to choose the one that best fits your needs. Mapping data is a powerful way to visualize and understand complex datasets, and R provides a rich set of tools to help you create informative and visually appealing maps.