This tutorial will introduce you to map making and visualizing spatial networks in R. We will use animal trade data from the Convention on International Trade in Endangered Species of Wild Fauna and Flora (CITES) Trade Database to trace flows of particular species between countries.
First, let’s load the packages used in this tutorial. Some of these may be new packages for you. If so, download them first and then load them.
library(maps)
library(ggplot2)
library(tidyverse)
library(sf)
library(gridExtra)
library(sfnetworks)
library(ggraph)
library(igraph)
Second, let’s load in the data from CITES. On their Trade Database, you will see that you can filter their dataset by year, countries, taxa, purpose and other filters. For this analysis, I’ve selected only records from 2022 for the purpose “H - Hunting trophy.” This file may be downloaded here or you can directly download a .csv file of this data from the website.
Going further: Feel free to try downloading a different subset of the data based on your own interests. Your results may not look exactly the same, but you can adapt the following code based on your dataset.
Once the data are loaded, let’s take a look at the structure and the first few rows.
cites <- read.csv("https://maddiebrown.github.io/ANTH630/data/CITES_2022_HuntingTrophyData.csv")
str(cites)
'data.frame': 912 obs. of 16 variables:
$ Year : int 2022 2022 2022 2022 2022 2022 2022 2022 2022 2022 ...
$ App. : chr "I" "I" "I" "I" ...
$ Taxon : chr "Capra falconeri" "Capra falconeri" "Capra falconeri" "Capra falconeri" ...
$ Class : chr "Mammalia" "Mammalia" "Mammalia" "Mammalia" ...
$ Order : chr "Artiodactyla" "Artiodactyla" "Artiodactyla" "Artiodactyla" ...
$ Family : chr "Bovidae" "Bovidae" "Bovidae" "Bovidae" ...
$ Genus : chr "Capra" "Capra" "Capra" "Capra" ...
$ Importer : chr "ES" "ES" "IT" "MX" ...
$ Exporter : chr "KZ" "TJ" "RU" "PK" ...
$ Origin : chr "XX" "" "" "" ...
$ Importer.reported.quantity: int 1 2 2 NA NA NA NA NA NA 2 ...
$ Exporter.reported.quantity: int NA NA NA 1 4 1 6 1 1 NA ...
$ Term : chr "trophies" "trophies" "trophies" "trophies" ...
$ Unit : chr "Number of specimens" "Number of specimens" "Number of specimens" "Number of specimens" ...
$ Purpose : chr "H" "H" "H" "H" ...
$ Source : chr "W" "W" "C" "W" ...
head(cites)
Year App. Taxon Class Order Family Genus Importer
1 2022 I Capra falconeri Mammalia Artiodactyla Bovidae Capra ES
2 2022 I Capra falconeri Mammalia Artiodactyla Bovidae Capra ES
3 2022 I Capra falconeri Mammalia Artiodactyla Bovidae Capra IT
4 2022 I Capra falconeri Mammalia Artiodactyla Bovidae Capra MX
5 2022 I Capra falconeri Mammalia Artiodactyla Bovidae Capra RU
6 2022 I Capra falconeri Mammalia Artiodactyla Bovidae Capra SE
Exporter Origin Importer.reported.quantity Exporter.reported.quantity
1 KZ XX 1 NA
2 TJ 2 NA
3 RU 2 NA
4 PK NA 1
5 PK NA 4
6 PK NA 1
Term Unit Purpose Source
1 trophies Number of specimens H W
2 trophies Number of specimens H W
3 trophies Number of specimens H C
4 trophies Number of specimens H W
5 trophies Number of specimens H W
6 trophies Number of specimens H W
From this we see that there are 912 observations of 16 variables. Some of the column names and data points are self explanatory, but others may require checking the metadata. This database has a clear guide to the data. From this guide, we can see explanations of the various abbreviations and codes used in the data. In particular, the full names of the import and export country codes are listed out.
To save some time, I’ve already copied these codes and the full country names into a .csv file which can be downloaded here. In addition, I edited some of the country names to better match the names used in other spatial dataset we will be using in this tutorial.
country_code
column to
signal whether the match was for the import or export country.# read in country codes data
country_codes <- read.csv("https://maddiebrown.github.io/ANTH630/data/country_codes.csv")
# check out the structure of the data
str(country_codes)
'data.frame': 184 obs. of 3 variables:
$ country_code: chr "AD" "AE" "AF" "AG" ...
$ country_name: chr "Andorra" "United Arab Emirates" "Afghanistan" "Antigua and Barbuda" ...
$ X : chr "" "" "" "" ...
# merge the country codes based on importer codes
cites2 <- left_join(cites, country_codes, join_by(Importer == country_code))
# rename the name column to 'Importer_country'
cites2 <- rename(cites2, Importer_country = country_name)
# merge the country codes based on exporter codes
cites3 <- left_join(cites2, country_codes, join_by(Exporter == country_code))
# rename the name column to 'Exporter_country'
cites3 <- rename(cites3, Exporter_country = country_name)
# Double check that the merges worked as intended
cites3 %>%
select(Importer, Importer_country, Exporter, Exporter_country)
Importer Importer_country Exporter Exporter_country
1 ES Spain KZ Kazakhstan
2 ES Spain TJ Tajikistan
3 IT Italy RU Russia
4 MX Mexico PK Pakistan
5 RU Russia PK Pakistan
6 SE Sweden PK Pakistan
7 US United States PK Pakistan
8 CA Canada TJ Tajikistan
9 DE Germany TJ Tajikistan
10 DK Denmark TJ Tajikistan
11 ES Spain TJ Tajikistan
12 FR France TJ Tajikistan
13 US United States TJ Tajikistan
14 AT Austria ZA South Africa
15 AT Austria ZA South Africa
16 BE Belgium ZA South Africa
17 CZ <NA> ZA South Africa
18 DK Denmark ZA South Africa
19 EE Estonia ZA South Africa
20 ES Spain ZA South Africa
21 FI Finland ZA South Africa
22 HU Hungary ZA South Africa
23 HU Hungary ZA South Africa
24 PL Poland ZA South Africa
25 RO Romania ZA South Africa
26 SK <NA> ZA South Africa
27 DE Germany AE United Arab Emirates
28 PL Poland ZA South Africa
29 AT Austria ZW Zimbabwe
30 BG Bulgaria ZM Zambia
31 CZ <NA> ZM Zambia
32 DK Denmark ZM Zambia
33 FR France ZM Zambia
34 IT Italy TZ Tanzania
35 IT Italy ZM Zambia
36 MX Mexico TZ Tanzania
37 ZA South Africa TZ Tanzania
38 AT Austria <NA> Namibia
39 BG Bulgaria <NA> Namibia
40 CZ <NA> <NA> Namibia
41 DE Germany <NA> Namibia
42 DK Denmark <NA> Namibia
43 ES Spain <NA> Namibia
44 FI Finland <NA> Namibia
45 HU Hungary <NA> Namibia
46 IT Italy <NA> Namibia
47 LT Lithuania <NA> Namibia
48 PL Poland <NA> Namibia
49 RO Romania <NA> Namibia
50 AT Austria ZA South Africa
51 RO Romania <NA> Namibia
52 AT Austria <NA> Namibia
53 AT Austria TZ Tanzania
54 AT Austria TZ Tanzania
55 AT Austria ZM Zambia
56 AT Austria ZW Zimbabwe
57 AU Australia TZ Tanzania
58 BE Belgium <NA> Namibia
59 BE Belgium TZ Tanzania
60 BE Belgium ZM Zambia
61 BG Bulgaria <NA> Namibia
62 CZ <NA> <NA> Namibia
63 CZ <NA> TZ Tanzania
64 CZ <NA> TZ Tanzania
65 CZ <NA> ZM Zambia
66 DE Germany BW Botswana
67 DE Germany <NA> Namibia
68 DE Germany TZ Tanzania
69 DE Germany ZM Zambia
70 DE Germany ZW Zimbabwe
71 DK Denmark <NA> Namibia
72 DK Denmark TZ Tanzania
73 DK Denmark TZ Tanzania
74 DK Denmark ZM Zambia
75 DK Denmark ZW Zimbabwe
76 EC Ecuador ZA South Africa
77 ES Spain BW Botswana
78 ES Spain <NA> Namibia
79 ES Spain TZ Tanzania
80 ES Spain TZ Tanzania
81 ES Spain ZW Zimbabwe
82 FR France TZ Tanzania
83 FR France TZ Tanzania
84 FR France TZ Tanzania
85 FR France ZM Zambia
86 GB United Kingdom TZ Tanzania
87 HU Hungary <NA> Namibia
88 HU Hungary TZ Tanzania
89 HU Hungary TZ Tanzania
90 HU Hungary ZW Zimbabwe
91 IE Ireland ZW Zimbabwe
92 IT Italy ZM Zambia
93 LT Lithuania <NA> Namibia
94 LV Latvia <NA> Namibia
95 MA Morocco ZA South Africa
96 MX Mexico TZ Tanzania
97 OM Oman TZ Tanzania
98 OM Oman TZ Tanzania
99 OM Oman TZ Tanzania
100 PL Poland BW Botswana
101 PL Poland <NA> Namibia
102 PL Poland ZW Zimbabwe
103 PT Portugal ZM Zambia
104 RU Russia TZ Tanzania
105 SK <NA> <NA> Namibia
106 US United States TZ Tanzania
107 UY Uruguay ES Spain
108 ZA South Africa TZ Tanzania
109 ZA South Africa TZ Tanzania
110 BG Bulgaria <NA> Namibia
111 DK Denmark ZA South Africa
112 CZ <NA> <NA> Namibia
113 HU Hungary <NA> Namibia
114 DE Germany <NA> Namibia
115 BE Belgium CM Cameroon
116 DE Germany US United States
117 DK Denmark US United States
118 PT Portugal US United States
119 RO Romania US United States
120 AT Austria MA Morocco
121 BE Belgium ZA South Africa
122 BG Bulgaria ZA South Africa
123 CA Canada MA Morocco
124 DE Germany ZA South Africa
125 DK Denmark ZA South Africa
126 ES Spain MA Morocco
127 GB United Kingdom ES Spain
128 HU Hungary MA Morocco
129 HU Hungary MK North Macedonia
130 HU Hungary ZA South Africa
131 MX Mexico ES Spain
132 NO Norway ES Spain
133 RU Russia MA Morocco
134 SE Sweden ZA South Africa
135 TR Turkey ZA South Africa
136 US United States ES Spain
137 US United States MA Morocco
138 AT Austria AZ Azerbaijan
139 BE Belgium RU Russia
140 BG Bulgaria RU Russia
141 DE Germany RU Russia
142 FR France AZ Azerbaijan
143 HU Hungary AZ Azerbaijan
144 HU Hungary RU Russia
145 LV Latvia AZ Azerbaijan
146 PT Portugal RU Russia
147 DK Denmark ZA South Africa
148 BE Belgium CM Cameroon
149 HU Hungary ZA South Africa
150 AT Austria ZA South Africa
151 AT Austria ZA South Africa
152 DK Denmark ZA South Africa
153 FI Finland ZA South Africa
154 HU Hungary ZA South Africa
155 PT Portugal ZA South Africa
156 SE Sweden ZA South Africa
157 SK <NA> ZA South Africa
158 AT Austria <NA> Namibia
159 AT Austria ZA South Africa
160 AT Austria ZA South Africa
161 BE Belgium ZA South Africa
162 BG Bulgaria <NA> Namibia
163 BG Bulgaria ZA South Africa
164 CZ <NA> <NA> Namibia
165 CZ <NA> ZA South Africa
166 DE Germany <NA> Namibia
167 DE Germany <NA> Namibia
168 DE Germany ZA South Africa
169 DE Germany ZA South Africa
170 DE Germany ZA South Africa
171 DE Germany ZM Zambia
172 DE Germany ZM Zambia
173 DK Denmark <NA> Namibia
174 DK Denmark ZA South Africa
175 ES Spain BW Botswana
176 ES Spain ZA South Africa
177 FI Finland ZA South Africa
178 FI Finland ZA South Africa
179 HU Hungary <NA> Namibia
180 HU Hungary <NA> Namibia
181 HU Hungary ZA South Africa
182 HU Hungary ZA South Africa
183 HU Hungary ZM Zambia
184 PL Poland ZA South Africa
185 PT Portugal ZA South Africa
186 RO Romania <NA> Namibia
187 RO Romania ZA South Africa
188 SE Sweden ZA South Africa
189 SE Sweden ZA South Africa
190 SK <NA> <NA> Namibia
191 SK <NA> ZA South Africa
192 AR Argentina ES Spain
193 DE Germany MN Mongolia
194 ES Spain MN Mongolia
195 ES Spain TJ Tajikistan
196 DE Germany TJ Tajikistan
197 DK Denmark TJ Tajikistan
198 DK Denmark TJ Tajikistan
199 US United States TJ Tajikistan
200 DE Germany PK Pakistan
201 DE Germany PK Pakistan
202 ES Spain PK Pakistan
203 HU Hungary PK Pakistan
204 MX Mexico PK Pakistan
205 PL Poland PK Pakistan
206 RU Russia PK Pakistan
207 US United States PK Pakistan
208 AR Argentina KG Kyrgyzstan
209 AT Austria KG Kyrgyzstan
210 AT Austria KG Kyrgyzstan
211 AT Austria TJ Tajikistan
212 AT Austria TJ Tajikistan
213 AU Australia KG Kyrgyzstan
214 BE Belgium KG Kyrgyzstan
215 BG Bulgaria TJ Tajikistan
216 BY Belarus TJ Tajikistan
217 CA Canada KG Kyrgyzstan
218 CZ <NA> TJ Tajikistan
219 CZ <NA> TJ Tajikistan
220 DE Germany KG Kyrgyzstan
221 DE Germany KG Kyrgyzstan
222 DE Germany TJ Tajikistan
223 DK Denmark KG Kyrgyzstan
224 DK Denmark TJ Tajikistan
225 DK Denmark TJ Tajikistan
226 ES Spain KG Kyrgyzstan
227 ES Spain KG Kyrgyzstan
228 FR France KG Kyrgyzstan
229 FR France TJ Tajikistan
230 GB United Kingdom DK Denmark
231 HU Hungary KG Kyrgyzstan
232 HU Hungary KG Kyrgyzstan
233 HU Hungary TJ Tajikistan
234 HU Hungary TJ Tajikistan
235 ID Indonesia KG Kyrgyzstan
236 IR Iran KG Kyrgyzstan
237 MX Mexico KG Kyrgyzstan
238 MX Mexico TJ Tajikistan
239 RO Romania TJ Tajikistan
240 RU Russia KG Kyrgyzstan
241 RU Russia TJ Tajikistan
242 SK <NA> KG Kyrgyzstan
243 SK <NA> KG Kyrgyzstan
244 US United States KG Kyrgyzstan
245 ZA South Africa KG Kyrgyzstan
246 ZA South Africa TJ Tajikistan
247 ES Spain PK Pakistan
248 MX Mexico PK Pakistan
249 US United States PK Pakistan
250 CH Switzerland IT Italy
251 DE Germany ZA South Africa
252 DE Germany ZA South Africa
253 DK Denmark ZA South Africa
254 FI Finland ZA South Africa
255 IT Italy ZA South Africa
256 PT Portugal ZA South Africa
257 SK <NA> ZA South Africa
258 AR Argentina KG Kyrgyzstan
259 AT Austria RU Russia
260 BG Bulgaria RU Russia
261 BG Bulgaria TJ Tajikistan
262 CA Canada SE Sweden
263 CH Switzerland KG Kyrgyzstan
264 DE Germany CA Canada
265 DE Germany CA Canada
266 DE Germany KG Kyrgyzstan
267 DE Germany RU Russia
268 DE Germany RU Russia
269 DK Denmark CA Canada
270 ES Spain KG Kyrgyzstan
271 ES Spain KG Kyrgyzstan
272 FR France KG Kyrgyzstan
273 HU Hungary CA Canada
274 LB Lebanon BG Bulgaria
275 LU Luxembourg CA Canada
276 MX Mexico KG Kyrgyzstan
277 US United States KG Kyrgyzstan
278 US United States TJ Tajikistan
279 US United States ET Ethiopia
280 US United States TZ Tanzania
281 AT Austria ZA South Africa
282 BE Belgium ZA South Africa
283 BG Bulgaria ZA South Africa
284 CZ <NA> ZA South Africa
285 DE Germany ZA South Africa
286 DK Denmark ZA South Africa
287 DK Denmark ZW Zimbabwe
288 EE Estonia ZA South Africa
289 FI Finland ZA South Africa
290 HU Hungary ZA South Africa
291 RO Romania ZA South Africa
292 RO Romania ZA South Africa
293 SE Sweden ZA South Africa
294 US United States ET Ethiopia
295 US United States ET Ethiopia
296 BE Belgium CM Cameroon
297 BE Belgium ET Ethiopia
298 BE Belgium ET Ethiopia
299 DE Germany CM Cameroon
300 DE Germany TZ Tanzania
301 DE Germany TZ Tanzania
302 DK Denmark CM Cameroon
303 DK Denmark TZ Tanzania
304 ES Spain TZ Tanzania
305 RU Russia TZ Tanzania
306 US United States TZ Tanzania
307 US United States TZ Tanzania
308 ZA South Africa TZ Tanzania
309 BE Belgium ZM Zambia
310 BG Bulgaria ZM Zambia
311 CH Switzerland IT Italy
312 DE Germany TZ Tanzania
313 DE Germany TZ Tanzania
314 DE Germany ZA South Africa
315 DK Denmark TZ Tanzania
316 DK Denmark TZ Tanzania
317 DK Denmark ZA South Africa
318 DK Denmark ZM Zambia
319 ES Spain TZ Tanzania
320 ES Spain TZ Tanzania
321 FR France TZ Tanzania
322 FR France TZ Tanzania
323 IT Italy ZA South Africa
324 SK <NA> TZ Tanzania
325 US United States TZ Tanzania
326 US United States TZ Tanzania
327 ZA South Africa TZ Tanzania
328 US United States ET Ethiopia
329 AT Austria <NA> Namibia
330 AT Austria <NA> Namibia
331 AT Austria <NA> Namibia
332 AT Austria ZA South Africa
333 AT Austria ZW Zimbabwe
334 BE Belgium ZA South Africa
335 BG Bulgaria <NA> Namibia
336 CZ <NA> <NA> Namibia
337 CZ <NA> <NA> Namibia
338 DE Germany <NA> Namibia
339 DE Germany <NA> Namibia
340 DE Germany <NA> Namibia
341 DE Germany <NA> Namibia
342 DE Germany ZA South Africa
343 DE Germany ZA South Africa
344 DE Germany ZW Zimbabwe
345 DE Germany ZW Zimbabwe
346 DK Denmark CM Cameroon
347 DK Denmark <NA> Namibia
348 DK Denmark <NA> Namibia
349 DK Denmark ZA South Africa
350 DK Denmark ZM Zambia
351 DK Denmark ZM Zambia
352 DK Denmark ZW Zimbabwe
353 EE Estonia ZA South Africa
354 FI Finland ZA South Africa
355 FI Finland ZA South Africa
356 HU Hungary <NA> Namibia
357 HU Hungary ZA South Africa
358 HU Hungary ZW Zimbabwe
359 LT Lithuania <NA> Namibia
360 LT Lithuania ZA South Africa
361 PL Poland <NA> Namibia
362 PL Poland ZA South Africa
363 PL Poland ZW Zimbabwe
364 RO Romania <NA> Namibia
365 SE Sweden <NA> Namibia
366 SE Sweden ZA South Africa
367 SE Sweden ZA South Africa
368 SK <NA> <NA> Namibia
369 SK <NA> ZW Zimbabwe
370 US United States TZ Tanzania
371 US United States ET Ethiopia
372 AT Austria TZ Tanzania
373 AT Austria TZ Tanzania
374 AT Austria ZA South Africa
375 AT Austria ZM Zambia
376 AT Austria ZW Zimbabwe
377 BE Belgium TZ Tanzania
378 BE Belgium ZA South Africa
379 BE Belgium ZA South Africa
380 BE Belgium ZA South Africa
381 BE Belgium ZA South Africa
382 BG Bulgaria ZM Zambia
383 CZ <NA> ZA South Africa
384 DE Germany TZ Tanzania
385 DE Germany TZ Tanzania
386 DE Germany ZA South Africa
387 DE Germany ZA South Africa
388 DE Germany ZA South Africa
389 DE Germany ZM Zambia
390 DE Germany ZW Zimbabwe
391 DK Denmark TZ Tanzania
392 DK Denmark TZ Tanzania
393 DK Denmark TZ Tanzania
394 DK Denmark ZA South Africa
395 DK Denmark ZM Zambia
396 DK Denmark ZM Zambia
397 EE Estonia ZA South Africa
398 FI Finland ZA South Africa
399 FR France TZ Tanzania
400 GB United Kingdom TZ Tanzania
401 HU Hungary <NA> Namibia
402 HU Hungary ZA South Africa
403 LV Latvia ZA South Africa
404 MX Mexico TZ Tanzania
405 PL Poland TZ Tanzania
406 PL Poland TZ Tanzania
407 PL Poland ZA South Africa
408 PL Poland ZA South Africa
409 PT Portugal <NA> Namibia
410 PT Portugal ZA South Africa
411 PT Portugal ZM Zambia
412 RO Romania ZA South Africa
413 RO Romania ZA South Africa
414 SE Sweden ZA South Africa
415 SE Sweden ZM Zambia
416 US United States TZ Tanzania
417 US United States TZ Tanzania
418 ZA South Africa TZ Tanzania
419 AT Austria BW Botswana
420 AT Austria BW Botswana
421 AT Austria BW Botswana
422 AT Austria <NA> Namibia
423 AT Austria <NA> Namibia
424 AT Austria ZW Zimbabwe
425 AT Austria ZW Zimbabwe
426 AT Austria ZW Zimbabwe
427 BG Bulgaria ZW Zimbabwe
428 CH Switzerland DE Germany
429 CZ <NA> ZW Zimbabwe
430 DE Germany BW Botswana
431 DE Germany BW Botswana
432 DE Germany BW Botswana
433 DE Germany BW Botswana
434 DE Germany BW Botswana
435 DE Germany <NA> Namibia
436 DE Germany <NA> Namibia
437 DE Germany <NA> Namibia
438 DE Germany ZW Zimbabwe
439 DE Germany ZW Zimbabwe
440 DK Denmark BW Botswana
441 DK Denmark <NA> Namibia
442 DK Denmark ZA South Africa
443 DK Denmark ZW Zimbabwe
444 EC Ecuador ZA South Africa
445 ES Spain BW Botswana
446 ES Spain BW Botswana
447 ES Spain BW Botswana
448 ES Spain BW Botswana
449 ES Spain <NA> Namibia
450 ES Spain ZA South Africa
451 ES Spain ZW Zimbabwe
452 ES Spain ZW Zimbabwe
453 FI Finland ZW Zimbabwe
454 FR France BW Botswana
455 HU Hungary BW Botswana
456 HU Hungary <NA> Namibia
457 HU Hungary ZW Zimbabwe
458 IT Italy BW Botswana
459 IT Italy ZW Zimbabwe
460 JP Japan DE Germany
461 LT Lithuania <NA> Namibia
462 LU Luxembourg ZW Zimbabwe
463 LU Luxembourg ZW Zimbabwe
464 PL Poland BW Botswana
465 PL Poland ZW Zimbabwe
466 RO Romania ZW Zimbabwe
467 SE Sweden ZW Zimbabwe
468 SK <NA> ZW Zimbabwe
469 US United States DK Denmark
470 AT Austria <NA> Namibia
471 AT Austria <NA> Namibia
472 AT Austria <NA> Namibia
473 AT Austria ZA South Africa
474 BG Bulgaria <NA> Namibia
475 BG Bulgaria <NA> Namibia
476 CZ <NA> <NA> Namibia
477 CZ <NA> <NA> Namibia
478 DE Germany <NA> Namibia
479 DE Germany <NA> Namibia
480 DE Germany <NA> Namibia
481 DE Germany <NA> Namibia
482 DE Germany <NA> Namibia
483 DE Germany ZA South Africa
484 DK Denmark <NA> Namibia
485 DK Denmark <NA> Namibia
486 DK Denmark ZA South Africa
487 DK Denmark ZA South Africa
488 EC Ecuador ZA South Africa
489 ES Spain <NA> Namibia
490 FI Finland <NA> Namibia
491 HU Hungary <NA> Namibia
492 HU Hungary <NA> Namibia
493 HU Hungary ZA South Africa
494 LV Latvia <NA> Namibia
495 PE Peru EC Ecuador
496 PL Poland <NA> Namibia
497 PL Poland ZA South Africa
498 PT Portugal <NA> Namibia
499 SE Sweden <NA> Namibia
500 SE Sweden <NA> Namibia
501 SE Sweden ZA South Africa
502 SE Sweden ZA South Africa
503 SK <NA> <NA> Namibia
504 SK <NA> <NA> Namibia
505 AT Austria ZA South Africa
506 AT Austria ZA South Africa
507 DK Denmark ZA South Africa
508 HU Hungary ZA South Africa
509 QA Qatar KG Kyrgyzstan
510 AT Austria ZA South Africa
511 AT Austria ZA South Africa
512 AT Austria ZA South Africa
513 BE Belgium ZA South Africa
514 BE Belgium ZA South Africa
515 BG Bulgaria <NA> Namibia
516 CZ <NA> <NA> Namibia
517 CZ <NA> ZA South Africa
518 DE Germany <NA> Namibia
519 DE Germany <NA> Namibia
520 DE Germany ZA South Africa
521 DK Denmark ZA South Africa
522 EE Estonia ZA South Africa
523 FI Finland ZA South Africa
524 FI Finland ZA South Africa
525 HU Hungary <NA> Namibia
526 HU Hungary ZA South Africa
527 PL Poland <NA> Namibia
528 PL Poland ZA South Africa
529 PT Portugal ZA South Africa
530 RO Romania ZA South Africa
531 SE Sweden ZA South Africa
532 SK <NA> ZA South Africa
533 DE Germany ZW Zimbabwe
534 DE Germany ZW Zimbabwe
535 SE Sweden ZA South Africa
536 PL Poland ZW Zimbabwe
537 AT Austria ZA South Africa
538 AT Austria ZA South Africa
539 BE Belgium CM Cameroon
540 CZ <NA> ZA South Africa
541 DE Germany TZ Tanzania
542 DE Germany ZA South Africa
543 DK Denmark CM Cameroon
544 PL Poland ZA South Africa
545 RO Romania ZA South Africa
546 SE Sweden ZA South Africa
547 SE Sweden ZA South Africa
548 SK <NA> ZA South Africa
549 US United States TZ Tanzania
550 AT Austria CA Canada
551 CZ <NA> CA Canada
552 DK Denmark US United States
553 AT Austria ZA South Africa
554 AT Austria ZA South Africa
555 AT Austria ZM Zambia
556 AT Austria ZW Zimbabwe
557 BE Belgium TZ Tanzania
558 BE Belgium ZA South Africa
559 BG Bulgaria ZA South Africa
560 BG Bulgaria ZA South Africa
561 BG Bulgaria ZM Zambia
562 CH Switzerland TZ Tanzania
563 CZ <NA> ZA South Africa
564 DE Germany TZ Tanzania
565 DE Germany ZA South Africa
566 DE Germany ZA South Africa
567 DE Germany ZW Zimbabwe
568 DK Denmark TZ Tanzania
569 DK Denmark TZ Tanzania
570 DK Denmark ZA South Africa
571 DK Denmark ZA South Africa
572 DK Denmark ZA South Africa
573 EC Ecuador ZA South Africa
574 EE Estonia ZA South Africa
575 ES Spain TZ Tanzania
576 ES Spain ZA South Africa
577 ES Spain ZA South Africa
578 ES Spain ZW Zimbabwe
579 FI Finland ZA South Africa
580 FI Finland ZW Zimbabwe
581 HU Hungary ZA South Africa
582 IT Italy TZ Tanzania
583 IT Italy ZA South Africa
584 KZ Kazakhstan ZA South Africa
585 LI Liechtenstein AT Austria
586 LU Luxembourg ZA South Africa
587 MT Malta TZ Tanzania
588 OM Oman TZ Tanzania
589 PE Peru EC Ecuador
590 PL Poland ZA South Africa
591 PL Poland ZA South Africa
592 PL Poland ZA South Africa
593 PT Portugal ZA South Africa
594 RO Romania ZA South Africa
595 RO Romania ZA South Africa
596 RO Romania ZA South Africa
597 RO Romania ZA South Africa
598 RO Romania ZA South Africa
599 SE Sweden TZ Tanzania
600 SE Sweden TZ Tanzania
601 SE Sweden ZA South Africa
602 SI Slovenia ZA South Africa
603 SI Slovenia ZA South Africa
604 SK <NA> ZA South Africa
605 TR Turkey ZA South Africa
606 TR Turkey ZA South Africa
607 TR Turkey ZA South Africa
608 US United States TZ Tanzania
609 US United States TZ Tanzania
610 UY Uruguay ES Spain
611 ZA South Africa TZ Tanzania
612 AT Austria CA Canada
613 AT Austria ZA South Africa
614 BG Bulgaria CA Canada
615 CZ <NA> CA Canada
616 HU Hungary CA Canada
617 SE Sweden US United States
618 SE Sweden US United States
619 PL Poland ZA South Africa
620 AT Austria ZA South Africa
621 BE Belgium ZA South Africa
622 BG Bulgaria <NA> Namibia
623 BG Bulgaria ZA South Africa
624 CZ <NA> <NA> Namibia
625 CZ <NA> ZA South Africa
626 CZ <NA> ZA South Africa
627 DE Germany <NA> Namibia
628 DE Germany <NA> Namibia
629 DE Germany <NA> Namibia
630 DE Germany <NA> Namibia
631 DE Germany <NA> Namibia
632 DE Germany ZA South Africa
633 DE Germany ZA South Africa
634 DE Germany ZA South Africa
635 DE Germany ZW Zimbabwe
636 DK Denmark ZA South Africa
637 EC Ecuador ZA South Africa
638 EE Estonia ZA South Africa
639 ES Spain ZA South Africa
640 FI Finland ZA South Africa
641 HU Hungary <NA> Namibia
642 HU Hungary <NA> Namibia
643 HU Hungary ZA South Africa
644 IT Italy ZA South Africa
645 KZ Kazakhstan ZA South Africa
646 PE Peru EC Ecuador
647 PL Poland <NA> Namibia
648 PL Poland ZA South Africa
649 PL Poland ZW Zimbabwe
650 RO Romania ZA South Africa
651 RO Romania ZA South Africa
652 RO Romania ZA South Africa
653 SE Sweden <NA> Namibia
654 SE Sweden ZA South Africa
655 SK <NA> <NA> Namibia
656 SK <NA> <NA> Namibia
657 SK <NA> ZA South Africa
658 AT Austria TZ Tanzania
659 AT Austria TZ Tanzania
660 AT Austria TZ Tanzania
661 AT Austria TZ Tanzania
662 AT Austria TZ Tanzania
663 AT Austria ZA South Africa
664 AT Austria ZA South Africa
665 AT Austria ZM Zambia
666 AT Austria ZW Zimbabwe
667 BE Belgium CM Cameroon
668 BE Belgium ZA South Africa
669 BE Belgium ZM Zambia
670 BE Belgium ZW Zimbabwe
671 BG Bulgaria ZM Zambia
672 CZ <NA> TZ Tanzania
673 CZ <NA> TZ Tanzania
674 CZ <NA> ZM Zambia
675 CZ <NA> ZW Zimbabwe
676 DE Germany <NA> Namibia
677 DE Germany TZ Tanzania
678 DE Germany ZA South Africa
679 DE Germany ZA South Africa
680 DE Germany ZM Zambia
681 DE Germany ZM Zambia
682 DE Germany ZW Zimbabwe
683 DE Germany ZW Zimbabwe
684 DE Germany ZW Zimbabwe
685 DK Denmark TZ Tanzania
686 DK Denmark ZA South Africa
687 DK Denmark ZM Zambia
688 DK Denmark ZW Zimbabwe
689 EC Ecuador ZA South Africa
690 ES Spain TZ Tanzania
691 ES Spain ZA South Africa
692 ES Spain ZM Zambia
693 ES Spain ZW Zimbabwe
694 ES Spain ZW Zimbabwe
695 FR France TZ Tanzania
696 FR France TZ Tanzania
697 FR France TZ Tanzania
698 FR France ZM Zambia
699 GB United Kingdom TZ Tanzania
700 GB United Kingdom TZ Tanzania
701 HU Hungary <NA> Namibia
702 HU Hungary ZA South Africa
703 HU Hungary ZW Zimbabwe
704 IT Italy ZM Zambia
705 LI Liechtenstein AT Austria
706 LT Lithuania <NA> Namibia
707 NO Norway TZ Tanzania
708 PL Poland TZ Tanzania
709 PL Poland ZA South Africa
710 PL Poland ZA South Africa
711 PT Portugal ZA South Africa
712 PT Portugal ZM Zambia
713 US United States TZ Tanzania
714 US United States TZ Tanzania
715 US United States TZ Tanzania
716 ZA South Africa TZ Tanzania
717 ZA South Africa TZ Tanzania
718 AT Austria CM Cameroon
719 LT Lithuania <NA> Namibia
720 AT Austria ZA South Africa
721 AT Austria ZA South Africa
722 BG Bulgaria ZA South Africa
723 DE Germany ZA South Africa
724 DE Germany ZA South Africa
725 DK Denmark ZA South Africa
726 DK Denmark ZA South Africa
727 ES Spain <NA> Namibia
728 ES Spain ZA South Africa
729 ES Spain ZA South Africa
730 FR France ZA South Africa
731 HU Hungary ZA South Africa
732 HU Hungary ZA South Africa
733 PL Poland ZA South Africa
734 PT Portugal ZA South Africa
735 RO Romania ZA South Africa
736 SE Sweden ZA South Africa
737 AT Austria CA Canada
738 AT Austria CA Canada
739 CZ <NA> CA Canada
740 DE Germany CA Canada
741 DE Germany CA Canada
742 DK Denmark CA Canada
743 FI Finland CA Canada
744 HU Hungary CA Canada
745 PL Poland CA Canada
746 SE Sweden CA Canada
747 SE Sweden US United States
748 SE Sweden US United States
749 SK <NA> CA Canada
750 AD Andorra HR Croatia
751 AE United Arab Emirates DK Denmark
752 AT Austria RU Russia
753 CZ <NA> RU Russia
754 DE Germany RU Russia
755 DE Germany RU Russia
756 DK Denmark RU Russia
757 ES Spain CA Canada
758 ES Spain US United States
759 HU Hungary RU Russia
760 IT Italy RU Russia
761 KW Kuwait HR Croatia
762 <NA> Namibia HR Croatia
763 NO Norway SE Sweden
764 NO Norway SE Sweden
765 PL Poland RU Russia
766 PL Poland RU Russia
767 RS Serbia HR Croatia
768 SE Sweden RU Russia
769 UA Ukraine HR Croatia
770 US United States HR Croatia
771 US United States PL Poland
772 ES Spain CA Canada
773 AT Austria AR Argentina
774 BE Belgium AR Argentina
775 DE Germany AR Argentina
776 DE Germany US United States
777 EC Ecuador AR Argentina
778 FI Finland AR Argentina
779 PT Portugal AR Argentina
780 AT Austria KG Kyrgyzstan
781 AT Austria TR Turkey
782 AT Austria TR Turkey
783 AU Australia TR Turkey
784 BE Belgium TR Turkey
785 BG Bulgaria TR Turkey
786 BR Brazil TR Turkey
787 CA Canada TR Turkey
788 DE Germany PK Pakistan
789 DE Germany TR Turkey
790 DK Denmark PK Pakistan
791 DK Denmark TR Turkey
792 DK Denmark TR Turkey
793 ES Spain PK Pakistan
794 ES Spain TR Turkey
795 HU Hungary PK Pakistan
796 HU Hungary TR Turkey
797 HU Hungary TR Turkey
798 IT Italy TR Turkey
799 JM Jamaica TR Turkey
800 LU Luxembourg TR Turkey
801 MX Mexico PK Pakistan
802 MX Mexico TR Turkey
803 NL Netherlands TR Turkey
804 PL Poland PK Pakistan
805 PL Poland TR Turkey
806 RO Romania TR Turkey
807 RU Russia TR Turkey
808 SE Sweden TR Turkey
809 SE Sweden TR Turkey
810 SK <NA> TR Turkey
811 SK <NA> TR Turkey
812 US United States PK Pakistan
813 US United States TR Turkey
814 AE United Arab Emirates KG Kyrgyzstan
815 AR Argentina KG Kyrgyzstan
816 AT Austria KG Kyrgyzstan
817 AU Australia KG Kyrgyzstan
818 AU Australia KG Kyrgyzstan
819 BA Bosnia and Herzegovina KG Kyrgyzstan
820 BE Belgium KG Kyrgyzstan
821 BE Belgium KG Kyrgyzstan
822 BE Belgium PK Pakistan
823 BE Belgium TJ Tajikistan
824 BG Bulgaria KG Kyrgyzstan
825 BG Bulgaria KG Kyrgyzstan
826 CA Canada KG Kyrgyzstan
827 CH Switzerland KG Kyrgyzstan
828 CZ <NA> KG Kyrgyzstan
829 DE Germany KG Kyrgyzstan
830 DE Germany KG Kyrgyzstan
831 DE Germany MN Mongolia
832 DE Germany TJ Tajikistan
833 DK Denmark KG Kyrgyzstan
834 DK Denmark KG Kyrgyzstan
835 DK Denmark PK Pakistan
836 ES Spain KG Kyrgyzstan
837 ES Spain PK Pakistan
838 FI Finland KG Kyrgyzstan
839 FI Finland RU Russia
840 FR France KG Kyrgyzstan
841 GB United Kingdom KG Kyrgyzstan
842 HU Hungary KG Kyrgyzstan
843 HU Hungary KG Kyrgyzstan
844 ID Indonesia KG Kyrgyzstan
845 IE Ireland KG Kyrgyzstan
846 KZ Kazakhstan KG Kyrgyzstan
847 LV Latvia KG Kyrgyzstan
848 LV Latvia KG Kyrgyzstan
849 MX Mexico KG Kyrgyzstan
850 NL Netherlands KG Kyrgyzstan
851 NO Norway KG Kyrgyzstan
852 NZ New Zealand KG Kyrgyzstan
853 PL Poland KG Kyrgyzstan
854 PL Poland KG Kyrgyzstan
855 PL Poland MN Mongolia
856 PT Portugal KG Kyrgyzstan
857 PT Portugal KG Kyrgyzstan
858 RO Romania KG Kyrgyzstan
859 RS Serbia KG Kyrgyzstan
860 RU Russia KG Kyrgyzstan
861 SE Sweden KG Kyrgyzstan
862 SE Sweden PK Pakistan
863 TR Turkey KG Kyrgyzstan
864 UA Ukraine KG Kyrgyzstan
865 US United States KG Kyrgyzstan
866 US United States PK Pakistan
867 ZA South Africa KG Kyrgyzstan
868 BE Belgium NP Nepal
869 CZ <NA> NP Nepal
870 DK Denmark PK Pakistan
871 RU Russia PK Pakistan
872 US United States PK Pakistan
873 DK Denmark CM Cameroon
874 DK Denmark TZ Tanzania
875 DK Denmark TZ Tanzania
876 ES Spain TZ Tanzania
877 FR France TZ Tanzania
878 MA Morocco ZA South Africa
879 RU Russia TZ Tanzania
880 RU Russia TZ Tanzania
881 US United States ET Ethiopia
882 US United States TZ Tanzania
883 ZA South Africa TZ Tanzania
884 DK Denmark ZA South Africa
885 PT Portugal AR Argentina
886 DE Germany TZ Tanzania
887 RU Russia TZ Tanzania
888 US United States TZ Tanzania
889 DE Germany ZA South Africa
890 PL Poland ZA South Africa
891 SE Sweden ZA South Africa
892 AT Austria ZA South Africa
893 BE Belgium ZA South Africa
894 CZ <NA> ZA South Africa
895 DE Germany ZA South Africa
896 ES Spain TZ Tanzania
897 PL Poland ZA South Africa
898 SE Sweden ZA South Africa
899 SK <NA> ZA South Africa
900 US United States TZ Tanzania
901 US United States TZ Tanzania
902 US United States TZ Tanzania
903 AT Austria ZA South Africa
904 AT Austria ZA South Africa
905 BE Belgium ZA South Africa
906 DE Germany ZA South Africa
907 DK Denmark ZW Zimbabwe
908 EC Ecuador ZA South Africa
909 FI Finland ZA South Africa
910 HU Hungary ZA South Africa
911 PL Poland ZA South Africa
912 SE Sweden ZA South Africa
For this tutorial, we’ll be creating maps and network diagrams of the trade in lions (Panthera leo) in 2022. Taking a look at the import/export data, it seems that sometimes the trade is reported by the importing country and sometimes by the exporting country. There are also a lot of missing values.
cites3 %>%
select(Importer.reported.quantity, Exporter.reported.quantity) %>%
slice(10:20)
Importer.reported.quantity Exporter.reported.quantity
1 2 NA
2 NA 2
3 NA 1
4 NA 5
5 1 NA
6 1 NA
7 1 NA
8 1 NA
9 8 NA
10 2 NA
11 6 NA
In order to work with this data, we can collapse the importer and exporter reported quantities into a “totaltrade” column.
# sum the total number of imported specimens by the taxon per country. It looks
# like data are sometimes reported by the importer and other times by the
# exporter. Let's combine these reported numbers. If there are counts for each,
# let's use the importer number for this use case.
# first, we make a combined trade amount check this out for how to work with na
# in an ifelse:
# https://stackoverflow.com/questions/22076353/how-to-include-na-in-ifelse
cites3$totaltrade <- ifelse(is.na(cites3$Importer.reported.quantity), cites3$Exporter.reported.quantity,
cites3$Importer.reported.quantity)
# compare our new sum to the existing import and export data
cites3 %>%
select(totaltrade, Importer.reported.quantity, Exporter.reported.quantity) %>%
slice(10:20)
totaltrade Importer.reported.quantity Exporter.reported.quantity
1 2 2 NA
2 2 NA 2
3 1 NA 1
4 5 NA 5
5 1 1 NA
6 1 1 NA
7 1 1 NA
8 1 1 NA
9 8 8 NA
10 2 2 NA
11 6 6 NA
# looks good
Before we use these totaltrade
numbers, let’s double
check that all the trade is being reported in the same unit. This
dataset has a column called Unit
that we can make a table
of in order to check how many different units are used in the
dataset.
# there is only one row that is not in number of specimens reported
table(cites3$Unit) #this is 'g' of elephant hair
g Number of specimens
384 1 527
For this demonstration using only lion data, all the units seem to be in Number of specimens.
Now we can group the list of trade events into an aggregated list of
total imports per species per pair of countries in 2022. How might we
create this summary? Hint: Try group_by()
.
imports_perspecies_percountry <- cites3 %>%
group_by(Importer_country, Taxon) %>%
summarise(totaltrade = sum(totaltrade))
# double check work look at all the Loxodonta africana imported to Austria
# https://dplyr.tidyverse.org/reference/filter.html
cites3 %>%
filter(Taxon == "Loxodonta africana" & Importer_country == "Austria") %>%
top_n(10)
Year App. Taxon Class Order Family Genus
1 2022 I Loxodonta africana Mammalia Proboscidea Elephantidae Loxodonta
2 2022 II Loxodonta africana Mammalia Proboscidea Elephantidae Loxodonta
3 2022 II Loxodonta africana Mammalia Proboscidea Elephantidae Loxodonta
4 2022 II Loxodonta africana Mammalia Proboscidea Elephantidae Loxodonta
5 2022 II Loxodonta africana Mammalia Proboscidea Elephantidae Loxodonta
6 2022 II Loxodonta africana Mammalia Proboscidea Elephantidae Loxodonta
7 2022 II Loxodonta africana Mammalia Proboscidea Elephantidae Loxodonta
8 2022 II Loxodonta africana Mammalia Proboscidea Elephantidae Loxodonta
9 2022 II Loxodonta africana Mammalia Proboscidea Elephantidae Loxodonta
Importer Exporter Origin Importer.reported.quantity
1 AT ZW 2
2 AT BW 4
3 AT BW 6
4 AT BW 4
5 AT <NA> 1
6 AT <NA> 2
7 AT ZW 3
8 AT ZW 7
9 AT ZW 4
Exporter.reported.quantity Term Unit Purpose Source
1 NA trophies H W
2 NA skin pieces H W
3 NA trophies H W
4 NA tusks H W
5 NA trophies H W
6 NA tusks Number of specimens H W
7 NA teeth H W
8 NA trophies H W
9 NA tusks H W
Importer_country X.x Exporter_country X.y totaltrade
1 Austria Zimbabwe 2
2 Austria Botswana 4
3 Austria Botswana 6
4 Austria Botswana 4
5 Austria Namibia 1
6 Austria Namibia 2
7 Austria Zimbabwe 3
8 Austria Zimbabwe 7
9 Austria Zimbabwe 4
# sum the number reported by the importer country
cites3 %>%
filter(Taxon == "Loxodonta africana" & Importer_country == "Austria") %>%
summarise(sum = sum(totaltrade))
sum
1 33
Take a look at the output from this data processing.
imports_perspecies_percountry %>%
slice(1:10, 30:40)
# A tibble: 314 × 3
# Groups: Importer_country [57]
Importer_country Taxon totaltrade
<chr> <chr> <int>
1 Andorra Ursus arctos 1
2 Argentina Canis lupus 2
3 Argentina Capra sibirica 4
4 Argentina Ovis ammon 1
5 Argentina Ovis polii 1
6 Australia Capra hircus aegagrus 1
7 Australia Capra sibirica 5
8 Australia Ovis polii 2
9 Australia Panthera pardus 1
10 Austria Acinonyx jubatus 1
# ℹ 304 more rows
Then let’s filter out rows without data on the number of species.
# filter out rows with no data on number of specimens
# https://tidyr.tidyverse.org/reference/drop_na.html
imports_perspecies_percountry <- imports_perspecies_percountry %>%
drop_na(totaltrade)
First, let’s take a look at how many different countries each species is imported to.
# how many different countries are the taxa imported to?
# table(imports_perspecies_percountry$Taxon)
From this list, it appears that Panthera leo (lions) are imported to 28 countries. We’ll visualize these flows in the next section. First, we’ll make two subsets of the data, first for the imports and second fo rthe exports.
# Let's look at Panthera leo (lions), which is imported to 28 countries
Panthera_leo_imports <- imports_perspecies_percountry %>%
filter(Taxon == "Panthera leo")
# now let's repeat the process to isolate the export data sum the total number
# of exported specimens by the taxon per country
exports_perspecies_percountry <- cites3 %>%
group_by(Exporter_country, Taxon) %>%
summarise(totaltrade = sum(totaltrade))
# how many different countries are the taxa exported from?
table(exports_perspecies_percountry$Taxon)
Acinonyx jubatus Alligator mississippiensis
1 1
Ammotragus lervia Antilope cervicapra
4 2
Axis porcinus Canis aureus
1 4
Canis lupus Capra caucasica
6 2
Capra falconeri Capra falconeri heptneri
4 1
Capra hircus Capra hircus aegagrus
1 2
Capra sibirica Capra spp.
5 2
Caracal caracal Cephalophus dorsalis
2 1
Cephalophus silvicultor Ceratotherium simum
1 2
Ceratotherium simum simum Cercopithecus mitis
2 2
Cervus elaphus barbarus Chlorocebus pygerythrus
1 3
Chlorocebus spp. Civettictis civetta
1 2
Colobus guereza Crocodylus niloticus
1 5
Damaliscus pygargus Damaliscus pygargus pygargus
1 1
Diceros bicornis Equus zebra hartmannae
1 3
Equus zebra zebra Erythrocebus patas
1 1
Falco cherrug Felis lybica
1 2
Felis silvestris Galago senegalensis
1 1
Giraffa camelopardalis Gyps rueppellii
4 1
Hippopotamus amphibius Hyaena hyaena
7 1
Kobus leche Leptailurus serval
4 3
Loxodonta africana Lynx rufus
8 2
Mellivora capensis Oryx dammah
2 1
Oryx leucoryx Otolemur crassicaudatus
2 1
Ovis ammon Ovis bochariensis
3 1
Ovis cycloceros Ovis cycloceros cycloceros
1 1
Ovis polii Ovis punjabiensis
3 1
Panthera leo Panthera pardus
8 7
Papio anubis Papio cynocephalus
3 4
Papio hamadryas Papio ursinus
1 5
Philantomba monticola Piliocolobus spp.
2 1
Proteles cristata Pseudois nayaur
1 2
Puma concolor Puma concolor couguar
3 1
Python sebae Theropithecus gelada
1 1
Ursus americanus Ursus arctos
2 7
Ursus maritimus
1
# Let's look at Panthera leo (lions) #exported from 8 countries
Panthera_leo_exports <- exports_perspecies_percountry %>%
filter(Taxon == "Panthera leo")
# check out handiwork
Panthera_leo_exports
# A tibble: 8 × 3
# Groups: Exporter_country [8]
Exporter_country Taxon totaltrade
<chr> <chr> <int>
1 Austria Panthera leo 1
2 Ecuador Panthera leo 84
3 Namibia Panthera leo 1
4 South Africa Panthera leo 377
5 Spain Panthera leo 1
6 Tanzania Panthera leo 173
7 Zambia Panthera leo 2
8 Zimbabwe Panthera leo 10
Now the data are ready to be mapped.
maps
packageA simple map we can make of this data is a choropleth map with each
country shaded according to how many imports or exports they had. First,
we’ll load in the world map data from the maps
package. See
some tutorials linked below for more information on how to use this
package.
world <- map_data("world") #https://www.geeksforgeeks.org/adding-data-points-to-world-map-in-r/
# a stack overflow answer about how to find country names within the world map
# x <- maps::map('world', plot=FALSE) str(x) x$names
ggplot(world, aes(x = long, y = lat, group = group)) + geom_polygon()
Now we just have to merge the lion data with the spatial dataframe.
To do this, locate the column names with country data in them and
left_join()
them together. Finally, using
ggplot()
we graph the data.
# merge the lion import data with the world dataframe from the maps package
world_lionmerge <- left_join(world, Panthera_leo_imports, join_by(region == Importer_country))
# tutorial on how to make these types of maps
# https://cran.r-project.org/web/packages/ggfortify/vignettes/plot_map.html
ggplot(world_lionmerge, aes(x = long, y = lat, group = group, fill = totaltrade)) +
geom_polygon() + ggtitle("CITES recorded Lion imports in 2022 by country") +
scale_fill_viridis()
sf
packageThere are multiple ways to make maps with R. Now let’s make a
choropleth map of lion trade using the sf
package.
# an approach using the sf package. adapting the tutorial linked below to work
# with our dataset. https://ggplot2-book.org/maps
# https://r-spatial.org/r/2018/10/25/ggplot2-sf.html first we make a dataset of
# the world spatial data from the rnaturalearth dataset
world_sf <- ne_countries(scale = "medium", returnclass = "sf")
`?`(ne_countries())
class(world)
[1] "data.frame"
Previously we linked the lion trade data to the maps
dataset. Now join it to the new sf
dataset.
# subunit is used as the name as it seems to best match our data let's link the
# lion import data to the world map
panthera_leo_import_sf <- left_join(world_sf, Panthera_leo_imports, join_by(subunit ==
Importer_country))
With our newly joined dataset, we can plot the imports.
ggplot(data = panthera_leo_import_sf) + geom_sf(aes(fill = totaltrade)) + ggtitle("Self-reported Lion imports in 2022 by country") +
scale_fill_viridis()
# zoom in on Europe.
ggplot(data = panthera_leo_import_sf) + geom_sf(aes(fill = totaltrade)) + ggtitle("Self-reported Lion imports in 2022 by country") +
coord_sf(xlim = c(-12, 40), ylim = c(33, 75), expand = FALSE) + scale_fill_viridis()
We can also plot the imports and exports on maps next to one another
using gridarrange()
.
# link the export data
panthera_leo_export_sf <- left_join(world_sf, Panthera_leo_exports, join_by(subunit ==
Exporter_country))
p1 <- ggplot(data = panthera_leo_import_sf) + geom_sf(aes(fill = totaltrade)) + ggtitle("Self-reported Lion imports in 2022 by country") +
scale_fill_viridis()
p2 <- ggplot(data = panthera_leo_export_sf) + geom_sf(aes(fill = totaltrade)) + ggtitle("Self-reported Lion exports in 2022 by country") +
scale_fill_viridis()
grid.arrange(p1, p2)
sf
packageOur previous maps showed the imports and exports of lions aggregated to the level of an individual country. But what if we wanted to map the flow between two countries?
With a partner, brainstorm a workflow to approach this data visualization challenge, based on what we’ve covered about working with spatial and network data in the course thus far. Don’t worry if you don’t know which specific functions to use. Instead, focus on the data wrangling and analytical steps that would go into creating a visualization.
In order to map the flows, we need to locate a single point in each country that can serve as a reference point. There are numerous ways to identify a point inside the country boundaries. Here, we’ll follow this discussion that suggests using a function that will output a point within each country in the dataset.
Then we can replot the trade choropleth data but this time add in a point in each country. This doesn’t look the best, but it does show us that we are on the right track.
# how to find a point inside the country adapted from below
# https://github.com/ropensci/rnaturalearth/issues/55
country_point <- st_point_on_surface(world_sf)
# now plotting the import data with points for each country
ggplot() + geom_sf(data = panthera_leo_import_sf, aes(fill = totaltrade)) + geom_sf(data = country_point,
size = 1) + ggtitle("Self-reported Lion imports in 2022 by country") + scale_fill_viridis()
Now that we have the central points for each country, let’s map the
flows of lions between them. Our previous data wrangling efforts allowed
us to aggregate the total incoming and outgoing trades per country. To
track the flows between countries, we instead need to make a network
dataset. First, we’ll make a network object in the sf
package. Then, we’ll use igraph
and make an edgelist to
track these flows.
# Isolate out the total number of specimens exchanged between each country
imports_perspecies_perpairofcountries <- cites3 %>%
group_by(Importer_country, Exporter_country, Taxon) %>%
summarise(totaltrade = sum(totaltrade))
table(imports_perspecies_perpairofcountries$taxon)
< table of extent 0 >
# Isolate out the lion trades
Panthera_leo_tradeamounts <- imports_perspecies_perpairofcountries %>%
filter(Taxon == "Panthera leo")
# look at table
Panthera_leo_tradeamounts
# A tibble: 41 × 4
# Groups: Importer_country, Exporter_country [41]
Importer_country Exporter_country Taxon totaltrade
<chr> <chr> <chr> <int>
1 Austria South Africa Panthera leo 9
2 Austria Zambia Panthera leo 1
3 Austria Zimbabwe Panthera leo 2
4 Belgium South Africa Panthera leo 8
5 Belgium Tanzania Panthera leo 1
6 Bulgaria South Africa Panthera leo 5
7 Bulgaria Zambia Panthera leo 1
8 Denmark South Africa Panthera leo 22
9 Denmark Tanzania Panthera leo 3
10 Ecuador South Africa Panthera leo 84
# ℹ 31 more rows
To make a sf
network object, it seems like we need to
subset the nodes and ties similar to how this works in
igraph
. A link to a helpful tutorial I used to figure this
out is included below.
# start working on network
# https://github.com/loreabad6/TidyTuesday/blob/master/R/2021/week_05.md
# working with this dataset
Panthera_leo_tradeamounts
# A tibble: 41 × 4
# Groups: Importer_country, Exporter_country [41]
Importer_country Exporter_country Taxon totaltrade
<chr> <chr> <chr> <int>
1 Austria South Africa Panthera leo 9
2 Austria Zambia Panthera leo 1
3 Austria Zimbabwe Panthera leo 2
4 Belgium South Africa Panthera leo 8
5 Belgium Tanzania Panthera leo 1
6 Bulgaria South Africa Panthera leo 5
7 Bulgaria Zambia Panthera leo 1
8 Denmark South Africa Panthera leo 22
9 Denmark Tanzania Panthera leo 3
10 Ecuador South Africa Panthera leo 84
# ℹ 31 more rows
# create nodes
nodes <- country_point
# subset only columns we need
node_subset <- nodes %>%
select(name, geometry)
# filter to only those points that are in dataset
node_subset <- node_subset %>%
filter(name %in% Panthera_leo_tradeamounts$Importer_country | name %in% Panthera_leo_tradeamounts$Exporter_country)
# create edges
edges <- Panthera_leo_tradeamounts %>%
filter(Importer_country %in% node_subset$name, Exporter_country %in% node_subset$name) %>%
select(from = Importer_country, to = Exporter_country, everything())
edges <- edges %>%
select(-Taxon)
# make network object
liontrade_net <- sfnetwork(node_subset, edges)
Then we can make a map of the trades using the following code.
# set the layout with a function from the tutorial linked below see here:
# https://github.com/loreabad6/TidyTuesday/blob/master/R/2021/week_05.md
layout_sf = function(graph) {
# Extract X and Y coordinates from the nodes
graph = activate(graph, "nodes")
x = sf::st_coordinates(graph)[, "X"]
y = sf::st_coordinates(graph)[, "Y"]
data.frame(x, y)
}
# plot
ggraph(liontrade_net, layout = layout_sf) + geom_sf(data = panthera_leo_import_sf,
aes(fill = totaltrade)) + geom_edge_arc(aes(width = totaltrade, alpha = totaltrade),
strength = 0.7, color = "red") + geom_node_point(size = 1) + ggtitle("Trade in lions between countries. Countries colored by imports") +
scale_fill_viridis() + scale_fill_viridis()
# export graph
ggraph(liontrade_net, layout = layout_sf) + geom_sf(data = panthera_leo_export_sf,
aes(fill = totaltrade)) + geom_edge_arc(aes(width = totaltrade, alpha = totaltrade),
color = "red") + geom_node_point(size = 1) + ggtitle("Trade in lions between countries. Countries colored by exports") +
scale_fill_viridis()
# how to add a legend for the transparency
# https://stackoverflow.com/questions/70799664/r-how-to-modify-the-legend
p <- ggraph(liontrade_net, layout = layout_sf) + geom_sf(data = panthera_leo_export_sf,
aes(fill = totaltrade)) + geom_edge_arc(aes(width = totaltrade, alpha = totaltrade),
color = "red") + geom_node_point(size = 1) + ggtitle("Trade in lions between countries. Countries colored by exports") +
scale_fill_viridis()
p + scale_edge_alpha_continuous(name = "Weight", seq(0, 100, 20))
# plotting the network along a single axis ggraph(liontrade_net) +
# geom_edge_arc(aes(width = totaltrade/100)) + geom_node_text(size=5,
# aes(label=name))
For the next section, we’ll review how to visualize networks with igraph.
Panthera_leo_tradeamounts
object into an
edgelist with the columns: Exporter_country, Importer_country, and
totaltrade. Look back at last week’s lesson about how to turn a
dataframe into an igraph object.status
vertex attribute that notes whether a
country is only an exporter, importer or both.statuscolor
vertex attribute based on the
import/export status noted in the previous step.# first change order of import and export
lion_edgelist <- Panthera_leo_tradeamounts %>%
select(Exporter_country, Importer_country, totaltrade)
# make into network object
lion_igraph <- graph.data.frame(lion_edgelist)
# check out graph
lion_igraph
IGRAPH d3d1b81 DN-- 32 41 --
+ attr: name (v/c), totaltrade (e/n)
+ edges from d3d1b81 (vertex names):
[1] South Africa->Austria Zambia ->Austria
[3] Zimbabwe ->Austria South Africa->Belgium
[5] Tanzania ->Belgium South Africa->Bulgaria
[7] Zambia ->Bulgaria South Africa->Denmark
[9] Tanzania ->Denmark South Africa->Ecuador
[11] South Africa->Estonia South Africa->Finland
[13] Zimbabwe ->Finland South Africa->Germany
[15] Tanzania ->Germany Zimbabwe ->Germany
+ ... omitted several edges
# add node attributes based on import and export
V(lion_igraph)$status <- ifelse(V(lion_igraph)$name %in% Panthera_leo_tradeamounts$Exporter_country &
V(lion_igraph)$name %in% Panthera_leo_tradeamounts$Importer_country, "Both",
ifelse(V(lion_igraph)$name %in% Panthera_leo_tradeamounts$Exporter_country, "Exporter",
"Importer"))
V(lion_igraph)$status
[1] "Both" "Exporter" "Exporter" "Exporter" "Both" "Both"
[7] "Exporter" "Both" "Importer" "Importer" "Importer" "Importer"
[13] "Importer" "Importer" "Importer" "Importer" "Importer" "Importer"
[19] "Importer" "Importer" "Importer" "Importer" "Importer" "Importer"
[25] "Importer" "Importer" "Importer" "Importer" "Importer" "Importer"
[31] "Importer" "Importer"
V(lion_igraph)$name
[1] "South Africa" "Zambia" "Zimbabwe" "Tanzania"
[5] "Austria" "Ecuador" "Namibia" "Spain"
[9] "Belgium" "Bulgaria" "Denmark" "Estonia"
[13] "Finland" "Germany" "Hungary" "Italy"
[17] "Kazakhstan" "Liechtenstein" "Luxembourg" "Malta"
[21] "Oman" "Peru" "Poland" "Portugal"
[25] "Romania" "Slovenia" "Sweden" "Switzerland"
[29] "Turkey" "United States" "Uruguay" "NA"
# make status color for nodes blue is export, red is import, and purple is for
# countries who both import and export lions
V(lion_igraph)$statuscolor <- ifelse(V(lion_igraph)$status == "Both", "Purple", ifelse(V(lion_igraph)$status ==
"Exporter", "Light Blue", "Red"))
plot(lion_igraph)
Here are some further adjustments to make the graph slightly more legible. Whenever working with RMarkdown, the node labels might still end up overlapping. When producing figures for publication, you can get around this by exporting the figure with specific margin settings. Alternatively, you can pop out the figure from the viewer in RStudio and resize it within the window.
# adjust transparency of nodes within graph:
# https://stackoverflow.com/questions/53803011/how-to-set-different-transparencies-for-different-vertices-in-igraph
plot(lion_igraph, layout = layout.fruchterman.reingold, edge.arrow.size = 0.4, vertex.label.cex = 0.7,
vertex.label.color = "black", vertex.label.family = "sans", vertex.color = adjustcolor(V(lion_igraph)$statuscolor,
0.5), vertex.frame.color = "light yellow", edge.width = E(lion_igraph)$totaltrade/15 +
0.2)
# from this you can see an issue with 'NA' (not to mention this is also the
# code for Namibia). When we previously removed missing values, this may have
# created issues. It would be important to go back and check through why there
# are missing values associated with the South African exports in this
# visualization.
# Panthera_leo_tradeamounts %>% filter(Exporter_country=='South Africa')
Recreate the map of trades between countries and the network map for a different species or other subset of the data. What can you infer from these visualizations about wild animal trade?