我正在努力学习(或者更具体地说,记住并学习实现)R中的空间数据分析。我在操作地理数据方面遇到了一些基本的困难,这妨碍了任何方法的学习。
开门见山地说,当我试图绘制一张财富热图时,我得到了一些明显奇怪的结果:财富和填充之间没有美学联系,许多国家缺失。我预计由于数据集不完整,会缺失一些国家,但不会像这样多。其次,填充值与某个值匹配的所有国家似乎都具有相同的值-见下文。
我有一个数据集df
,包含各国的财富数据(来自瑞士信贷)和一些gdp增长数据,下面是dput()
:
我下载并解压缩了世界行政数据的shapefile:
download.file(
url = "https://public.opendatasoft.com/api/explore/v2.1/catalog/datasets/world-administrative-boundaries/exports/shp?lang=en&timezone=Europe%2FLondon",
destfile = "world_administrative_boundaries.zip")
unzip(
zipfile = "world_administrative_boundaries.zip",
exdir = "world_administrative_boundaries"
)
这很好用。顺便说一句,我希望得到国家和(联合 Realm )区域及较低级别空间数据来源的建议,以及数据接口的R包的建议。但无论如何...
首先,我加载了一些软件包:
library(tidyverse)
library(sf)
然后,我将形状文件读入R,并将数据集与我的财富数据合并。
world_adm0_sf <-st_read("world_administrative_boundaries/world-administrative-boundaries.shp")
world_adm0_sf <- inner_join(world_adm0_sf, df, by = "iso3")
这样就给予了一个部分数据集--但不像图中那样部分,然后我使用ggplot 2:
ggplot(data = world_adm0_sf, aes(fill=wealth)) +
geom_sf()+
scale_fill_steps() +
theme_light()
ggsave(filename = "Wealth.png",
device = "png",
width = 10,
height = 4)
这就产生了上图。
这都是初步的,我真实的的目标是创建一个邻接矩阵,这样我就可以在数据上运行一些空间模型,我想我可以这样做:
library(spdep)
w_world_adm0 <- poly2nb(world_adm0_sf) %>% nb2mat(style = "W")
但这会产生以下错误:Error in poly2nb(world_adm0_sf) : Empty geometries found
.我只能想象这与绘图的问题有关。
任何帮助都非常感谢。
structure(list(iso3 = c("USA", "CHN", "JPN", "DEU", "GBR", "FRA",
"IND", "CAN", "ITA", "AUS", "ESP", "NLD", "CHE", "MEX", "BEL",
"IDN", "BRA", "SWE", "SAU", "DNK", "AUT", "SGP", "NZL", "ISR",
"POL", "NOR", "THA", "PRT", "BGD", "ARE", "PHL", "VNM", "ZAF",
"IRL", "GRC", "FIN", "PAK", "CHL", "UKR", "ROU", "COL", "MYS",
"HUN", "KWT", "KAZ", "PER", "QAT", "LKA", "MAR", "KEN", "LBN",
"LUX", "ARG", "DZA", "BGR", "ECU", "ETH", "HRV", "BLR", "JOR",
"SVN", "SRB", "TUN", "CRI", "SLV", "OMN", "URY", "PAN", "LTU",
"BHR", "LVA", "ISL", "AZE", "CYP", "BOL", "BIH", "EST", "ALB",
"NPL", "KHM", "MUS", "COD", "MLT", "NIC", "TTO", "ARM", "GEO",
"CMR", "JAM", "LBY", "SEN", "MNE", "NAM", "MDG", "BWA", "ZMB",
"TJK", "MLI", "GAB", "MWI", "GNQ", "MOZ", "BRB", "MNG", "LBR",
"FJI", "TCD", "GUY", "ERI", "COG", "SYC", "SLE", "BLZ", "COM",
"DJI", "CAF", "LSO", "GNB"), wealth = c(126300.403036252, 73939.8891311991,
27260.570352125, 17389.799264483, 15675.9889849248, 16282.4392222225,
12700.3877756376, 10585.7395730159, 12176.0329952741, 9268.27874790906,
8481.28808374314, 5436.51408079177, 4672.6030405074, 3774.9451374071,
3543.77178519827, 3086.57900830581, 3034.93997199139, 2741.53111378823,
1905.26613759989, 1782.18132955766, 1855.14333493139, 1636.83125031141,
1288.20550646464, 1307.54537310394, 1464.60908917529, 1345.59016547028,
1392.90190541805, 1337.19890038495, 898.958801638123, 926.368205722374,
933.191002686083, 874.858045943263, 903.040059653359, 882.20086484466,
962.807508534614, 836.536239906252, 663.532991342168, 737.397506705501,
517.376031972012, 584.466282600888, 586.072886162324, 565.720706897012,
450.495646279881, 413.524330775955, 466.674062703931, 427.957436973251,
347.37129513471, 368.206981773813, 338.261747852825, 328.180531919524,
307.493974123627, 305.899138540934, 260.992240660559, 254.245993196906,
215.860597024656, 207.22935523629, 221.394916106677, 205.419322153595,
164.189635248546, 184.189116509732, 180.001925261347, 161.244568189131,
160.306734507052, 165.337454268781, 145.171877463561, 144.726277337691,
150.407054934631, 129.783182270863, 131.132956518345, 114.623545266656,
114.486922170596, 98.9661166555199, 98.3003532984958, 101.44441788665,
87.1461534054738, 74.9746836821397, 77.795657106148, 66.1431475890217,
71.2009574127775, 55.4559618547982, 59.5703711670758, 48.0465708689348,
57.4375916937781, 51.4806937778165, 46.9537143621449, 44.8053089422735,
43.2099298782149, 40.5284167581835, 41.1425896096207, 70.5563794908187,
39.5376722680099, 31.4416134859147, 25.4319082241779, 28.7033124617245,
23.5162145038624, 21.8465824967143, 21.5778619134227, 20.8494437096404,
17.2032811219951, 18.5250206627168, 14.6881425292255, 13.2498106184224,
15.5574142939735, 12.7783060223608, 11.2938871435042, 9.82612644104218,
8.61312574821742, 6.08951482556896, 5.04939300180385, 4.01486292583383,
3.2236344186979, 2.88763002016324, 2.54563458904526, 2.47536481004851,
1.96914002009599, 1.93613437718668, 1.90500798026385, 1.64313029137334
), gdp_growth = c(5.94548476122172, 8.1097925807738, 1.65707111732294,
2.62698726672524, 7.52491037367686, 6.81658913649873, 8.68122873083119,
4.54088730942124, 6.73727712298924, 2.23621243944461, 5.5195949732682,
4.86321914933848, 4.22171657654073, 4.72056529768081, 6.13386596356445,
3.69124011191289, 4.61942162067312, 5.07508472544021, 3.24092976848051,
4.85570758570233, 4.55685091308877, 7.61396261502647, 3.71401011995567,
8.61199034846989, 6.84771336241919, 3.88093603636767, 1.53383558441951,
5.482391708458, 6.93867508910159, 3.91629608037678, 5.70315596840621,
2.56155114232494, 4.9130966818804, 13.5882471079549, 8.43442550167381,
2.97322935640895, 6.48708677384843, 11.6682247016219, 3.40000000000001,
5.10012819668945, 10.6770130738261, 3.09215964982188, 7.11731531479809,
NA, 4.29999999999981, 13.3495090777977, 1.59075695426887, 3.32676381781447,
7.9296679419552, 7.51735538703886, -7.0012393603983, 5.10245544656804,
10.3982494646904, 3.50000000049758, 7.63460971569079, 4.23525124172572,
5.63730300819225, 13.0722049215979, 2.29987941172813, 2.2281384129623,
8.2110625918128, 7.54991253537386, 4.32277635659793, 7.57956529907922,
10.2784761168053, 3.09227668737446, 4.36700931644991, 15.3358739290709,
5.98103211278502, 2.22535124229128, 4.06807386332741, 4.40304478596893,
5.60003727280338, 6.63892696813528, 6.1058042252093, 7.54559112979621,
8.01346292400842, 8.51550112324915, 4.24694014145481, 3.02638936277746,
3.70361112396466, 6.20015401075946, 10.2977635999018, 10.3443407049189,
-1.02654016223218, 5.69999999999997, 10.4655372923886, 3.64991688648905,
4.60000000000001, 31.3724222267248, 6.06449596752137, 12.4343589851741,
2.65605081999696, 4.40234000007614, 11.3700218626674, 4.59873403878439,
9.2001904308498, 3.05261356942125, 1.462286510102, 2.75141640762955,
-0.945382916517076, 2.36398841807251, -0.189272866293138, 1.63676957584143,
4.98671283481617, -5.11160739321285, -1.1999905246095, 20.0600109474921,
NA, -2.20000260638274, 7.8618824199665, 4.10470008884118, 15.2261306532663,
2.11219557695527, 4.81247738295502, 0.900000006878756, 1.34762315508203,
3.80000242656607)), row.names = c(NA, -118L), class = c("tbl_df",
"tbl", "data.frame"))
1条答案
按热度按时间qvtsj1bj1#
问题的部分原因是您正在使用的行政边界的shapefile不完整。如果您在内部联接之前单独绘制它,则可以看到这种情况:
相反,我们可以使用方便的rnaturalearth包来获取世界上所有国家的shapefile,包括它们的ISO代码:
我们可以进行连接,并将数据绘制在Map的顶部,这样没有数据的国家仍然可见,我们应该选择一个差异很大的色标,以增加不同国家财富之间的对比: