top of page
Writer's pictureBernard Kilonzo

Creating Interactive Maps in R: A Step-by-Step Guide

line of code on a laptop

Overview

Interactive maps are digital tools that allow users to engage actively with geographical data. Unlike static maps, which provide a fixed view of information, interactive maps enable users to manipulate the map by zooming in and out, panning across different areas, and clicking on specific features for additional information. This interactivity enhances the user experience by making exploration more immersive and informative.

Step-by-Step Guide

To create an interactive map in R, first you need to load all the required packages in your R session as shown below.

loading packages in r session

Set your working directory (the default folder that the R environment uses for reading and saving files.)

setting working directory in r

Load the datasets, in this example I would like to plot the sample size by commune. In that regard, I will need to load two datasets namely.

  • Survey sample data which contains the sample sizes by commune.

  • Haiti commune boundaries shapefile data.

connecting datasets in r

Here is the preview of the two datasets.

Survey Sample Data

sample data

Haiti Commune Shapefile Data

sample data

Next, join the two datasets as shown below.

joining data in r

Reviewing the merged data shows that the sample data has been merged with the shapefile data as shown below.

sample data

Using now merged data; you can create a simple spatial map showing the sample size by commune using the following code.

creating a basic map in r

Running the above code generates the view below.

a map created in r

Let’s customize the map, by adding a white boundary and applying gold-brown color on the legend. Add theme_void() to remove background elements.

See the code below.

customizing maps in r

Running the above code generates the view below.

example of customized map in r

You can convert the above map into an interactive map using the ‘ggplotly’ function from the ‘plotly’ package as shown below.

converting static maps to interactive ones in r

Running the above code generates the interactive map below.

example of an interactive map

Note, you can also render interactive map using the ‘girafe’ function from the ‘ggiraph’ package.

girafe(ggobj = mp)

Conclusion

The integration of interactivity into maps generated with ‘ggplot2’ significantly enhances the analytical capabilities of spatial data visualization. By leveraging packages like ‘ggiraph’ and ‘plotly’, users can create interactive maps that not only present data effectively but also allow for user engagement through features such as tooltips, zooming, and clickable elements. This interactivity is particularly beneficial in fields such as urban planning, environmental studies, and public health, where understanding geographical context is crucial.

If you like the work we do and would like to work with us, drop us an email on our contacts page and we’ll reach out!

Thank you for reading!

bottom of page