我想使用sf和plotly包繪制地理空間地圖。但是,我沒有國家級資料,只有地區級資料(亞洲、歐洲、拉丁美洲等)。這是我將要處理的資料集的模型,我正在尋找一種查找經度、緯度和幾何形狀的方法。我打算先在Excel中收集所有資料,然后將它們移動到R分析

我已經查找了幾個包/資料集,例如rnaturalearth、maps::map等,但我找不到正確的緯度/經度/坐標或區域的幾何形狀。我在哪里可以找到它們?提前致謝
uj5u.com熱心網友回復:
在這里,您對您的問題有一些想法:
- 由于不確定您需要如何定義每個區域(國家的區域分類有很多可能性),也許您應該首先將世界上的每個國家映射到您認為它所屬的區域。之后,您只需將相應區域加入每個國家/地區即可。
- 在 excel 中存盤幾何圖形不是一個好的選擇(欄位長度、正在使用什么 crs 等)。存盤空間物件的更好選擇是
.gpkg格式。
請在此處找到如何生成您提到的資料集。
我將在這里使用聯合國的地理區域分類。這在countrycode包裝上可用,所以基本上我將每個 ISO-3 國家代碼映射到相應的區域。之后,我按地區對國家進行分組,這樣我就可以按地區擁有一個幾何圖形。最后,我提取邊界框并將它們附加到資料集:
library(giscoR)
library(countrycode)
library(sf)
world <- gisco_get_countries()
# Add the subregion
world$region <- countrycode(world$ISO3_CODE,
origin = "iso3c",
destination = "un.regionsub.name")
#> Warning in countrycode_convert(sourcevar = sourcevar, origin = origin, destination = dest, : Some values were not matched unambiguously: ATA, CPT, XA, XB, XC, XD, XE, XF, XG, XH, XI, XL, XM, XN, XO, XU, XV
unique(world$region)
#> [1] "Latin America and the Caribbean" "Polynesia"
#> [3] "Western Europe" NA
#> [5] "Southern Europe" "Western Asia"
#> [7] "Southern Asia" "Sub-Saharan Africa"
#> [9] "Australia and New Zealand" "Eastern Europe"
#> [11] "Northern America" "South-eastern Asia"
#> [13] "Eastern Asia" "Micronesia"
#> [15] "Northern Europe" "Melanesia"
#> [17] "Central Asia" "Northern Africa"
# Group regions
library(dplyr)
library(ggplot2)
subworld <- world %>%
group_by(region) %>%
# Mock the data field
summarise(data=n())
ggplot(subworld)
geom_sf(aes(fill=region))

# Create final table
bbox <- lapply(st_geometry(subworld), st_bbox) %>%
bind_rows() %>%
mutate(across(where(is.numeric), as.double))
# Final dataset
end <- subworld %>% bind_cols(bbox) %>%
select(region, data, xmin, xmax, ymin, ymax)
head(end)
#> Simple feature collection with 6 features and 6 fields
#> Geometry type: MULTIPOLYGON
#> Dimension: XY
#> Bounding box: xmin: -180 ymin: -59.51912 xmax: 180 ymax: 81.85799
#> Geodetic CRS: WGS 84
#> # A tibble: 6 x 7
#> region data xmin xmax ymin ymax geometry
#> <chr> <int> <dbl> <dbl> <dbl> <dbl> <MULTIPOLYGON [°]>
#> 1 Australia and New ~ 6 -178. 179. -53.2 -9.21 (((143.5617 -13.77157, 1~
#> 2 Central Asia 5 46.6 87.3 35.2 55.4 (((50.3399 45.05235, 50.~
#> 3 Eastern Asia 7 73.6 146. 18.2 53.5 (((120.8954 22.336, 120.~
#> 4 Eastern Europe 10 -180 180. 41.2 81.9 (((68.7333 76.49168, 68.~
#> 5 Latin America and ~ 48 -118. 3.43 -59.5 32.7 (((-64.01772 -54.74885, ~
#> 6 Melanesia 5 -180 180. -22.7 -0.826 (((178.2331 -17.34448, 1~
由reprex 包于 2022-03-23 創建(v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/447565.html
下一篇:C#替換怪異
