Skip to content

Instantly share code, notes, and snippets.

@rafapereirabr
Created October 7, 2019 17:25
Show Gist options
  • Select an option

  • Save rafapereirabr/6a58d3ded83b4b7ad3a0f360565d05c6 to your computer and use it in GitHub Desktop.

Select an option

Save rafapereirabr/6a58d3ded83b4b7ad3a0f360565d05c6 to your computer and use it in GitHub Desktop.

Simple example of how to create a world map using full hemispheric orthographic projection in R

library(sf)
library(ggplot2)
library(mapview)
library(lwgeom)
library(rnaturalearth)

# world data
world <- rnaturalearth::ne_countries(scale = 'small', returnclass = 'sf')


# 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')

# map
ggplot() +
  geom_sf(data=world, color="gray80", aes(fill=continent)) +
  coord_sf( crs= "+proj=ortho +lat_0=20 +lon_0=-30")

ggplot() +
  geom_sf(data=world, color="gray80", aes(fill=continent)) +
  coord_sf( crs= "+proj=ortho +lat_0=20 +lon_0=90")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment