geom_sf and ggplot: polygons disappear in some projections

40 Views Asked by At

Some polygons/countries disappear when I use orthographic projections with ggplot. I can make either Russia or the US vanish!

library(tmap)
library(tidyverse)
library(sf)
data("World")
World %>% st_transform("+proj=robin") %>% ggplot() + 
   geom_sf(aes(fill=life_exp)) ## looks fine
World %>% st_transform("+proj=ortho") %>% ggplot() + 
   geom_sf(aes(fill=life_exp)) ## Russia is gone!
World %>% st_transform("+proj=ortho +lat_0=40") %>% ggplot() + 
   geom_sf(aes(fill=life_exp)) # Russia is back but US is gone!

This is very strange!

1

There are 1 best solutions below

0
Mark R On

There's a hack here using mapview: https://gist.github.com/rafapereirabr/26965dd851debad32ad2e659024ba451

library(lwgeom)
library(mapview)
# Fix polygons so they don't get cut in ortho projection
World  <- st_cast(World, 'MULTILINESTRING') %>%
  st_cast('LINESTRING', do_split=TRUE) %>%
  mutate(npts = npts(geometry, by_feature = TRUE)) %>%
  st_cast('POLYGON')

World %>% st_transform("+proj=ortho +lat_0=40") %>% ggplot() + 
   geom_sf(aes(fill=life_exp)) # Russia is back but US is gone!