Open In App

Making Static & Interactive Maps With ggvis

Last Updated : 25 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Static Maps with ggplot2

ggplot2 is a versatile data visualization package in R. It is primarily known for creating static plots and charts, including maps. To create static maps using ggplot2, we need to load spatial data and use the geom_sf layer to plot it.

R




# Load required packages
library(ggplot2)
library(sf)
library(maps)
 
# Load and plot world map data
world <- sf::st_as_sf(map("world", plot = FALSE, fill = TRUE))
 
ggplot() +
  geom_sf(data = world) +
  labs(title = "World Map")


Output:

gh

Making Static & Interactive Maps With ggvis

  • The globe map data is loaded using the map function from the maps package.
  • The sf::The map data is transformed into a sf object, which ggplot2 may use for spatial data, using the st_as_sf function.
  • A ggplot object is initialised by ggplot().
  • The global map data is added to the plot using the geom_sf function, and the plot’s title is changed to “World Map” using the labs function.

Fill color to the map

R




# Load required packages
library(ggplot2)
library(maps)
 
# Load and plot world map data
world <- map_data("world")
 
ggplot(data = world, aes(x = long, y = lat, group = group)) +
  geom_polygon(fill = "lightblue", color = "black") +
  coord_fixed(ratio = 1) +
  labs(title = "World Map")


Output:

gh

Making Static & Interactive Maps With ggvis

  • We create a ggplot object and specify the aesthetics with aes. We use long and lat for longitude and latitude, respectively, and group to group the data for polygons.
  • We use geom_polygon to plot the world map polygons with a light blue fill and black borders.
  • coord_fixed(ratio = 1) ensures that the aspect ratio of the map is preserved.
  • labs(title = “World Map”) sets the title of the plot to “World Map.”

Interactive Maps with leaflet

leaflet is an interactive mapping package in R. It is excellent for creating web-based maps with interactive features.
Let’s explore a couple of interactive map examples.

R




# Load required package
library(leaflet)
 
# Create an interactive map
leaflet() %>%
  addTiles() %>%
  addMarkers(
    lng = -118.2437, lat = 34.0522,
    popup = "Los Angeles, CA"
  ) %>%
  setView(lng = -118.2437, lat = 34.0522, zoom = 10)


Output:

gh-(1)

Making Static & Interactive Maps With ggvis

  • The addMarkers() function adds markers to the map. In this case, a marker is placed at the specified longitude (lng) and latitude (lat) coordinates, representing the location of Los Angeles, CA. The popup argument defines the text that appears when you click on the marker.
  • The setView() function specifies the initial view of the map. It sets the center of the map at the specified coordinates (lng and lat) and sets the initial zoom level (zoom).

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:

gh-(2)

Making Static & Interactive Maps With ggvis

  • 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).

Conclusion

Mapping data in R is a powerful way to explore geographic patterns and communicate information effectively. This article introduced us to ggplot2 for creating static maps and leaflet for interactive maps. By mastering these packages, we can create a wide range of informative and visually appealing maps to enhance our data analysis and visualization projects.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads