I have the following shapefiles:
qmasp <- st_read("Rock_Lobster_(Spiny_Red)_QMAs.shp", quiet = TRUE)
statShp <- st_read("Rock_Lobster_Statistical_Areas.shp", quiet = TRUE)
NE_countries <- st_read("ne_110m_admin_0_countries", quiet = TRUE)
I want them to appear together as a single map. Each shapefile looks like this (see images):
and I am using the following code:
transform QMA shapefile to an sf object
qmasf=st_as_sf(qmasp)
qmasf4326=st_transform(qmasf, 4326)
then NZ UTM for plotting
qmasf2134=st_transform(qmasf, 2193)#2134
qmasf2134 %>%
ggplot()+
geom_sf(aes(fill=FishstockC))
transform Stat Area Shapefile to sf object
statsf=st_as_sf(statShp)
statsf4326=st_transform(statsf, 4326)
then NZ UTM for plotting
statsf2134=st_transform(statsf, 2134)#check if NZ2000 is better
stats<-statsf2134 %>%
ggplot()+
geom_sf(fill= 'White')+
geom_sf_text(
aes(label = Statistica),
size = 4 / .pt
)
stats
Plot Map
theme_set(theme_bw())
newzealand <- ne_countries(country="new zealand", type="countries", scale = 'large', returnclass = "sf")
ggplot(data = newzealand) +
geom_sf() +
coord_sf(crs = 2193)
I can then plot data points using coordinate data onto specific polygons from the QMA shapefile on the map with this code:
to prepare data for plotting on map
samples_sf <- sf::st_as_sf(d2, coords=c("Longitude","Latitude"), crs=4326)
Subset CRA3 polygon from the QMA shapefile
CRA3_shapefile<-qmasf2134 %>%
filter(FishstockC %in% c("CRA2","CRA3"))
CRA3_shapefile
which looks like this (see below):
or this depending on which code I use:

My questions are, (i) How do I produce a map that has all three shape files on the one image, essentially layered on top of one another? (ii) Why is my map of New Zealand so small and how do I make it the same size as the other shapefiles, without losing Chatham Island (the third island of New Zealand)? This island is to the East of the north and south island of New Zealand.
Any help will be greatly appreciated!




Tēnā koe e hoa, here's a repex given we don't have access to your data. You can think of
coord_sf()as a zoom function. To get the extent of your data, usest_bbox()and work forwards from there. With some trial and error, you'll achieve the desired result. The example image uses the xlim/ylim (long/lat) as stated. For each layer, add a newgeom_sf()as shown below. You have to explicitly state thedata =each time, but I prefer this approach as I find it easier to follow. Thegeom_sf()are plotted in order, so your background layers go first:Update based on OP's comment
Often it is more desirable to filter out the unnecessary geometries rather than using
coord_sf(). It's trickier in this case as your NZ data are MULTIPOLYGON so every individual polygon will share the same non-geometry values (attributes in ArcGIS parlance). In other words, there's no way of knowing which polygons represent Avaiki Nui/Cook Islands other than by their long/lat values.In your case, the simplest approach is to 'borrow' the max ylim value from the previous step e.g 6204853 and to filter out everything north of 6204853. This approach involves breaking your MULTIPOLYGON into individual polygons using
st_cast():