Skip to content

Instantly share code, notes, and snippets.

@WayneAHof
Created August 29, 2024 04:42
Show Gist options
  • Save WayneAHof/5a3b332c79ffc80b198d24b3044e1a54 to your computer and use it in GitHub Desktop.
Save WayneAHof/5a3b332c79ffc80b198d24b3044e1a54 to your computer and use it in GitHub Desktop.
How to make a simple NZ Map
# Draw a NZ map and plot two points of interest
# Load libraries
library(ggplot2)
library(sf)
library(maps)
# Load New Zealand map data
nz_map <- st_as_sf(map("nz", plot = FALSE, fill = TRUE))
# Define the coordinates for the two points and their labels
points_df <- data.frame(
lon = c(171.000, 173.800), # Longitude of the points
lat = c(-44.800, -35.000), # Latitude of the points
label = c("A", "B") # Labels for the points
)
# Convert the points dataframe to an sf object
points_sf <- st_as_sf(points_df, coords = c("lon", "lat"), crs = 4326)
# Plot the map with the points and labels
ggplot() +
geom_sf(data = nz_map, fill = "lightgreen", color = "black", alpha = 0.5) +
geom_sf(data = points_sf, color = "red", size = 2) +
geom_text(
data = points_df, aes(x = lon, y = lat, label = label),
nudge_y = 0.2, color = "black", size = 4, fontface = "bold"
) +
theme_classic() +
labs(x = "Longitude", y = "Latitude")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment