The world’s leading publication for data science, AI, and ML professionals.

How to Make Stunning Geomaps in R: A Complete Guide with Leaflet

A complete guide to GeoMaps with R and Leaflet.

Photo by Annie Spratt on Unsplash
Photo by Annie Spratt on Unsplash

Data Visualization can be tough to get right, and geospatial data is not an exception. Today you’ll learn how to build aesthetically pleasing interactive maps with R and leaflet.

The article is structured in the following way:

  • Dataset loading and preparation
  • Create your first map
  • Tweak marker size
  • Tweak marker aesthetics
  • Add popups
  • Conclusion

Dataset loading and preparation

One type of natural disaster is perfect for geospatial data visualization – earthquakes. It’s easy to compare magnitudes through marker sizes and geolocations through marker positions. That’s what you’ll do today.

The Earthquakes in Japan dataset provides an overview of all earthquakes in Japan between 2001 and 2018. Here’s how the first couple of rows look like, once loaded with R:

Image 1 - Head of Earthquakes in Japan dataset (image by author)
Image 1 – Head of Earthquakes in Japan dataset (image by author)

There are over 14,000 data points in the dataset. Visualizing all of them would be a nightmare (many overlapping points), so let’s filter out some of them.

The below snippet loads in all of the required libraries, declares a couple of variables for visualizations, and performs data filtering – only earthquakes of magnitude 6 and above are kept:

Here’s how the filtered dataset looks like:

Image 2 - Head of Earthquakes in Japan dataset (magnitude >= 6) (image by author)
Image 2 – Head of Earthquakes in Japan dataset (magnitude >= 6) (image by author)

And that’s all you need to create your first map. Let’s do that next.


Create your first map

You’ll use the Leaflet package for the job. Your first map will use japan_lat and japan_lon variables to set the geolocation and will draw marker points as large as the magnitude was.

There’s a lot you can do to make the map aesthetically pleasing. Setting the tiles is the first step, and I’ve found Esri.WorldStreetMap looks best at this location. You can refer to this website for all of the available options.

Below is a code snippet you can use to draw your first, basic map:

Here’s how it looks like:

Image 3 - Geomap of Earthquakes near Japan from 2001 to 2018 (image by author)
Image 3 – Geomap of Earthquakes near Japan from 2001 to 2018 (image by author)

The leaflet package has no idea it’s displaying earthquakes. As a result, magnitudes of 6 and 9 look nearly identical, even though the second one is 1000 times stronger. Let’s fix that next.


Tweak marker size

You can use the following completely non-scientific formula to calculate marker size:

Image 4 - Formula for calculating marker size (image by author)
Image 4 – Formula for calculating marker size (image by author)

The x here represents the magnitude, and c represents a constant you can play around with. The larger it is, the easier it is to spot stronger earthquakes. Just don’t go too crazy with it.

You can implement this formula in the radius parameter. Here’s how:

As you can see, the value for c is set to 2. Here’s how the map looks now:

Image 5 - Geomap of Earthquakes near Japan from 2001 to 2018 (styled markers) (image by author)
Image 5 – Geomap of Earthquakes near Japan from 2001 to 2018 (styled markers) (image by author)

Now we’re getting somewhere. The default outline and fill colors look a bit dull, so let’s change them next.

Tweak Marker Aesthetics

The three parameters are crucial to making your maps more aesthetically pleasing – color, fill color, and fill opacity. You can tweak all of them inside the addCircles() function. Here’s how:

This code snippet makes the markers red, and makes their fill color a bit more transparent than before:

Image 6 - Geomap of Earthquakes near Japan from 2001 to 2018 (styled markers v2) (image by author)
Image 6 – Geomap of Earthquakes near Japan from 2001 to 2018 (styled markers v2) (image by author)

At this point, your maps don’t tell the full picture. Yes, you can eyeball a couple of strongest earthquakes by comparing marker sizes, but is that really a way to go?

Popups say no, and you’ll learn how to add them next.


Add popups

Popups provide a neat and clean way of displaying more information whenever you click on a marker of interest. You’ll use them to add information on the time of the earthquake, it’s magnitude, depth, and place.

You can use the paste0 function to add the data. If you want something styled, you can use various HTML tags. For example, the <strong> tag will make a portion of the text bold, and <br> makes a line break:

Here’s the corresponding output:

Image 7 - Final geomap of Earthquakes near Japan from 2001 to 2018 (with added popups) (image by author)
Image 7 – Final geomap of Earthquakes near Japan from 2001 to 2018 (with added popups) (image by author)

And that’s just enough to get you started. Let’s wrap things up next.

Conclusion

Visualizing geospatial data is easy with R. If your data is in the right format, a couple of lines of code will be enough.

Today you’ve learned how to make basic geospatial data visualizations, how to tweak markers and other aesthetics, and how to add popups. This alone will be enough for most of the visualizations.

Join my private email list for more helpful insights.


Loved the article? Become a Medium member to continue learning without limits. I’ll receive a portion of your membership fee if you use the following link, with no extra cost to you.

Join Medium with my referral link – Dario Radečić


Originally published at https://appsilon.com on December 31, 2020.


Related Articles