Interactive World Map

Interactive Map of the World This document displays an interactive map of the world using the tmap and leaflet R packages. You can zoom, pan, and click on countries to see more information.

Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.

Running Code

When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

# Load the necessary libraries.
# If you don't have them installed, run:
# install.packages(c("tmap", "leaflet"))
library(tmap)
library(leaflet)

# Set tmap to "view" mode, which uses leaflet for interactivity.
# This is crucial for creating an interactive map in a static document.
tmap_mode("view")
ℹ tmap mode set to "view".
# Load the built-in "World" spatial dataset from the tmap package.
data("World", package = "tmap")

# Create the tmap object.
# We use tm_shape() to define the spatial data, and then add layers.
# tm_polygons() creates colored polygons based on the 'continent' variable,
# and we add popups for GDP per capita.
# tm_borders() adds borders to the polygons for clarity.
# tm_basemap() adds a topographic background map for context and navigation.
tmap_object <- 
  tm_shape(World) + 
  tm_polygons("continent", id = "name",
              popup.vars = c("GDP per capita" = "gdp_cap_est")) + 
  tm_borders(col = "white") +
  tm_basemap(server = "Esri.WorldTopoMap")

# The last object in the code chunk is automatically rendered.
tmap_object

You can add options to executable code like this

[1] 4

The echo: false option disables the printing of code (only output is displayed).