I will try to outline my actions in detail.
To start with, I got a database of crude oil from un comtrade with imports by 5 countries (China, USA, India, South Korea, Japan) for the year 2000. Thus I have a database consisting of 145 lines. The first column contains information about the country which is the importer, the second column about the exporter.
structure(list(Reporter = c("USA", "USA", "USA", "USA", "India",
"Japan", "Japan", "USA", "Rep. of Korea", "USA", "Japan", "Japan",
"Japan", "Rep. of Korea", "USA", "USA", "USA", "China", "USA",
"USA", "Rep. of Korea", "USA", "Japan", "Japan", "Rep. of Korea",
"China", "China", "Rep. of Korea", "India", "China", "Rep. of Korea",
"USA", "Rep. of Korea", "Japan", "China", "Rep. of Korea", "India",
"China", "China", "India", "China", "China", "Japan", "Japan",
"Rep. of Korea", "India", "USA", "India", "USA", "Rep. of Korea",
"Rep. of Korea", "Rep. of Korea", "Japan", "USA", "Rep. of Korea",
"Japan", "USA", "USA", "USA", "Rep. of Korea", "USA", "China",
"Japan", "China", "China", "China", "Rep. of Korea", "India",
"Japan", "Japan", "USA", "China", "India", "China", "Rep. of Korea",
"Rep. of Korea", "China", "Japan", "China", "USA", "USA", "Rep. of Korea",
"USA", "Rep. of Korea", "China", "India", "China", "Rep. of Korea",
"Japan", "India", "Rep. of Korea", "Rep. of Korea", "USA", "India",
"China", "China", "China", "China", "USA", "Rep. of Korea", "China",
"USA", "USA", "Rep. of Korea", "USA", "USA", "Japan", "China",
"China", "Rep. of Korea", "India", "India", "Japan", "China",
"USA", "Japan", "USA", "USA", "USA", "USA", "China", "Rep. of Korea",
"Japan", "Rep. of Korea", "China", "USA", "USA", "China", "Rep. of Korea",
"Japan", "Japan", "USA", "USA", "USA", "India", "USA", "USA",
"China", "Japan", "India", "India", "USA", "USA", "India", "China"
), Partner = c("Saudi Arabia", "Canada", "Venezuela", "Mexico",
"Areas, nes", "Saudi Arabia", "United Arab Emirates", "Nigeria",
"Saudi Arabia", "Iraq", "Iran", "Qatar", "Kuwait", "United Arab Emirates",
"Angola", "Norway", "Colombia", "Oman", "United Kingdom", "Kuwait",
"Iran", "Gabon", "Indonesia", "Oman", "Kuwait", "Angola", "Iran",
"Oman", "Saudi Arabia", "Saudi Arabia", "Qatar", "Ecuador", "Indonesia",
"China", "Indonesia", "Australia", "Nigeria", "Yemen", "Fmr Sudan",
"Kuwait", "Iraq", "Viet Nam", "Iraq", "Australia", "Angola",
"United Arab Emirates", "Argentina", "Iran", "Trinidad and Tobago",
"Congo", "Yemen", "Iraq", "Viet Nam", "Australia", "Malaysia",
"Mexico", "Indonesia", "China", "Congo", "Ecuador", "Malaysia",
"Qatar", "Brunei Darussalam", "Norway", "Russian Federation",
"Congo", "Russian Federation", "Egypt", "Fmr Sudan", "Malaysia",
"Yemen", "Nigeria", "Yemen", "Equatorial Guinea", "Brunei Darussalam",
"Niger", "Australia", "USA", "United Kingdom", "Guatemala", "Brunei Darussalam",
"USA", "Dem. Rep. of the Congo", "Gabon", "Malaysia", "Malaysia",
"Kazakhstan", "Norway", "Nigeria", "Qatar", "China", "Fmr Sudan",
"Equatorial Guinea", "Iraq", "Gabon", "Kuwait", "United Arab Emirates",
"Cameroon", "Viet Nam", "Peru", "Papua New Guinea", "Russian Federation",
"Cameroon", "Nigeria", "Denmark", "Brazil", "Norway", "Thailand",
"Brunei Darussalam", "Guinea", "Mexico", "Oman", "Egypt", "Brazil",
"Peru", "Ecuador", "Thailand", "Spain", "Iran", "United Arab Emirates",
"Libya", "Antigua and Barbuda", "Yemen", "Egypt", "Egypt", "CГґte d'Ivoire",
"Oman", "USA", "Thailand", "Bahrain", "Thailand", "Germany",
"Estonia", "Neth. Antilles", "South Africa", "Namibia", "Singapore",
"Mongolia", "New Zealand", "USA", "Japan", "Algeria", "Rep. of Korea",
"Panama", "Germany"), Qty = c(69785202126, 68349221243, 68326932683,
64923669168, 57159000064, 53691639675, 52396394737, 46817696134,
38307387772, 31471382247, 25554794183, 19184268129, 18481591406,
16695296617, 16497467586, 16029110463, 15953011573, 15660839936,
14459452736, 13796910873, 11134838478, 10393629031, 10258716565,
9751327665, 9417368771, 8636634112, 7000465408, 6586187350, 5769723904,
5730211328, 5702528697, 5553458497, 5290777764, 5113191253, 4575188480,
4361612670, 3888963072, 3612423424, 3313590784, 3223781888, 3183182080,
3158472192, 3151280715, 3081015515, 3067260000, 2921931008, 2850134892,
2607684096, 2587749446, 2547349198, 2485083122, 2443798762, 2365431992,
2342513214, 2308853961, 2130664704, 1942125162, 1828376381, 1814260579,
1785874000, 1609282280, 1598901888, 1534923974, 1477843712, 1476737920,
1454356736, 1355873401, 1293729024, 1285355978, 1278701346, 1259876360,
1252518912, 1248772992, 1223383808, 1163368000, 1144188000, 1108399232,
1062041363, 1041526592, 977722731, 897418483, 877541040, 845556546,
801940467, 744316800, 739848000, 724177472, 694896000, 685405539,
672387008, 554965585, 540327751, 508204324, 4.87e+08, 457252032,
433428000, 430473920, 426744352, 408635880, 392727578, 390598528,
390189912, 389451923, 384376548, 350920922, 327039700, 285413702,
285143680, 275486240, 274015471, 264478000, 260122000, 238997756,
227806048, 204376795, 192144011, 150791409, 140634221, 135842986,
130777039, 129973032, 125115000, 124681401, 123443000, 120061792,
110795499, 106762492, 105548008, 84693986, 70275359, 57248174,
47944463, 40236018, 30783728, 18364000, 13419253, 12551365, 9631763,
5994199, 374000, 350000, 339115, 86420, 24000, 180)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -145L))
Next I create "vertices" with the names of importers and exporters:
vertices <- data.frame(name = unique(c(as.character(imports$Partner), as.character(imports$Reporter))) )
Result:
structure(list(name = c("USA", "India", "Japan", "Rep. of Korea",
"China", "Saudi Arabia", "Canada", "Venezuela", "Mexico", "Areas, nes",
"United Arab Emirates", "Nigeria", "Iraq", "Iran", "Qatar", "Kuwait",
"Angola", "Norway", "Colombia", "Oman", "United Kingdom", "Gabon",
"Indonesia", "Ecuador", "Australia", "Yemen", "Fmr Sudan", "Viet Nam",
"Argentina", "Trinidad and Tobago", "Congo", "Malaysia", "Brunei Darussalam",
"Russian Federation", "Egypt", "Equatorial Guinea", "Niger",
"Guatemala", "Dem. Rep. of the Congo", "Kazakhstan", "Cameroon",
"Peru", "Papua New Guinea", "Denmark", "Brazil", "Thailand",
"Guinea", "Spain", "Libya", "Antigua and Barbuda", "CГґte d'Ivoire",
"Bahrain", "Germany", "Estonia", "Neth. Antilles", "South Africa",
"Namibia", "Singapore", "Mongolia", "New Zealand", "Algeria",
"Panama")), class = "data.frame", row.names = c(NA, -62L))
Next:
mygraph <- graph_from_data_frame( imports, vertices=vertices )
Result:
structure(list(62, TRUE, c(0, 0, 0, 0, 1, 2, 2, 0, 3, 0, 2, 2,
2, 3, 0, 0, 0, 4, 0, 0, 3, 0, 2, 2, 3, 4, 4, 3, 1, 4, 3, 0, 3,
2, 4, 3, 1, 4, 4, 1, 4, 4, 2, 2, 3, 1, 0, 1, 0, 3, 3, 3, 2, 0,
3, 2, 0, 0, 0, 3, 0, 4, 2, 4, 4, 4, 3, 1, 2, 2, 0, 4, 1, 4, 3,
3, 4, 2, 4, 0, 0, 3, 0, 3, 4, 1, 4, 3, 2, 1, 3, 3, 0, 1, 4, 4,
4, 4, 0, 3, 4, 0, 0, 3, 0, 0, 2, 4, 4, 3, 1, 1, 2, 4, 0, 2, 0,
0, 0, 0, 4, 3, 2, 3, 4, 0, 0, 4, 3, 2, 2, 0, 0, 0, 1, 0, 0, 4,
2, 1, 1, 0, 0, 1, 4), c(5, 6, 7, 8, 9, 5, 10, 11, 5, 12, 13,
14, 15, 10, 16, 17, 18, 19, 20, 15, 13, 21, 22, 19, 15, 16, 13,
19, 5, 5, 14, 23, 22, 4, 22, 24, 11, 25, 26, 15, 12, 27, 12,
24, 16, 10, 28, 13, 29, 30, 25, 12, 27, 24, 31, 8, 22, 4, 30,
23, 31, 14, 32, 17, 33, 30, 33, 34, 26, 31, 25, 11, 25, 35, 32,
36, 24, 0, 20, 37, 32, 0, 38, 21, 31, 31, 39, 17, 11, 14, 4,
26, 35, 12, 21, 15, 10, 40, 27, 41, 42, 33, 40, 11, 43, 44, 17,
45, 32, 46, 8, 19, 34, 44, 41, 23, 45, 47, 13, 10, 48, 49, 25,
34, 34, 50, 19, 0, 45, 51, 45, 52, 53, 54, 55, 56, 57, 58, 59,
0, 2, 60, 3, 61, 52), c(142, 57, 0, 1, 2, 3, 119, 7, 9, 118,
19, 14, 15, 16, 126, 18, 21, 56, 31, 53, 70, 98, 46, 48, 58,
60, 80, 101, 92, 79, 82, 102, 114, 104, 105, 116, 117, 125, 131,
132, 133, 135, 136, 141, 139, 140, 28, 110, 4, 45, 36, 93, 47,
89, 39, 111, 72, 85, 67, 134, 143, 77, 33, 5, 55, 6, 88, 42,
10, 11, 12, 106, 23, 22, 115, 43, 122, 68, 52, 69, 62, 112, 130,
129, 138, 81, 90, 8, 13, 103, 51, 20, 30, 24, 44, 87, 27, 83,
32, 59, 35, 50, 91, 49, 54, 74, 66, 123, 75, 99, 128, 109, 121,
127, 29, 96, 71, 40, 26, 61, 95, 25, 63, 17, 78, 94, 34, 76,
37, 38, 41, 65, 84, 108, 64, 124, 73, 86, 97, 100, 113, 107,
120, 144, 137), c(139, 77, 81, 127, 140, 142, 57, 33, 90, 0,
28, 5, 8, 29, 1, 2, 3, 110, 55, 4, 119, 45, 6, 13, 96, 7, 36,
88, 103, 71, 9, 93, 42, 51, 40, 118, 47, 10, 20, 26, 89, 11,
30, 61, 19, 39, 12, 24, 95, 14, 44, 25, 15, 106, 87, 63, 16,
126, 111, 23, 27, 17, 18, 78, 21, 83, 94, 56, 22, 32, 34, 31,
115, 59, 53, 43, 35, 76, 70, 72, 122, 50, 37, 68, 91, 38, 98,
52, 41, 46, 48, 58, 49, 65, 60, 85, 69, 54, 84, 80, 62, 74, 108,
101, 66, 64, 67, 112, 123, 124, 92, 73, 75, 79, 82, 86, 102,
97, 114, 99, 100, 104, 105, 113, 116, 130, 128, 107, 109, 117,
120, 121, 125, 129, 131, 144, 132, 133, 134, 135, 136, 137, 138,
141, 143), c(0, 44, 61, 85, 113, 145, 145, 145, 145, 145, 145,
145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145,
145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145,
145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145,
145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145
), c(0, 4, 4, 5, 6, 9, 14, 15, 16, 19, 20, 25, 30, 35, 40, 44,
49, 52, 56, 57, 62, 64, 67, 71, 74, 78, 83, 86, 89, 90, 91, 94,
99, 103, 106, 110, 112, 113, 114, 115, 116, 118, 120, 121, 122,
124, 128, 129, 130, 131, 132, 133, 134, 136, 137, 138, 139, 140,
141, 142, 143, 144, 145), list(c(1, 0, 1), structure(list(), names = character(0)),
list(name = c("USA", "India", "Japan", "Rep. of Korea", "China",
"Saudi Arabia", "Canada", "Venezuela", "Mexico", "Areas, nes",
"United Arab Emirates", "Nigeria", "Iraq", "Iran", "Qatar",
"Kuwait", "Angola", "Norway", "Colombia", "Oman", "United Kingdom",
"Gabon", "Indonesia", "Ecuador", "Australia", "Yemen", "Fmr Sudan",
"Viet Nam", "Argentina", "Trinidad and Tobago", "Congo",
"Malaysia", "Brunei Darussalam", "Russian Federation", "Egypt",
"Equatorial Guinea", "Niger", "Guatemala", "Dem. Rep. of the Congo",
"Kazakhstan", "Cameroon", "Peru", "Papua New Guinea", "Denmark",
"Brazil", "Thailand", "Guinea", "Spain", "Libya", "Antigua and Barbuda",
"CГґte d'Ivoire", "Bahrain", "Germany", "Estonia", "Neth. Antilles",
"South Africa", "Namibia", "Singapore", "Mongolia", "New Zealand",
"Algeria", "Panama")), list(Qty = c(69785202126, 68349221243,
68326932683, 64923669168, 57159000064, 53691639675, 52396394737,
46817696134, 38307387772, 31471382247, 25554794183, 19184268129,
18481591406, 16695296617, 16497467586, 16029110463, 15953011573,
15660839936, 14459452736, 13796910873, 11134838478, 10393629031,
10258716565, 9751327665, 9417368771, 8636634112, 7000465408,
6586187350, 5769723904, 5730211328, 5702528697, 5553458497,
5290777764, 5113191253, 4575188480, 4361612670, 3888963072,
3612423424, 3313590784, 3223781888, 3183182080, 3158472192,
3151280715, 3081015515, 3067260000, 2921931008, 2850134892,
2607684096, 2587749446, 2547349198, 2485083122, 2443798762,
2365431992, 2342513214, 2308853961, 2130664704, 1942125162,
1828376381, 1814260579, 1785874000, 1609282280, 1598901888,
1534923974, 1477843712, 1476737920, 1454356736, 1355873401,
1293729024, 1285355978, 1278701346, 1259876360, 1252518912,
1248772992, 1223383808, 1163368000, 1144188000, 1108399232,
1062041363, 1041526592, 977722731, 897418483, 877541040,
845556546, 801940467, 744316800, 739848000, 724177472, 694896000,
685405539, 672387008, 554965585, 540327751, 508204324, 4.87e+08,
457252032, 433428000, 430473920, 426744352, 408635880, 392727578,
390598528, 390189912, 389451923, 384376548, 350920922, 327039700,
285413702, 285143680, 275486240, 274015471, 264478000, 260122000,
238997756, 227806048, 204376795, 192144011, 150791409, 140634221,
135842986, 130777039, 129973032, 125115000, 124681401, 123443000,
120061792, 110795499, 106762492, 105548008, 84693986, 70275359,
57248174, 47944463, 40236018, 30783728, 18364000, 13419253,
12551365, 9631763, 5994199, 374000, 350000, 339115, 86420,
24000, 180))), <environment>), class = "igraph")
Next:
from <- match( imports$Reporter, vertices$name)
to <- match( imports$Partner, vertices$name)
And the last one:
ggraph(mygraph, layout = 'dendrogram', circular = TRUE)
geom_conn_bundle(data = get_con(from = from, to = to), alpha=0.2, colour="skyblue", tension = 0)
geom_node_point(aes(filter = leaf, x = x*1.05, y=y*1.05))
theme_void()
Error:
Multiple parents. Unfolding graph
And the result itself is shown in the figure above.