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.
Set your working directory (the default folder that the R environment uses for reading and saving files.)
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.
Here is the preview of the two datasets.
Survey Sample Data
Haiti Commune Shapefile Data
Next, join the two datasets as shown below.
Helpful: Combine data in R (merge data)
Reviewing the merged data shows that the sample data has been merged with the shapefile data as shown below.
Using now merged data; you can create a simple spatial map showing the sample size by commune using the following code.
Running the above code generates the view below.
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.
Running the above code generates the view below.
You can convert the above map into an interactive map using the ‘ggplotly’ function from the ‘plotly’ package as shown below.
Running the above code generates the interactive map below.
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!