In this exercise, we are going to re-use the results from the previous one. If you don’t have them available, fire up the code below.
# It depends on the map. Often compasses are placed where they don't cover any
# other import elements of the map. If you're unsure, it might be a good idea
# to place it outside of the map frame.
Look at this code to create compass coordinates (for an arrow):
gesis_cologne_compass_data <-
(gesis_bbox + c(?, ?, ?, ?)) %>%
sf::st_as_sfc() %>%
sf::st_sf()
gesis_cologne_compass_data <-
(gesis_bbox + c(2200, 1000, 200, 0)) %>%
sf::st_as_sfc() %>%
sf::st_sf()
Admittedly, drawing custom elements is a matter of fiddling around. We want to draw an arrow, and we didn’t have done this before. Here’s a way to use our compass data and add it to an existing ggplot. Please feel free to adapt it to your liking.
geom_segment(
data =
gesis_cologne_compass_data %>%
rotate_data() %>%
sf::st_bbox() %>%
{data.frame(xmin = .$xmin, xmax = .$xmax, ymin = .$ymin, ymax = .$ymax)},
aes(x = xmin, y = ymin, xend = xmax, yend = ymax),
size = 1.2,
arrow = arrow(length = unit(0.2, "cm"), type = "closed")
)
geom_segment(). Do your offset values from exercise “2” look nice? Adapt them, if not.
ggsn::blank() function to remove all the clutter. Also, you don’t need the rotate_data() function for the unrotated map, right?
# unrotated
ggplot() +
geom_sf(data = gesis_roads) +
geom_segment(
data =
gesis_cologne_compass_data %>%
sf::st_bbox() %>%
{data.frame(xmin = .$xmin, xmax = .$xmax, ymin = .$ymin, ymax = .$ymax)},
aes(x = xmin, y = ymin, xend = xmax, yend = ymax),
size = 1.2,
arrow = arrow(length = unit(0.2, "cm"), type = "closed")
) +
ggsn::blank()
# rotated
ggplot() +
geom_sf(data = rotate_data(gesis_roads)) +
geom_segment(
data =
gesis_cologne_compass_data %>%
rotate_data() %>%
sf::st_bbox() %>%
{data.frame(xmin = .$xmin, xmax = .$xmax, ymin = .$ymin, ymax = .$ymax)},
aes(x = xmin, y = ymin, xend = xmax, yend = ymax),
size = 1.2,
arrow = arrow(length = unit(0.2, "cm"), type = "closed")
) +
ggsn::blank()