Introduction to cartographr"

knitr::opts_chunk$set(
  echo=TRUE,
  message=FALSE,
  warning=FALSE,
  collapse = TRUE,
  comment = "#>"
)

In this vignette, you will learn how to add new information to a map. Here, we retrieve a map of Manhattan, NYC.

library(cartographr)
library(dplyr)
library(ggplot2)
library(sf)

First, we load the locations of crimes in Manhattan.

data("crime") 
data("soho_boundary")

Convert longitude / latitude into a sf

To add the crime locations in the dataset to our map, we harness the sf (simple features) package, which includes a lot of useful tools for working with geo data. To this end, we convert the dataset into a sf object using latitude and longitude.

crime |> head()

This can be achieved with the function sf::st_as_sf() using the coordinates from the dataset.

Setup the map parameters

We have to provide longitude, latitude and x_distance (i.e., the width of the map in meters). Furthermore, we define the extend of the OSM data in meters in get_osmdata(). y_distance is calculated automatically using the output size aspect ratio.

set_output_size("A4", orientation = "portrait")
osm <- get_osmdata(sf = soho_boundary)

Plot the map

plot_map() generates a ggplot2 object using the color theme set as parameter. That means that we can easily adjust the plot using ggplot2 commands and also add new information to the map.

p <- osm |> 
  crop(soho_boundary) |> 
  plot_map(palette = "alphabet") +
  theme_infomap_barlow() + 

  # Add geom with crimes
  geom_point(data = crime,
             aes(x = longitude, y = latitude, shape = type), color="#A72424", size=2) + 

  # Set labels
  labs(title = "CRIME IN SOHO",
       shape = "TYPE")

Now we can plot the map by simply calling its print method implicity:

p

Save map

save_map() can be used to store the print-ready plot object to the disk as a drawn object in pdf format.

save_map(plot = p, filename = "ny_crime.pdf")
ggsave("png/plot the map-1.png", plot = p, width = 210, height= 297, units = "mm", dpi = 96)


Try the cartographr package in your browser

Any scripts or data that you put into this service are public.

cartographr documentation built on Aug. 21, 2025, 5:46 p.m.