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

问题描述 投票:0回答:3

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

我最想要一个包含

sf
ggplot
的解决方案,但欢迎任何解决方案。

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

r plot r-sf geo
3个回答
8
投票

您可以在

sf
ggplot2
中使用正交投影投影到
"+proj=ortho"
坐标参考系。

让海洋变蓝可能需要一些调整;我已经通过地球半径来缓冲空岛(受到这个要点的启发)。

有关所使用的参数(即 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()

ortographic view of world countries, with germany in red


2
投票

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

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

# Test of https://stackoverflow.com/a/70756593/7877917

# 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:\nhttps://stackoverflow.com/a/70756593/7877917"
  )

enter image description here


0
投票

我意识到这是一个旧帖子,但是有人知道如何在这张地图上绘制点吗?我尝试了几种不同的方法,但最终总是在中间找到一个点。

© www.soinside.com 2019 - 2024. All rights reserved.