[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.

## 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:

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

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)

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

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

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

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)

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)

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.

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 tag will make a portion of the text bold, and
makes a line break:

Here’s the corresponding output:

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.