In this vignette, you will learn how to add new information to a map. Here, we retrieve a map of Manhattan, NYC.
First, we load the locations of crimes in Manhattan.
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.
This can be achieved with the function sf::st_as_sf()
using the coordinates from the dataset.
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.
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: