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

[This article was first published on r – Appsilon | End­ to­ End Data Science Solutions, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Geomaps with R and Leaflet Thumbnail

Geo maps with R and Leaflet

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

Having problems with basic R visualizations? Here’s our guide for building stunning scatter plots.

The article is structured in the following way:

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 1 – Head of Earthquakes in Japan dataset

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:

= 6)” width=”1395″ height=”135″ srcset_temp=”https://appsilon.com/wp-content/uploads/2020/12/2-5.png 1395w, https://appsilon.com/wp-content/uploads/2020/12/2-5-600×58.png 600w, https://appsilon.com/wp-content/uploads/2020/12/2-5-1024×99.png 1024w, https://appsilon.com/wp-content/uploads/2020/12/2-5-768×74.png 768w” sizes=”(max-width: 1395px) 100vw, 1395px” />

Image 2 – Head of Earthquakes in Japan dataset (magnitude >= 6)

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 we’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 3 – Geomap of Earthquakes near Japan from 2001 to 2018

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 4 – Formula for calculating marker size

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 5 – Geomap of Earthquakes near Japan from 2001 to 2018 (styled markers)

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 6 – Geomap of Earthquakes near Japan from 2001 to 2018 (styled markers v2)

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 7 – Final geomap of Earthquakes near Japan from 2001 to 2018 (with added popups)

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.

You can expect more basic R tutorials weekly. Fill out the subscribe form below, so you never miss an update.

Learn More

Appsilon is hiring for remote roles! See our Careers page for all open positions, including R Shiny DevelopersFullstack EngineersFrontend Engineers, a Senior Infrastructure Engineer, and a Community Manager. Join Appsilon and work on groundbreaking projects with the world’s most influential Fortune 500 companies.

Article How to Make Stunning Geomaps in R: A Complete Guide with Leaflet comes from Appsilon | End­ to­ End Data Science Solutions.

To leave a comment for the author, please follow the link and comment on their blog: r – Appsilon | End­ to­ End Data Science Solutions.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)