R
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Data Structures
-
Section 4: Data Manipulation
-
Section 5: Visualization and Statistics
-
Section 6: Advanced R
-
Section 7: Practical Projects
-
Section 8: Interview Practice
-
Section 9: More Practice Exercises
-
Section 10: Shiny Apps in Depth
-
Section 11: More Data Wrangling
-
Section 12: Tidyverse Deep Dive
-
Section 13: Statistical Modeling Deep Dive
-
Section 14: Machine Learning in R
-
Section 15: R Visualization Deep Dive
-
Section 16: R Package Development Deep Dive
-
Section 17: R for Reproducible Research
-
Section 18: R and Databases
-
Section 19: R Performance Optimization
-
Section 20: Bioinformatics and Specialized R
-
Section 21: More Shiny Practice
-
Section 22: More Practice Exercises
-
Section 23: R Data Structures Deep Dive
-
Section 24: More Interview and Review
-
Section 25: R for Business Analytics
-
Section 26: R Text Mining and NLP
-
Section 27: R Spatial Data Analysis
-
Section 28: R Deep Learning
-
Section 29: Advanced Statistical Techniques
-
Section 30: R Object Systems Deep Dive
-
Section 31: R Environments and Metaprogramming
-
Section 32: R for Finance
-
Section 33: R for Clinical and Health Data
-
Section 34: More Shiny Advanced Practice
-
Section 35: R Data Cleaning Deep Dive
-
Section 36: R Reporting Automation
-
Section 37: More Practical Projects Round 2
-
Section 38: R Ecosystem and Career
-
Section 39: More Visualization Practice
-
Section 40: R for Experimentation
-
Section 41: R for Genomics and Bioinformatics Deep Dive
-
Section 42: R for Marketing Analytics
-
Section 43: R Data Import/Export Deep Dive
-
Section 44: R String Processing Deep Dive
-
Section 45: R for Actuarial and Insurance Analytics
-
Section 46: R Testing and Quality Assurance Deep Dive
-
Section 47: R Version Control and Collaboration
-
Section 48: R Advanced Functional Programming
-
Section 49: R for Supply Chain and Operations
-
Section 50: More Practice Exercises Round 3
-
Section 51: R Dashboards and BI Integration
-
Section 52: R Data Governance and Ethics
-
Section 53: More Modeling Practice
-
Section 54: R Final Capstone Projects
-
Section 55: R for Sports Analytics
-
Section 56: More Interview Practice Round 2
-
Section 57: R Networking and APIs
-
Section 58: R for Environmental Science
-
Section 59: R Notebook and Documentation Practices
-
Section 60: More Data Wrangling Mastery
-
Section 61: R for A/B Testing at Scale
-
Section 62: R Package Ecosystem Deep Dive
66: Geospatial Visualization with leaflet
I can't tell you how many times I've seen a developer stare at a blank grey screen or a map of the South Pacific when they were actually trying to map a neighborhood in London. It's a rite of passage with leaflet. Usually, it's not a bug in the package, but a fundamental misunderstanding of how geospatial coordinates are handled in the underlying JavaScript libraries.
The map is showing the middle of the ocean
library(leaflet)
# Let's say we have some coffee shop data for Seattle
shops <- data.frame(
name = c("Cafe A", "Cafe B"),
lat = c(47.6062, 47.6150),
lon = c(-122.3321, -122.3410)
)
leaflet(shops) %>%
addTiles() %>%
addMarkers(lng = ~lat, lat = ~lon) # Wait, this looks right... right?
If you run the code above, you'll notice the markers aren't in Seattle. In fact, they're probably nowhere near Washington state. The code runs without an error message, which is the most frustrating part. You're just looking at the wrong part of the world.
Swap your X and Y
The fix is simple, but the reason is what matters: leaflet follows the GeoJSON standard, which treats coordinates as [longitude, latitude]. Think of it like a standard Cartesian plane: Longitude is your X-axis (east-west) and Latitude is your Y-axis (north-south). In the broken code above, I passed the latitude to the lng argument and the longitude to the lat argument.
leaflet(shops) %>%
addTiles() %>%
addMarkers(lng = ~lon, lat = ~lat) # Correct: Longitude first, then Latitude
Once you align these, your markers will snap back to where they belong. Always double-check your column mapping; it's the single most common mistake I see when people start with geospatial R.
Changing the map's aesthetic
By default, addTiles() pulls from OpenStreetMap. It's great for general use, but depending on what you're visualizing, it might be too cluttered. I often prefer the "CartoDB" tiles because they provide a cleaner, more muted background that makes your data points actually pop.
You can swap the default provider by passing a URL to addTiles() or using the providers argument if you have the leafletProviders package installed. For a quick, clean look without extra packages, try this:
leaflet(shops) %>%
addProviderTiles(providers = "CartoDB.Positron") %>%
addMarkers(lng = ~lon, lat = ~lat)
Managing marker clutter with clustering
If you're mapping five coffee shops, addMarkers() is fine. If you're mapping five thousand, your browser will likely hang, and your map will just be a solid blob of blue pins. This is where addMarkerCluster() becomes your best friend.
Instead of drawing every individual point, clustering groups nearby markers into a single circle with a number inside. As you zoom in, the clusters break apart into individual pins. It's much more performant and significantly more readable.
leaflet(shops) %>%
addProviderTiles(providers = "CartoDB.Positron") %>%
addMarkerCluster(lng = ~lon, lat = ~lat, popup = ~name)
I added the popup argument here too. In a real-world project, a pin without a label is useless. By passing a column from your data frame to popup, you give the user a way to actually identify what they're looking at.
📋 Practical Task
Visualizing NYC Bike Stations with Clustering
You have been handed a dataset of New York City Citi Bike stations. Your goal is to create an interactive map that doesn't overwhelm the user with thousands of overlapping pins.
Requirements:
- Create a data frame called
bike_stationswith at least 5 sets of coordinates (Latitude and Longitude) located within NYC (approx. Lat: 40.7, Lon: -74.0). - Use the
CartoDB.DarkMattertile provider to give the map a "dark mode" aesthetic. - Implement
addMarkerClusterinstead of standard markers to handle the station density. - Ensure each marker has a popup that displays the station's name (you'll need to add a
station_namecolumn to your data frame). - Verify that your Longitude and Latitude are mapped to the correct
leafletarguments to avoid the "ocean bug".
There are no comments for now.