Interactive Choropleth Map
R
# Load required package library (leaflet) # Sample data cities <- data.frame ( City = c ( "New York" , "Los Angeles" , "Chicago" , "Houston" , "Phoenix" ), Population = c (8398748, 3990456, 2705994, 2320268, 1680992) ) # Create an interactive choropleth map leaflet (data = cities) %>% addTiles () %>% addCircleMarkers ( lng = -118.2437, lat = 34.0522, radius = ~ sqrt (Population) / 1000, color = "red" , popup = ~City ) %>% setView (lng = -98.5795, lat = 39.8283, zoom = 4) |
Output:
- We use the addCircleMarkers() function to add circle markers to the map. These markers represent cities. The lng and lat arguments specify the coordinates for placing the markers. The radius argument scales the size of the markers based on the square root of population values divided by 1000. The color argument sets the marker color to red. The popup argument specifies the city name to be displayed when clicking on a marker.
- The setView() function defines the initial view of the map, with the center located at the specified coordinates (lng and lat) and an initial zoom level (zoom).
Making Static & Interactive Maps With ggvis
In R Programming Language data visualization, making static and interactive maps is a common activity. Although the R package ggvis allows for interactive data visualizations, map visualizations are not one of its natural capabilities. For static maps and interactive maps, respectively, we commonly utilize tools like ggplot2 and leaflet.
In this article, we will check how to use ggplot2 and leaflet in R to generate both static and interactive maps.