在 R 中以地球视角绘制地理数据

plot geodata on the globe perspective in R

(如何)可以在具有 d3/perspective 视图的地球上绘制地理数据(例如多边形图层),类似于维基百科上的 this graphic

我最想要 sfggplot 的解决方案,但欢迎任何解决方案。

(我问这个主要是出于好奇,但由于我经常看到这样的图形,我想这个问题可能会有用)

您可以在 sfggplot2 中使用正投影投影到 "+proj=ortho" 坐标参考系统。

让海洋变蓝可能需要一些调整;我已采用地球半径缓冲 Null Island(受 this gist 启发)。

有关使用的参数(即 lat_0 和 lon_0)的更多信息,请查看 PROJ 文档 https://proj.org/operations/projections/ortho.html

有关更形象的示例,请考虑这段代码:

library(sf)
library(giscoR) # for the countries dataset only
library(ggplot2)

# projection string used for the polygons & ocean background
crs_string <- "+proj=ortho +lon_0=30 +lat_0=30"

# background for the globe - center buffered by earth radius
ocean <- st_point(x = c(0,0)) %>%
  st_buffer(dist = 6371000) %>%
  st_sfc(crs = crs_string)

# country polygons, cut to size
world <- gisco_countries %>% 
  st_intersection(ocean %>% st_transform(4326)) %>% # select visible area only
  st_transform(crs = crs_string) # reproject to ortho

# one of the visible ones red (don't really matter which one :)
world$fill_color <- ifelse(world$ISO3_CODE == "DEU", "interesting", "dull")
   
# now the action!
ggplot(data = world) +
  geom_sf(data = ocean, fill = "aliceblue", color = NA) + # background first
  geom_sf(aes(fill = fill_color), lwd = .1) + # now land over the oceans
  scale_fill_manual(values = c("interesting" = "red",
                               "dull" = "lightyellow"),
                    guide = "none") +
  theme_void()

太棒了@jindra-lacko!我通过创建南极洲的极地视图地图(地图的圣地 ;))冒昧地检查了上面的代码,并且工作惊人!:

library(sf)
library(giscoR) # for the countries dataset only
library(ggplot2)

# Test of 

# projection string used for the polygons & ocean background
crs_string <- "+proj=ortho +lat_0=-90 +lon_0=0"

# background for the globe - center buffered by earth diameter
ocean <- st_point(x = c(0, 0)) %>%
  st_buffer(dist = 6371000) %>%
  st_sfc(crs = crs_string)

# country polygons, cut to size
world <- gisco_countries %>%
  st_intersection(ocean %>% st_transform(4326)) %>% # select visible area only
  st_transform(crs = crs_string) # reproject to ortho

# one of the visible ones red (don't really matter which one :)
world$fill_color <- ifelse(world$ISO3_CODE == "DEU", "interesting", "dull")

# background for the globe - center buffered by earth diameter
ocean <- st_point(x = c(0, 0)) %>%
  st_buffer(dist = 6371000) %>%
  st_sfc(crs = crs_string)

# now the action!
ggplot(data = world) +
  geom_sf(data = ocean, fill = "aliceblue", color = "black") + # background first
  geom_sf(aes(fill = fill_color), lwd = .1) + # now land over the oceans
  scale_fill_manual(
    values = c(
      "interesting" = "red",
      "dull" = "lightyellow"
    ),
    guide = "none"
  ) +
  theme_void() +
  labs(
    title = "Polar View: Antarctica",
    caption = "Based on Jindra Lacko:\n"
  )