3

When plotting a SpatVector using ggplot2, if I define a projection using coord_sf(), it adds these lines that I think come from the data wrapping around the plot extent. Best way to fix?

library(ggplot2)
library(tidyterra)
library(terra)
library(rnaturalearth)

coast <- rnaturalearth::ne_coastline(returnclass = "sv")

ggplot() +
  geom_spatvector(data = coast) +
  theme_minimal()

ggplot() +
  geom_spatvector(data = coast) +
  coord_sf(crs = "+proj=robin",) +  # Robinson projection
  theme_minimal()

Image has wrapping issue once Robinson projection is added

Without projection

1

2 Answers 2

5

Use the terra::normalize.longitude() function to correct this:

library(ggplot2)
library(tidyterra)
library(terra)
library(rnaturalearth)

coast <- ne_coastline(returnclass = "sv")
coast <- normalize.longitude(coast)

ggplot() +
  geom_spatvector(data = coast) +
  coord_sf(crs = "+proj=robin") +
  theme_minimal()
Sign up to request clarification or add additional context in comments.

1 Comment

For anyone wanting to dig a little deeper into where/why this occurs for this dataset, coast <- rnaturalearth::ne_coastline() |> dplyr::slice(94) terminates at 180 lon 68.96365 lat, and that point is the source of the error.
5

Another work-around is to crop the input data such that are no points at -180 or 180 degrees longitude

library(terra)
coast <- rnaturalearth::ne_coastline(returnclass = "sv")

x <- crop(coast, as.polygons(ext(-180,180,-90,90) - .001))
r <- project(x, "+proj=robin")
plot(r)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.