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

By:
Dario Radečić
December 31, 2020

<h2>Geomaps with R and Leaflet</h2> <span data-preserver-spaces="true">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 <code>leaflet</code>. </span> <blockquote><span data-preserver-spaces="true">Having problems with basic R visualizations? </span><a class="editor-rtfLink" href="https://wordpress.appsilon.com/ggplot-scatter-plots/" target="_blank" rel="noopener noreferrer"><span data-preserver-spaces="true">Here's our guide for building stunning scatter plots</span></a><span data-preserver-spaces="true">.</span></blockquote> <span data-preserver-spaces="true">The article is structured in the following way:</span> <ul><li><a href="#dataset">Dataset Loading and Preparation</a></li><li><a href="#first-map">Create Your First Geomap</a></li><li><a href="#marker-size">Tweak Marker Size</a></li><li><a href="#marker-aesthetics">Tweak Marker Aesthetics</a></li><li><a href="#popups">Add Popups</a></li><li><a href="#conclusion">Conclusion</a></li></ul> <h2 id="dataset"><span data-preserver-spaces="true">Dataset Loading and Preparation</span></h2> <span data-preserver-spaces="true">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.</span> <span data-preserver-spaces="true">The </span><a class="editor-rtfLink" href="https://www.kaggle.com/aerodinamicc/earthquakes-in-japan" target="_blank" rel="noopener noreferrer"><span data-preserver-spaces="true">Earthquakes in Japan</span></a><span data-preserver-spaces="true"> 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:</span> <img class="size-full wp-image-6340" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b395db6266fae6ace4e6de_1-6.webp" alt="Image 1 - Head of Earthquakes in Japan dataset" width="1398" height="139" /> Image 1 - Head of Earthquakes in Japan dataset <span data-preserver-spaces="true">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:</span> <script src="https://gist.github.com/darioappsilon/ca580bdecb6542fb982f7ec8ca0a6a58.js"></script> <span data-preserver-spaces="true">Here's how the filtered dataset looks like:</span> <img class="size-full wp-image-6341" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b395862912d32f5c05c4cc_2-5.webp" alt="Image 2 - Head of Earthquakes in Japan dataset (magnitude &gt;= 6)" width="1395" height="135" /> Image 2 - Head of Earthquakes in Japan dataset (magnitude &gt;= 6) <span data-preserver-spaces="true">And that's all you need to create your first map. Let's do that next.</span> <h2 id="first-map"><span data-preserver-spaces="true">Create Your First Geomap</span></h2> <span data-preserver-spaces="true">You'll use the <code>leaflet</code> package for the job. Your first map will use <code>japan_lat</code> and <code>japan_lon</code> variables to set the geolocation and will draw marker points as large as the magnitude was.</span> <span data-preserver-spaces="true">There's a lot you can do to make the map aesthetically pleasing. Setting the tiles is the first step, and we've found <code>Esri.WorldStreetMap</code> looks best at this location. You can refer to </span><a class="editor-rtfLink" href="http://leaflet-extras.github.io/leaflet-providers/preview/" target="_blank" rel="noopener noreferrer"><span data-preserver-spaces="true">the leaflet extras page</span></a><span data-preserver-spaces="true"> for all of the available options.</span> <span data-preserver-spaces="true">Below is a code snippet you can use to draw your first, basic map:</span> <script src="https://gist.github.com/darioappsilon/67c356b0d8478c58e90d1d27c0186feb.js"></script> <span data-preserver-spaces="true">Here's how it looks like:</span> <img class="size-full wp-image-6343" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b0211a487b2c34e797d539_Webp.net-compress-image.webp" alt="Image 3 - Geomap of Earthquakes near Japan from 2001 to 2018 " width="1579" height="1012" /> Image 3 - Geomap of Earthquakes near Japan from 2001 to 2018 <span data-preserver-spaces="true">The <code>leaflet</code> 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.</span> <h2 id="marker-size"><span data-preserver-spaces="true">Tweak Marker Size</span></h2> <span data-preserver-spaces="true">You can use the following completely non-scientific formula to calculate marker size:</span> <img class="size-full wp-image-6344" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b0207ee939cce3819613ad_4-5.webp" alt="Image 4 - Formula for calculating marker size" width="185" height="70" /> Image 4 - Formula for calculating marker size <span data-preserver-spaces="true">The </span><strong><span data-preserver-spaces="true">x</span></strong><span data-preserver-spaces="true"> here represents the magnitude, and </span><strong><span data-preserver-spaces="true">c</span></strong><span data-preserver-spaces="true"> 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.</span> <span data-preserver-spaces="true">You can implement this formula in the <code>radius</code> parameter. Here's how:</span> <script src="https://gist.github.com/darioappsilon/05a851aa414680f33b3411d7057b578c.js"></script> <span data-preserver-spaces="true">As you can see, the value for </span><strong><span data-preserver-spaces="true">c</span></strong><span data-preserver-spaces="true"> is set to 2. Here's how the map looks now:</span> <img class="size-full wp-image-6345" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01d0b431bfa246f514713_5.webp" alt="Image 5 - Geomap of Earthquakes near Japan from 2001 to 2018 (styled markers)" width="1579" height="1012" /> Image 5 - Geomap of Earthquakes near Japan from 2001 to 2018 (styled markers) <span data-preserver-spaces="true">Now we're getting somewhere. The default outline and fill colors look a bit dull, so let's change them next.</span> <h2 id="marker-aesthetics"><span data-preserver-spaces="true">Tweak Marker Aesthetics</span></h2> <span data-preserver-spaces="true">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 <code>addCircles()</code> function. Here's how:</span> <script src="https://gist.github.com/darioappsilon/db0b9e1591903230908d5c196f99e281.js"></script> <span data-preserver-spaces="true">This code snippet makes the markers red, and makes their fill color a bit more transparent than before:</span> <img class="size-full wp-image-6346" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01d0ba862659a10c2fbc8_6.webp" alt="Image 6 - Geomap of Earthquakes near Japan from 2001 to 2018 (styled markers v2)" width="1579" height="1012" /> Image 6 - Geomap of Earthquakes near Japan from 2001 to 2018 (styled markers v2) <span data-preserver-spaces="true">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?</span> <span data-preserver-spaces="true">Popups say no, and you'll learn how to add them next.</span> <h2 id="popups"><span data-preserver-spaces="true">Add Popups</span></h2> <span data-preserver-spaces="true">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, its magnitude, depth, and place.</span> <span data-preserver-spaces="true">You can use the <code>paste0</code> function to add the data. If you want something styled, you can use various HTML tags. For example, the <code>&lt;strong&gt;</code> tag will make a portion of the text bold, and <code>&lt;br&gt;</code> makes a line break:</span> <script src="https://gist.github.com/darioappsilon/1c32576f5f47f0c892b8445ddc6cb961.js"></script> <span data-preserver-spaces="true">Here's the corresponding output:</span> <img class="size-full wp-image-6347" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b0211f8c1238fe205b4c05_Webp.net-compress-sfasf.webp" alt="Image 7 - Final geomap of Earthquakes near Japan from 2001 to 2018 (with added popups)" width="1591" height="1012" /> Image 7 - Final Geomap of Earthquakes near Japan from 2001 to 2018 (with added popups) <span data-preserver-spaces="true">And that's just enough to get you started. Let's wrap things up next.</span> <h2 id="conclusion"><span data-preserver-spaces="true">Conclusion</span></h2> <span data-preserver-spaces="true">Visualizing geospatial data is easy with R. If your data is in the right format, a couple of lines of code will be enough. </span> <span data-preserver-spaces="true">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.</span> <span data-preserver-spaces="true">You can expect more basic R tutorials weekly. Fill out the subscribe form below, so you never miss an update.</span> <h2><span data-preserver-spaces="true">Learn More</span></h2><ul><li><a class="editor-rtfLink" href="https://wordpress.appsilon.com/ggplot2-bar-charts/" target="_blank" rel="noopener noreferrer"><span data-preserver-spaces="true">How to Make Stunning Bar Charts in R: A Complete Guide with ggplot2</span></a></li><li><a class="editor-rtfLink" href="https://wordpress.appsilon.com/ggplot2-line-charts/" target="_blank" rel="noopener noreferrer"><span data-preserver-spaces="true">How to Make Stunning Line Charts in R: A Complete Guide with ggplot2</span></a></li><li><a class="editor-rtfLink" href="https://wordpress.appsilon.com/ggplot-scatter-plots/" target="_blank" rel="noopener noreferrer"><span data-preserver-spaces="true">How to Make Stunning Histograms in R: A Complete Guide with ggplot2</span></a></li><li><a class="editor-rtfLink" href="https://wordpress.appsilon.com/r-for-programmers/" target="_blank" rel="noopener noreferrer"><span data-preserver-spaces="true">What Can I Do With R? 6 Essential R Packages for Programmers</span></a></li></ul> <a href="https://appsilon.com/careers/" target="_blank" rel="noopener noreferrer"><img class="size-large wp-image-5940 aligncenter" src="https://wordpress.appsilon.com/wp-content/uploads/2020/11/appsilon.hiring0-1024x576.jpg" alt="" width="1024" height="576" /></a> <p style="text-align: center;"><strong><span data-preserver-spaces="true">Appsilon is hiring for remote roles! See our </span></strong><a class="editor-rtfLink" href="https://wordpress.appsilon.com/careers/" target="_blank" rel="noopener noreferrer"><strong><span data-preserver-spaces="true">Careers</span></strong></a><strong><span data-preserver-spaces="true"> page for all open positions, including </span></strong><a class="editor-rtfLink" href="https://wordpress.appsilon.com/careers/#r-shiny-developer" target="_blank" rel="noopener noreferrer"><strong><span data-preserver-spaces="true">R Shiny Developers</span></strong></a><strong><span data-preserver-spaces="true">. Join Appsilon and work on groundbreaking projects with the world's most influential Fortune 500 companies.</span></strong></p>

Have questions or insights?

Engage with experts, share ideas and take your data journey to the next level!

Is Your Software GxP Compliant?

Download a checklist designed for clinical managers in data departments to make sure that software meets requirements for FDA and EMA submissions.
Explore Possibilities

Share Your Data Goals with Us

From advanced analytics to platform development and pharma consulting, we craft solutions tailored to your needs.

Talk to our Experts
r
data visualization
leaflet
tutorials