ropensci / rnaturalearth

An R package to hold and facilitate interaction with natural earth map data :earth_africa:
http://ropensci.github.io/rnaturalearth/
Other
217 stars 23 forks source link

Chatham Islands with New Zealand #95

Closed fvaux closed 11 months ago

fvaux commented 11 months ago

Hello, I'm learning to use rnaturalearth, and I want to be able to plot New Zealand with the Chatham Islands and Subantarctic Islands in their normal positions.

However, when I run: sp::plot(ne_states(country = "new zealand")) The Chatham Islands are on the far left.

Then, when I try to use the coord_sf function to change the zoom on my plot, I'm unsure how to include the Chatham Islands.

Is there an easy solution?

#Load library packages
library(ggplot2)
library(dplyr)
library(rnaturalearth)
library(rnaturalearthdata)
library(rnaturalearthhires)

# Read the CSV file (generated from https://www.inaturalist.org/observations/export )
data <- read.csv("observations-368324.csv") #Haliotis iris iNaturalist data

# Load the New Zealand map data
world <- ne_countries(scale = "large", country = "New Zealand", returnclass = "sf")

# Filter the map to include only New Zealand
new_zealand <- filter(world, name == "New Zealand")

# Create a basic map of New Zealand with New Zealand filled in as grey
nz_map <- ggplot(data = data) +
  geom_sf(data = new_zealand, fill = "grey", color = NA) +
  geom_point(aes(x = longitude, y = latitude), size = 1, color = "blue") +
  #coord_sf(xlim = c(166.0, 179.0), ylim = c(-48.0, -34.0), expand = FALSE) + #Zoomed to New Zealand
  #coord_sf(xlim = c(166.0, 184.0), ylim = c(-48.0, -34.0), expand = FALSE) + #Zoomed to New Zealand with Chatham Islands
  coord_sf(xlim = c(0.0, 184.0), ylim = c(-52.0, -34.0), expand = FALSE) + #Zoomed to New Zealand with Subantarctic Islands
  theme_void()

# Save the map as a PDF
ggsave("nz_map_with_data.pdf", plot = nz_map)
PMassicotte commented 11 months ago

Could you reproject your data?

new_zealand |>
  st_transform("EPSG:3851") |>
  ggplot() +
  geom_sf()
fvaux commented 11 months ago

Thanks, I think that must be the solution. Would you transform the latitude and longitude data for EPSG:3851 to make that plotting work?

PMassicotte commented 11 months ago

Yes. Another option would be to do:

chatam <- new_zealand |>
  st_cast("POLYGON") |>
  slice(5:9)

st_geometry(chatam) <- st_geometry(chatam) + c(360, 0)

st_crs(chatam) <- "EPSG:4326"

But it hacking hard :)

fvaux commented 11 months ago

Great, thank you! Will try both solutions.