Skip to content

Instantly share code, notes, and snippets.

@kadyb
Last active February 11, 2026 20:04
Show Gist options
  • Select an option

  • Save kadyb/4250e5827517eb647510cfe66754d668 to your computer and use it in GitHub Desktop.

Select an option

Save kadyb/4250e5827517eb647510cfe66754d668 to your computer and use it in GitHub Desktop.
World map in globe-like view using R, sf, and ggplot2
library("sf")
library("ggplot2")
library("rnaturalearth")
world = ne_countries(scale = "medium", returnclass = "sf")
sel = "Poland"
country = world[world$name == sel, ]
# center the view on the selected country
ct = st_centroid(st_geometry(country))
coords = st_coordinates(ct)
lon_0 = coords[[1, "X"]]
lat_0 = coords[[1, "Y"]]
ortho_crs = paste0("+proj=ortho +lat_0=", lat_0, " +lon_0=", lon_0)
# visible hemisphere
horizon = st_buffer(ct, dist = 9900000)
world = st_intersection(world, horizon)
world_ortho = st_transform(world, crs = ortho_crs)
world_ortho = st_make_valid(world_ortho)
world_coast = st_union(world_ortho)
ocean = st_point(c(lon_0, lat_0))
ocean = st_sfc(ocean, crs = 4326)
ocean = st_transform(ocean, crs = ortho_crs)
ocean = st_buffer(ocean, dist = 6371000)
g = st_graticule(lat = seq(-90, 90, 20), lon = seq(-180, 180, 30))
g = st_transform(g, crs = ortho_crs)
ggplot() +
geom_sf(data = ocean, fill = "aliceblue", color = NA) +
geom_sf(data = world_ortho, fill = "lightgrey", color = "white") +
geom_sf(data = world_coast, fill = NA, color = "aliceblue") +
geom_sf(data = subset(world_ortho, name == sel), fill = "darkgreen", color = "black") +
geom_sf(data = g, color = alpha("grey", 0.5)) +
theme_void()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment