I am trying to create a plot where instead of using points to represent data, I use the outline of administrative boundaries.
The below code illustrates where I am up to. Just a simple ordered scatter plot. I am now stuck, trying to replace the points with the polygon shapes for each administrative boundary (nz_map). Because the polygons in nz_map have their own geometries associated with them, I'm not sure how to position them at the locations of the points in plot space.
Any advice would be much appreciated.
# load packages
library(tidyverse)
library(sf)
library(rnaturalearth)
# map of nz with administrative boundary
nz_map <- ne_states(country = "new zealand", returnclass = "sf")
# get regions of interest and calculate area
nz_map <- nz_map %>%
select(name, region) %>%
filter(region %in% c("South Island", "North Island")) %>%
mutate(area = as.vector(st_area(.)/1e6))
# convert to points to illustrate plotting
nz_map_pnts <- st_centroid(nz_map)
# plot
ggplot() +
geom_point(data = nz_map_pnts, aes(reorder(name, area), y = area)) +
labs(x = "Region",
y = "Area") +
theme_classic()+
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))


One option would be to use
lapplyandannotation_customto add a map for each region. To this end I first create a blank plot in cartesian coordinates where I map thenames onxand theareas on y. Then use e.g.lapplyto loop over the regions to add a map for the region at your desired coordinates: