我已經看到 R 代碼使用 ggplot 和 sf 庫生成全球地圖,但由于我對地理的了解有限,我無法理解許多細節。這是我需要用 R 完成的可視化任務。
如果我有世界上兩個城市的坐標(例如緯度和經度),我想在它們之間畫一條紅色的弧線,這是經過這兩個城市的大圓上較短的弧線,我該怎么做?結果應該類似于我們在谷歌地圖上查找航線時得到的結果。
這是我在一個固定的全球地圖上停下來的地方:
library("ggplot2")
library("sf")
world <- ne_countries(scale = "medium", returnclass = "sf")
ggplot(data = world)
geom_sf()
coord_sf(crs = "some crs code")
crs部分需要具體的地理知識,不知道用什么以及不同代碼的含義。
uj5u.com熱心網友回復:
- 您可能想看看https://geocompr.robinlovelace.net/以了解什么是空間資料。
- 這
code是對地圖投影的參考。世界是一個球體(-ish),在二維表面(螢屏、紙張等)上的表示是一個挑戰。這就是為什么有這么多不同的預測(參見https://geocompr.robinlovelace.net/spatial-class.html#crs-intro和https://geocompr.robinlovelace.net/reproj-geo-data.html# crs-in-r)。還檢查https://epsg.io/以查找每個投影的代碼。
現在,回到您的問題,您可能想要在城市之間繪制“大圓圈”,即代表球體上各點之間最短路徑的曲線(航班使用這些線來優化飛行時間和燃料):
library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1; sf_use_s2() is TRUE
library(rnaturalearth)
library(tidyverse)
world <- ne_countries(scale = "medium", returnclass = "sf")
# Get three cities
cities <- ne_download(type = "populated_places", returnclass = "sf") %>%
filter(NAME %in% c("San Francisco", "Rio de Janeiro", "Oslo"))
# On Mercator
ggplot(world)
geom_sf()
geom_sf(data=cities, color="red", size=3)
coord_sf(crs="EPSG:3857")

# On Robinson projection
ggplot(world)
geom_sf()
geom_sf(data=cities, color="red", size=3)
coord_sf(crs="ESRI:54030")

# Create great circles
library(geosphere)
init <- cities %>% as("Spatial")
dest <- bind_rows(cities[-1,], cities[1,]) %>% as("Spatial")
lines <- gcIntermediate(
init,
dest,
sp = TRUE,
addStartEnd = TRUE,
n = 100,
breakAtDateLine = FALSE
)
#> Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj
#> = prefer_proj): Discarded datum Unknown based on WGS84 ellipsoid in Proj4
#> definition
lines_sf <- st_union(st_as_sf(lines))
# Now we have the lines
ggplot(lines_sf)
geom_sf()
geom_sf(data=cities, color="red")

# Create a nice plot
ggplot(world)
geom_sf()
geom_sf(data=lines_sf)
geom_sf(data=cities, color="red", size=3)
coord_sf(crs=" proj=laea x_0=0 y_0=0 lon_0=-74 lat_0=40")

由reprex 包于 2022-06-10 創建(v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/491416.html
