Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with SpatVector wrapping when plotting

Tags:

r

ggplot2

terra

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

like image 935
Henry Holm Avatar asked Jun 25 '26 22:06

Henry Holm


1 Answers

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()
like image 106
L Tyrone Avatar answered Jun 27 '26 17:06

L Tyrone



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!