在地理地图(R)上可视化邻接矩阵

时间:2019-11-11 13:55:07

标签: r ggplot2 igraph ggmap network-analysis

我目前正在形象化世界各国之间的外交关系,其条件是派驻外交使团。使馆。

我使用的数据可以可复制的方式位于以下网址:http://www.correlatesofwar.org/data-sets/diplomatic-exchange

此数据集中的变量如下:

  • ccode1:国家1的州号
  • ccode2:国家2的州号
  • year-观察年
  • DR_at_1-第2边第1边的外交代表级别(请参见下文)
  • DR_at_2-第1边第2边的外交代表级别(请参见下文)
  • DE-第1面和第2面之间的任何外交往来(见下文)
  • version-数据集的当前版本

在下面,您将找到一个邻接表的摘要,该表在一个单独的数据框中显示国家之间的外交关系以及每个国家的经度和纬度数据。请注意,国家之间的联系是假设的,而地理数据是准确的。

                         country1
country2   1'    2'      3'       4'      5'    6'    
1          0     1       0        0       0     0   
2          1     0       1        0       0     0   
3          0     0       0        0       1     1   
4          1     0       1        0       1     1   
5          0     1       0        0       0     1   
6          0     0       1        0       1     0   

Countryno.  Country       x (lat)   y (long) 
1           Afghanistan   66.004    33.835     
2           Australia     134.49    -25.73288
3           Belgium       4.64      50.6398  
4           Austria       14.121    47.585
5           Bangladesh    90.238    23.860
6           Belarus       28.03     53.5310  

您可以使用以下dput函数导入数据:

地理数据:

World_df <- structure(list(Country = c("Aruba", "Afghanistan", "Angola", 
"Anguilla", "Albania", "Aland", "Andorra", "United Arab Emirates", 
"Argentina", "Armenia", "American Samoa", "Antarctica", "Ashmore and Cartier Islands", 
"French Southern and Antarctic Lands", "Antigua and Barbuda", 
"Australia", "Austria", "Azerbaijan", "Burundi", "Belgium", "Benin", 
"Burkina Faso", "Bangladesh", "Bulgaria", "Bahrain", "The Bahamas", 
"Bosnia and Herzegovina", "Saint Barthelemy", "Belarus", "Belize", 
"Bermuda", "Bolivia", "Brazil", "Barbados", "Brunei", "Bhutan", 
"Botswana", "Central African Republic", "Canada", "Switzerland", 
"Chile", "China", "Ivory Coast", "Cameroon", "Democratic Republic of the Congo", 
"Republic of the Congo", "Cook Islands", "Colombia", "Comoros", 
"Cape Verde", "Costa Rica", "Cuba", "Curacao", "Cayman Islands", 
"Northern Cyprus", "Cyprus", "Czech Republic", "Germany", "Djibouti", 
"Dominica", "Denmark", "Dominican Republic", "Algeria", "Ecuador", 
"Egypt", "Eritrea", "Spain", "Estonia", "Ethiopia", "Finland", 
"Fiji", "Falkland Islands", "France", "Faroe Islands", "Federated States of Micronesia", 
"Gabon", "Gaza", "United Kingdom", "Georgia", "Guernsey", "Ghana", 
"Guinea", "Gambia", "Guinea Bissau", "Equatorial Guinea", "Greece", 
"Grenada", "Greenland", "Guatemala", "Guam", "Guyana", "Hong Kong S.A.R.", 
"Heard Island and McDonald Islands", "Honduras", "Croatia", "Haiti", 
"Hungary", "Indonesia", "Isle of Man", "India", "Indian Ocean Territories", 
"British Indian Ocean Territory", "Ireland", "Iran", "Iraq", 
"Iceland", "Israel", "Italy", "Jamaica", "Jersey", "Jordan", 
"Japan", "Siachen Glacier", "Kazakhstan", "Kenya", "Kyrgyzstan", 
"Cambodia", "Kiribati", "Saint Kitts and Nevis", "South Korea", 
"Kosovo", "Kuwait", "Laos", "Lebanon", "Liberia", "Libya", "Saint Lucia", 
"Liechtenstein", "Sri Lanka", "Lesotho", "Lithuania", "Luxembourg", 
"Latvia", "Macau S.A.R", "Saint Martin", "Morocco", "Monaco", 
"Moldova", "Madagascar", "Maldives", "Mexico", "Marshall Islands", 
"Macedonia", "Mali", "Malta", "Myanmar", "Montenegro", "Mongolia", 
"Northern Mariana Islands", "Mozambique", "Mauritania", "Montserrat", 
"Mauritius", "Malawi", "Malaysia", "Namibia", "New Caledonia", 
"Niger", "Norfolk Island", "Nigeria", "Nicaragua", "Niue", "Netherlands", 
"Norway", "Nepal", "Nauru", "New Zealand", "Oman", "Pakistan", 
"Panama", "Pitcairn Islands", "Peru", "Philippines", "Palau", 
"Papua New Guinea", "Poland", "Puerto Rico", "North Korea", "Portugal", 
"Paraguay", "French Polynesia", "Qatar", "Romania", "Russia", 
"Rwanda", "Western Sahara", "Saudi Arabia", "Sudan", "South Sudan", 
"Senegal", "Singapore", "South Georgia and South Sandwich Islands", 
"Saint Helena", "Solomon Islands", "Sierra Leone", "El Salvador", 
"San Marino", "Somaliland", "Somalia", "Saint Pierre and Miquelon", 
"Republic of Serbia", "Sao Tome and Principe", "Suriname", "Slovakia", 
"Slovenia", "Sweden", "Swaziland", "Sint Maarten", "Seychelles", 
"Syria", "Turks and Caicos Islands", "Chad", "Togo", "Thailand", 
"Tajikistan", "Turkmenistan", "East Timor", "Tonga", "Trinidad and Tobago", 
"Tunisia", "Turkey", "Taiwan", "United Republic of Tanzania", 
"Uganda", "Ukraine", "Uruguay", "United States of America", "Uzbekistan", 
"Vatican", "Saint Vincent and the Grenadines", "Venezuela", "British Virgin Islands", 
"United States Virgin Islands", "Vietnam", "Vanuatu", "West Bank", 
"Wallis and Futuna", "Samoa", "Yemen", "South Africa", "Zambia", 
"Zimbabwe", "Tuvalu", "French Guiana"), x = c(-69.98267466, 66.00473115, 
17.53736149, -63.06498464, 20.04983034, 19.95324636, 1.56053368, 
54.30015737, -65.17980881, 44.9299343, -170.7180186, 19.92113271, 
123.58383039, 69.22673416, -61.7946956, 134.49099877, 14.12647809, 
47.54599646, 29.8751201, 4.64064572, 2.32784527, -1.7545684, 
90.23812793, 25.21552327, 50.54197759, -76.62835738, 17.76876588, 
-62.84068712, 28.03208711, -88.71010228, -64.75454185, -64.6853892, 
-53.09783199, -59.55980198, 114.72202554, 90.401895, 23.7985373, 
20.46826364, -98.30775723, 8.20866006, -71.38255879, 103.81907385, 
-5.56921598, 12.73964266, 23.64395637, 15.21965722, -159.7872415, 
-73.08114936, 43.68238096, -23.95990969, -84.19208786, -79.01606913, 
-68.9712225, -80.91213058, 33.5684401, 33.00600358, 15.31240065, 
10.38577693, 42.56067681, -61.35773122, 10.02799418, -70.50569086, 
2.61732102, -78.75203573, 29.8618993, 38.84615444, -3.64754874, 
25.54248555, 39.6008014, 26.27467024, 165.45055475, -59.35240055, 
2.53618475, -6.88096322, 153.23706115, 11.78863005, 34.34757119, 
-2.86563362, 43.50778995, -2.57240158, -1.21676901, -10.94066379, 
-15.39601809, -14.94972941, 10.34138898, 22.95556207, -61.68219415, 
-41.34191035, -90.36482288, 144.76792243, -58.98202596, 114.11379862, 
73.52052712, -86.61516846, 16.40413079, -72.68527364, 19.39559381, 
117.24009764, -4.53873822, 79.61197355, 104.84946519, 72.44540239, 
-8.13793167, 54.27407071, 43.74353204, -18.57397086, 35.00444995, 
12.07000946, -77.31482556, -2.12687655, 36.77137251, 138.03089212, 
77.18011287, 67.29149657, 37.79594437, 74.54165548, 104.90694312, 
-45.6220913, -62.68754242, 127.83916926, 20.87249613, 47.58699362, 
103.73772128, 35.88016327, -9.32207781, 18.00865782, -60.96969329, 
9.53573593, 80.70107795, 28.22722773, 23.88720035, 6.07182681, 
24.9123704, 113.50933102, -63.05971623, -8.45616045, 7.40628055, 
28.45673742, 46.70473751, 73.45711081, -102.52345042, 170.33963635, 
21.68211343, -3.54268794, 14.40522042, 96.48843207, 19.2388382, 
103.05299651, 145.61961878, 35.53367351, -10.3477974, -62.18519807, 
57.57120758, 34.2893585, 109.69761552, 17.20962769, 165.68492075, 
9.38546454, 167.94920199, 8.08944243, -85.03052894, -169.8699537, 
5.2814735, 15.34832968, 83.9158367, 166.93258517, 171.48495391, 
56.09165967, 69.33958027, -80.11915152, -128.31704094, -74.38242889, 
122.88392153, 134.40822154, 145.20744918, 19.39011841, -66.47307314, 
127.19247812, -8.50105506, -58.40014084, -144.90488761, 51.18478512, 
24.97293286, 96.68657347, 29.91989113, -12.2198297, 44.53687041, 
29.94047226, 30.24790056, -14.47348904, 103.8172532, -36.43320448, 
-9.54768684, 159.63284711, -11.79271209, -88.87164456, 12.45923029, 
46.2519833, 45.70716243, -56.30319339, 20.78958121, 6.72429635, 
-55.91234833, 19.47905212, 14.8044464, 16.74371219, 31.48193159, 
-63.05713283, 55.47601997, 38.50787939, -71.97385725, 18.64493211, 
0.96232118, 101.00288352, 71.01363312, 59.37099935, 125.84438644, 
-174.80975436, -61.26565805, 9.55288287, 35.16895529, 120.95427264, 
34.81310214, 32.36907536, 31.38325794, -56.01806795, -112.46167057, 
63.14001799, 12.43386255, -61.20129438, -66.18184085, -64.47140241, 
-64.80300497, 106.29913857, 167.68636735, 35.24782134, -177.34813306, 
-172.16481475, 47.58676005, 25.08388384, 27.77475082, 29.85145031, 
178.51989487, -53.2484462), y = c(12.52088881, 33.83523216, -12.29336112, 
18.22396692, 41.14245128, 60.21490035, 42.54229174, 23.90527175, 
-35.38135347, 40.28952206, -14.30448922, -80.50856414, -12.42994255, 
-49.24895992, 17.27754256, -25.73288736, 47.58549847, 40.28827674, 
-3.35939257, 50.63981435, 9.6417533, 12.26953681, 23.86730412, 
42.76889988, 26.04200845, 24.29031222, 44.17449912, 17.89881045, 
53.53131155, 17.20027475, 32.31368594, -16.70814536, -10.78777567, 
13.18145371, 4.51968241, 27.4110619, -22.18402031, 6.56823003, 
61.36206827, 46.79785296, -37.73069754, 36.56176801, 7.62842868, 
5.69110296, -2.87746013, -0.83788172, -21.21926813, 3.91383156, 
-11.87777573, 15.95518822, 9.9763424, 21.62290023, 12.19552443, 
19.42896455, 35.2627605, 34.91666932, 49.73341073, 51.10697903, 
11.74872052, 15.43947211, 55.98126391, 18.8943308, 28.15893844, 
-1.42381949, 26.49593108, 15.36186881, 40.24448626, 58.67192416, 
8.62278417, 64.49884844, -17.4285609, -51.7448299, 46.18700581, 
62.05386227, 7.4528106, -0.58660329, 31.39150353, 54.12387156, 
42.16855944, 49.46809855, 7.95345124, 10.43621595, 13.44965377, 
12.04744454, 1.70554042, 39.07466973, 12.1172555, 74.71051369, 
15.69404158, 13.4416655, 4.79378052, 22.39828782, -53.08725301, 
14.82688225, 45.08047284, 18.93500961, 47.16277712, -2.21505018, 
54.2241965, 22.88578035, -10.64829627, -7.33058251, 53.1754426, 
32.57503287, 33.03970511, 64.99575263, 31.4611019, 42.79663568, 
18.15695476, 49.21837488, 31.24579389, 37.59230065, 35.39236619, 
48.15687954, 0.5998746, 41.46221785, 12.72004932, 0.86052252, 
17.26458594, 36.3852463, 42.57078418, 29.33431677, 18.50217971, 
33.92306993, 6.45278273, 27.03094247, 13.8947837, 47.13665447, 
7.61266309, -29.5800467, 55.32611078, 49.76724687, 56.85085306, 
22.22310807, 18.08889331, 29.83762793, 43.75276115, 47.19499323, 
-19.37189599, 3.72854733, 23.94753932, 7.00352799, 41.59531059, 
17.34581461, 35.92151315, 21.18566514, 42.7889115, 46.82681435, 
15.82873009, -17.27381898, 20.25736926, 16.73941361, -20.2777156, 
-13.2180697, 3.78987255, -22.13030791, -21.29990206, 17.41912605, 
-29.05143845, 9.59411807, 12.84709633, -19.0494623, 52.10080804, 
68.75014407, 28.24891362, -0.51914199, -41.81112825, 20.60515412, 
29.94975221, 8.5175119, -24.36498595, -9.15280281, 11.77539737, 
7.28760993, -6.46416276, 52.12759537, 18.22812711, 40.15350291, 
39.59550247, -23.22823896, -14.72228068, 25.30600936, 45.85242939, 
61.98052168, -1.99033199, 24.22956358, 24.12245467, 15.99035431, 
7.30877585, 14.36624381, 1.35877066, -54.46487151, -12.40367621, 
-8.92175696, 8.56329069, 13.73944008, 43.94186939, 9.73345795, 
4.75065195, 46.91919986, 44.22150308, 0.44390851, 4.13055377, 
48.70547175, 46.11555444, 62.79232016, -26.55842734, 18.05081813, 
-4.66097821, 35.02547248, 21.83047424, 15.33333336, 8.52529798, 
15.11816347, 38.53045125, 39.11554017, -8.8288854, -20.42814141, 
10.45735757, 34.11956663, 39.06160342, 23.75400343, -6.27565815, 
1.27469024, 48.99656784, -32.79951982, 45.67955203, 41.75554081, 
41.90174097, 13.22471841, 7.12422395, 18.52593375, 17.95501398, 
16.64601451, -16.22630588, 31.94798033, -13.88724988, -13.75327339, 
15.90927655, -29.0003377, -13.45824537, -19.00420587, -7.76005828, 
3.92595718), cwon = structure(list(`countrycode(World_df$Country, "country.name", "cown")` = c(NA, 
700L, 540L, NA, 339L, NA, 232L, 696L, 160L, 371L, NA, NA, NA, 
NA, 58L, 900L, 305L, 373L, 516L, 211L, 434L, 439L, 771L, 355L, 
692L, 31L, 346L, NA, 370L, 80L, NA, 145L, 140L, 53L, 835L, 760L, 
571L, 482L, 20L, 225L, 155L, 710L, 437L, 471L, 490L, 484L, NA, 
100L, 581L, 402L, 94L, 40L, NA, NA, 352L, 352L, 316L, 255L, 522L, 
54L, 390L, 42L, 615L, 130L, 651L, 531L, 230L, 366L, 530L, 375L, 
950L, NA, 220L, NA, 987L, 481L, NA, 200L, 372L, NA, 452L, 438L, 
420L, 404L, 411L, 350L, 55L, NA, 90L, NA, 110L, NA, NA, 91L, 
344L, 41L, 310L, 850L, NA, 750L, NA, NA, 205L, 630L, 645L, 395L, 
666L, 325L, 51L, NA, 663L, 740L, NA, 705L, 501L, 703L, 811L, 
946L, 60L, 732L, 347L, 690L, 812L, 660L, 450L, 620L, 56L, 223L, 
780L, 570L, 368L, 212L, 367L, NA, NA, 600L, 221L, 359L, 580L, 
781L, 70L, 983L, 343L, 432L, 338L, 775L, 341L, 712L, NA, 541L, 
435L, NA, 590L, 553L, 820L, 565L, NA, 436L, NA, 475L, 93L, NA, 
210L, 385L, 790L, 970L, 920L, 698L, 770L, 95L, NA, 135L, 840L, 
986L, 910L, 290L, NA, 731L, 235L, 150L, NA, 694L, 360L, 365L, 
517L, NA, 670L, 625L, 626L, 433L, 830L, NA, NA, 940L, 451L, 92L, 
331L, 520L, 520L, NA, NA, 403L, 115L, 317L, 349L, 380L, 572L, 
NA, 591L, 652L, NA, 483L, 461L, 800L, 702L, 701L, 860L, 955L, 
52L, 616L, 640L, 713L, 510L, 500L, 369L, 165L, 2L, 704L, 327L, 
57L, 101L, NA, NA, 817L, 935L, NA, NA, 990L, 679L, 560L, 551L, 
552L, 947L, NA)), row.names = c(NA, -244L), class = "data.frame")), row.names = c(NA, 
-244L), class = "data.frame")

邻接矩阵:

Network_Matrix_2005 <- structure(c(0, 1, 1, 1, 1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, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 
0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
1, 1, 1, 1, 0), .Dim = c(20L, 20L), .Dimnames = list(c("2", "20", 
"31", "40", "41", "42", "51", "52", "53", "54", "55", "56", "57", 
"58", "60", "70", "80", "90", "91", "92"), c("2", "20", "31", 
"40", "41", "42", "51", "52", "53", "54", "55", "56", "57", "58", 
"60", "70", "80", "90", "91", "92")))

如果我当前的数据格式不是最理想的,则可以在下面找到我用来转换原始数据的代码:

DiploData <- Diplomatic_Exchange_2006v1

Connected_countries <- DiploData %>% group_by(year) %>% summarise(Frequency = sum(DE))

DiploData_2005 <- DiploData %>%
filter(year == 2005)

edges <- DiploData_2005[DiploData_2005$DR_at_1 > 0 | DiploData_2005$DR_at_2 > 0,]
edges <- cbind(as.character(edges$ccode1), as.character(edges$ccode2))
edges <- rbind(edges, edges[,2:1])

codes <- unique(c(DiploData_2005$ccode1, DiploData_2005$ccode2))
Network_Matrix_2005 <- matrix(0, nrow=length(codes), ncol = length(codes), dimnames=list(codes, codes))
Network_Matrix_2005[edges] <- 1

邻接表的值基于2/1国家是否在1/2国家(DR_at_1 > 0DR_at_2 > 0)中具有外交代表权。

我现在希望这些节点显示在世界地图上。

到目前为止,我已经构建了一个包含经度,纬度的数据框,并使用以下代码将国家/地区代码转换为与原始数据(Diplomatic_Exchange_2006v1)中使用的国家/地区代码一致:

wmap <- getMap(resolution="high")
centroids <- gCentroid(wmap, byid=TRUE)
World_df <- as.data.frame(centroids)
World_df <- tibble::rownames_to_column(World_df, var = "Country")
World_df$cwon <- as.data.frame(countrycode(World_df$Country, 'country.name', 'cown'))

我希望解决方案可能类似于您在以下链接上找到的解决方案:https://www.r-bloggers.com/three-ways-of-visualizing-a-graph-on-a-map/

供您参考,我正在使用以下软件包:

library(ggplot2)
library(ggthemes)
library(dplyr)
library(tidyverse)
library(igraph)
library(GGally)
library(network)
library(sna)
library(countrycode)
library(assertthat)
library(purrr)
library(ggraph)
library(ggmap)
library(rworldmap)

0 个答案:

没有答案