How to Make Stunning Geomaps in R: A Complete Guide with Leaflet
This article is originally published at https://appsilon.com
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
- 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:
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:
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:
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:
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:
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:
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:
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
- How to Make Stunning Bar Charts in R: A Complete Guide with ggplot2
- How to Make Stunning Bar Charts in R: A Complete Guide with ggplot2
- How to Make Stunning Bar Charts in R: A Complete Guide with ggplot2
- What Can I Do With R? 6 Essential R Packages for Programmers
Appsilon is hiring for remote roles! See our Careers page for all open positions, including R Shiny Developers, Fullstack Engineers, Frontend 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.
Thanks for visiting r-craft.org
This article is originally published at https://appsilon.com
Please visit source website for post related comments.