class: center, middle, inverse, title-slide # Join data
π€ --- layout: true <div class="my-footer"> <span> <a href="https://rstd.io/bootcamper" target="_blank">rstd.io/bootcamper</a> </span> </div> --- class: middle # Working with multiple data frames --- class: middle .hand[You...] .huge-blue[have] .hand[multiple data frames] .huge-pink[want] .hand[to slice it, and dice it, and juice it, and process it] --- ## Fisheries of the world Fisheries and Aquaculture Department of the Food and Agriculture Organization of the United Nations collects data on fisheries production of countries. <img src="img/fisheries-data.png" width="60%" style="display: block; margin: auto;" /> .center[ .hand[...] ] .footnote[ - Source: https://en.wikipedia.org/wiki/Fishing_industry_by_country ] --- ## Load data ```r fisheries <- read_csv("data/fisheries.csv") ``` ``` ## Parsed with column specification: ## cols( ## country = col_character(), ## capture = col_double(), ## aquaculture = col_double(), ## total = col_double() ## ) ``` --- ## First peek at the data .xsmall[ ```r skim(fisheries) ``` ``` ## ββ Data Summary ββββββββββββββββββββββββ ## Values ## Name fisheries ## Number of rows 216 ## Number of columns 4 ## _______________________ ## Column type frequency: ## character 1 ## numeric 3 ## ________________________ ## Group variables ## ## ββ Variable type: character ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ ## skim_variable n_missing complete_rate min max empty n_unique whitespace ## 1 country 0 1 4 32 0 215 0 ## ## ββ Variable type: numeric ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ ## skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist ## 1 capture 0 1 421916. 1478638. 0 3280. 33797 221884. 17800000 βββββ ## 2 aquaculture 0 1 508368. 4496073. 0 25.2 1574. 25998 63700000 βββββ ## 3 total 0 1 930284. 5846301. 0 7270. 44648. 271901. 81500000 βββββ ``` ] --- ## Some summary stats ```r fisheries %>% summarise( mean_cap = mean(capture), mean_aqc = mean(aquaculture), mean_tot = mean(total) ) ``` ``` ## # A tibble: 1 x 3 ## mean_cap mean_aqc mean_tot ## <dbl> <dbl> <dbl> ## 1 421916. 508368. 930284. ``` -- <br> .hand[ well, that was boring... ] --- ## A new approach! ```r fisheries %>% summarise(across(capture:total, mean)) ``` ``` ## # A tibble: 1 x 3 ## capture aquaculture total ## <dbl> <dbl> <dbl> ## 1 421916. 508368. 930284. ``` -- .hand[and once we learn more about data types...] ```r fisheries %>% summarise(across(is.numeric, mean)) ``` ``` ## # A tibble: 1 x 3 ## capture aquaculture total ## <dbl> <dbl> <dbl> ## 1 421916. 508368. 930284. ``` --- .discussion[ The (not-so-great) visualization below shows the distribution of fishery harvest of countries for 2016, by capture and aquaculture. What are some ways you would improve this visualization? Note that countries whose total harvest was less than 100,000 tons are not included in the visualization. ] <img src="img/fisheries.png" width="60%" style="display: block; margin: auto;" /> -- - Calculate summary statistics at the continent level and visualize them. - Map the data. --- .huge-pink[goal:] .hand-blue[calculate summary statistics at the continent level and visualize them] --- ## Data prep ```r continents <- read_csv("data/continents.csv") ``` Filter out countries whose total harvest was less than 100,000 tons since they are not included in the visualization: ```r fisheries <- fisheries %>% filter(total > 100000) fisheries ``` ``` ## # A tibble: 82 x 4 ## country capture aquaculture total ## <chr> <dbl> <dbl> <dbl> ## 1 Angola 486490 655 487145 ## 2 Argentina 755226 3673 758899 ## 3 Australia 174629 96847 271476 ## 4 Bangladesh 1674770 2203554 3878324 ## 5 Brazil 705000 581230 1286230 ## 6 Cambodia 629950 172500 802450 ## # β¦ with 76 more rows ``` --- class: middle # Data joins --- .pull-left[ ```r fisheries %>% select(country) ``` ``` ## # A tibble: 82 x 1 ## country ## <chr> ## 1 Angola ## 2 Argentina ## 3 Australia ## 4 Bangladesh ## 5 Brazil ## 6 Cambodia ## # β¦ with 76 more rows ``` ] .pull-right[ ```r continents ``` ``` ## # A tibble: 245 x 2 ## country continent ## <chr> <chr> ## 1 Afghanistan Asia ## 2 Γ land Islands Europe ## 3 Albania Europe ## 4 Algeria Africa ## 5 American Samoa Oceania ## 6 Andorra Europe ## # β¦ with 239 more rows ``` ] --- ## Joining data frames ``` something_join(x, y) ``` - `inner_join()`: all rows from x where there are matching values in y, return all combination of multiple matches in the case of multiple matches - `left_join()`: all rows from x - `right_join()`: all rows from y - `full_join()`: all rows from both x and y - `semi_join()`: all rows from x where there are matching values in y, keeping just columns from x. - `anti_join()`: return all rows from x where there are not matching values in y, never duplicate rows of x - ... --- ## Setup For the next few slides... .pull-left[ ```r x ``` ``` ## # A tibble: 3 x 1 ## value ## <dbl> ## 1 1 ## 2 2 ## 3 3 ``` ] .pull-right[ ```r y ``` ``` ## # A tibble: 3 x 1 ## value ## <dbl> ## 1 1 ## 2 2 ## 3 4 ``` ] --- ## `inner_join()` .pull-left[ ```r inner_join(x, y) ``` ``` ## Joining, by = "value" ``` ``` ## # A tibble: 2 x 1 ## value ## * <dbl> ## 1 1 ## 2 2 ``` ] .pull-right[ ![](img/inner-join.gif)<!-- --> ] --- ## `left_join()` .pull-left[ ```r left_join(x, y) ``` ``` ## Joining, by = "value" ``` ``` ## # A tibble: 3 x 1 ## value ## * <dbl> ## 1 1 ## 2 2 ## 3 3 ``` ] .pull-right[ ![](img/left-join.gif)<!-- --> ] --- ## `right_join()` .pull-left[ ```r right_join(x, y) ``` ``` ## Joining, by = "value" ``` ``` ## # A tibble: 3 x 1 ## value ## * <dbl> ## 1 1 ## 2 2 ## 3 4 ``` ] .pull-right[ ![](img/right-join.gif)<!-- --> ] --- ## `full_join()` .pull-left[ ```r full_join(x, y) ``` ``` ## Joining, by = "value" ``` ``` ## # A tibble: 4 x 1 ## value ## * <dbl> ## 1 1 ## 2 2 ## 3 3 ## 4 4 ``` ] .pull-right[ ![](img/full-join.gif)<!-- --> ] --- ## `semi_join()` .pull-left[ ```r semi_join(x, y) ``` ``` ## Joining, by = "value" ``` ``` ## # A tibble: 2 x 1 ## value ## <dbl> ## 1 1 ## 2 2 ``` ] .pull-right[ ![](img/semi-join.gif)<!-- --> ] --- ## `anti_join()` .pull-left[ ```r anti_join(x, y) ``` ``` ## Joining, by = "value" ``` ``` ## # A tibble: 1 x 1 ## value ## <dbl> ## 1 3 ``` ] .pull-right[ ![](img/anti-join.gif)<!-- --> ] --- .discussion[ We want to keep all rows and columns from `fisheries` and add a column for corresponding continents. Which join function should we use? ] .pull-left[ ```r fisheries %>% select(country) ``` ``` ## # A tibble: 82 x 1 ## country ## <chr> ## 1 Angola ## 2 Argentina ## 3 Australia ## 4 Bangladesh ## 5 Brazil ## 6 Cambodia ## # β¦ with 76 more rows ``` ] .pull-right[ ```r continents ``` ``` ## # A tibble: 245 x 2 ## country continent ## <chr> <chr> ## 1 Afghanistan Asia ## 2 Γ land Islands Europe ## 3 Albania Europe ## 4 Algeria Africa ## 5 American Samoa Oceania ## 6 Andorra Europe ## # β¦ with 239 more rows ``` ] --- ## Join fisheries and continents ```r fisheries <- left_join(fisheries, continents) ``` ``` ## Joining, by = "country" ``` -- .discussion[ How does `left_join()` know to join the two data frames by `country`? ] Hint: - Variables in the original fisheries dataset: ``` ## [1] "country" "capture" "aquaculture" "total" ``` - Variables in the continents dataset: ``` ## [1] "country" "continent" ``` --- ## Check the data ```r fisheries %>% filter(is.na(continent)) ``` ``` ## # A tibble: 3 x 5 ## country capture aquaculture total continent ## <chr> <dbl> <dbl> <dbl> <chr> ## 1 Democratic Republic of the Congo 237372 3161 240533 <NA> ## 2 Hong Kong 142775 4258 147033 <NA> ## 3 Myanmar 2072390 1017644 3090034 <NA> ``` --- ## Implement fixes ```r fisheries <- fisheries %>% mutate(continent = case_when( country == "Democratic Republic of the Congo" ~ "Africa", country == "Hong Kong" ~ "Asia", country == "Myanmar" ~ "Asia", TRUE ~ continent ) ) ``` ...and check again ```r fisheries %>% filter(is.na(continent)) ``` ``` ## # A tibble: 0 x 5 ## # β¦ with 5 variables: country <chr>, capture <dbl>, aquaculture <dbl>, total <dbl>, ## # continent <chr> ``` --- .discussion[ What does the following code do? ] ```r fisheries %>% mutate(aquaculture_perc = aquaculture / total) ``` --- .your-turn[ - Go to RStudio Cloud and start the second assignment: `04 - Join Data` - Open the first R Markdown file: `fisheries.Rmd` - Knit the document and work on the Exercise 1. ]
10
:
00
.footnote[ RStudio Cloud workspace for this bootcamp is at [rstd.io/bootcamper-cloud](https://rstd.io/bootcamper-cloud). ] --- .your-turn[ - Go to RStudio Cloud and start the second assignment: `04 - Join Data` - Open the first R Markdown file: `fisheries.Rmd` - Knit the document and work on the Exercises 2 - 4. ]
10
:
00
.footnote[ RStudio Cloud workspace for this bootcamp is at [rstd.io/bootcamper-cloud](https://rstd.io/bootcamper-cloud). ] --- ## Visualize continent summary stats ```r ggplot(fisheries_summary, aes(x = continent, y = mean_ap)) + geom_col() ``` ![](04-join-data_files/figure-html/unnamed-chunk-42-1.png)<!-- --> --- ## Improve visualization ```r ggplot(fisheries_summary, * aes(x = fct_reorder(continent, mean_ap), y = mean_ap)) + geom_col() ``` ![](04-join-data_files/figure-html/unnamed-chunk-43-1.png)<!-- --> --- ## Improve visualization further ```r ggplot(fisheries_summary, aes(y = fct_reorder(continent, mean_ap), x = mean_ap)) + geom_col() + * scale_x_continuous(labels = percent) + * labs( * x = "", * y = "", * title = "Average share of aquaculture by continent", * subtitle = "out of total fisheries harvest, 2016", * caption = "Source: bit.ly/2VrawTt" * ) + * theme_minimal() ``` `\(\rightarrow\)` See next slide... --- ![](04-join-data_files/figure-html/unnamed-chunk-45-1.png)<!-- --> --- .huge-pink[goal:] .hand-blue[map fishery data at the country level] --- ## Mapping the fisheries data - Obtain country boundaries and store as a data frame - Join the fisheries and country boundaries data frames - Plot the country boundaries, and fill by fisheries harvest data --- ## `map_data()` The `map_data()` function turns data from the **maps** package into a data frame suitable for plotting with ggplot2: ```r map_data("world") ``` ``` ## long lat group order region subregion ## 1 -69.89912 12.45200 1 1 Aruba <NA> ## 2 -69.89571 12.42300 1 2 Aruba <NA> ## 3 -69.94219 12.43853 1 3 Aruba <NA> ## 4 -70.00415 12.50049 1 4 Aruba <NA> ## 5 -70.06612 12.54697 1 5 Aruba <NA> ## 6 -70.05088 12.59707 1 6 Aruba <NA> ## 7 -70.03511 12.61411 1 7 Aruba <NA> ## 8 -69.97314 12.56763 1 8 Aruba <NA> ## 9 -69.91181 12.48047 1 9 Aruba <NA> ## 10 -69.89912 12.45200 1 10 Aruba <NA> ## 12 74.89131 37.23164 2 12 Afghanistan <NA> ## 13 74.84023 37.22505 2 13 Afghanistan <NA> ## 14 74.76738 37.24917 2 14 Afghanistan <NA> ## 15 74.73896 37.28564 2 15 Afghanistan <NA> ``` --- ## Connect the dots <img src="img/connect-the-dots-australia.png" width="50%" style="display: block; margin: auto;" /> --- ## A few fixes for better matching .discussion[ What does the following code do? ] ```r world_map <- map_data("world") %>% mutate(region = case_when( region == "UK" ~ "United Kingdom", region == "USA" ~ "United States", subregion == "Hong Kong" ~ "Hong Kong", TRUE ~ region ) ) ``` --- ## Map the world .midi[ ```r ggplot(world_map, aes(x = long, y = lat, group = group)) + geom_polygon(fill = "gray") + theme_minimal() ``` ![](04-join-data_files/figure-html/unnamed-chunk-50-1.png)<!-- --> ] --- ## Join fisheries and world map .pull-left[ ```r fisheries %>% select(country) ``` ``` ## # A tibble: 82 x 1 ## country ## <chr> ## 1 Angola ## 2 Argentina ## 3 Australia ## 4 Bangladesh ## 5 Brazil ## 6 Cambodia ## # ⦠with 76 more rows ``` ] .pull-right[ ```r world_map %>% select(region) ``` ``` ## region ## 1 Aruba ## 2 Aruba ## 3 Aruba ## 4 Aruba ## 5 Aruba ## 6 Aruba ## 7 Aruba ## 8 Aruba ## 9 Aruba ## 10 Aruba ## 12 Afghanistan ## 13 Afghanistan ## 14 Afghanistan ## 15 Afghanistan ## 16 Afghanistan ## 17 Afghanistan ## 18 Afghanistan ## 19 Afghanistan ## 20 Afghanistan ## 21 Afghanistan ## 22 Afghanistan ## 23 Afghanistan ## 24 Afghanistan ## 25 Afghanistan ## 26 Afghanistan ## 27 Afghanistan ## 28 Afghanistan ## 29 Afghanistan ## 30 Afghanistan ## 31 Afghanistan ## 32 Afghanistan ## 33 Afghanistan ## 34 Afghanistan ## 35 Afghanistan ## 36 Afghanistan ## 37 Afghanistan ## 38 Afghanistan ## 39 Afghanistan ## 40 Afghanistan ## 41 Afghanistan ## 42 Afghanistan ## 43 Afghanistan ## 44 Afghanistan ## 45 Afghanistan ## 46 Afghanistan ## 47 Afghanistan ## 48 Afghanistan ## 49 Afghanistan ## 50 Afghanistan ## 51 Afghanistan ## 52 Afghanistan ## 53 Afghanistan ## 54 Afghanistan ## 55 Afghanistan ## 56 Afghanistan ## 57 Afghanistan ## 58 Afghanistan ## 59 Afghanistan ## 60 Afghanistan ## 61 Afghanistan ## 62 Afghanistan ## 63 Afghanistan ## 64 Afghanistan ## 65 Afghanistan ## 66 Afghanistan ## 67 Afghanistan ## 68 Afghanistan ## 69 Afghanistan ## 70 Afghanistan ## 71 Afghanistan ## 72 Afghanistan ## 73 Afghanistan ## 74 Afghanistan ## 75 Afghanistan ## 76 Afghanistan ## 77 Afghanistan ## 78 Afghanistan ## 79 Afghanistan ## 80 Afghanistan ## 81 Afghanistan ## 82 Afghanistan ## 83 Afghanistan ## 84 Afghanistan ## 85 Afghanistan ## 86 Afghanistan ## 87 Afghanistan ## 88 Afghanistan ## 89 Afghanistan ## 90 Afghanistan ## 91 Afghanistan ## 92 Afghanistan ## 93 Afghanistan ## 94 Afghanistan ## 95 Afghanistan ## 96 Afghanistan ## 97 Afghanistan ## 98 Afghanistan ## 99 Afghanistan ## 100 Afghanistan ## 101 Afghanistan ## 102 Afghanistan ## 103 Afghanistan ## 104 Afghanistan ## 105 Afghanistan ## 106 Afghanistan ## 107 Afghanistan ## 108 Afghanistan ## 109 Afghanistan ## 110 Afghanistan ## 111 Afghanistan ## 112 Afghanistan ## 113 Afghanistan ## 114 Afghanistan ## 115 Afghanistan ## 116 Afghanistan ## 117 Afghanistan ## 118 Afghanistan ## 119 Afghanistan ## 120 Afghanistan ## 121 Afghanistan ## 122 Afghanistan ## 123 Afghanistan ## 124 Afghanistan ## 125 Afghanistan ## 126 Afghanistan ## 127 Afghanistan ## 128 Afghanistan ## 129 Afghanistan ## 130 Afghanistan ## 131 Afghanistan ## 132 Afghanistan ## 133 Afghanistan ## 134 Afghanistan ## 135 Afghanistan ## 136 Afghanistan ## 137 Afghanistan ## 138 Afghanistan ## 139 Afghanistan ## 140 Afghanistan ## 141 Afghanistan ## 142 Afghanistan ## 143 Afghanistan ## 144 Afghanistan ## 145 Afghanistan ## 146 Afghanistan ## 147 Afghanistan ## 148 Afghanistan ## 149 Afghanistan ## 150 Afghanistan ## 151 Afghanistan ## 152 Afghanistan ## 153 Afghanistan ## 154 Afghanistan ## 155 Afghanistan ## 156 Afghanistan ## 157 Afghanistan ## 158 Afghanistan ## 159 Afghanistan ## 160 Afghanistan ## 161 Afghanistan ## 162 Afghanistan ## 163 Afghanistan ## 164 Afghanistan ## 165 Afghanistan ## 166 Afghanistan ## 167 Afghanistan ## 168 Afghanistan ## 169 Afghanistan ## 170 Afghanistan ## 171 Afghanistan ## 172 Afghanistan ## 173 Afghanistan ## 174 Afghanistan ## 175 Afghanistan ## 176 Afghanistan ## 177 Afghanistan ## 178 Afghanistan ## 179 Afghanistan ## 180 Afghanistan ## 181 Afghanistan ## 182 Afghanistan ## 183 Afghanistan ## 184 Afghanistan ## 185 Afghanistan ## 186 Afghanistan ## 187 Afghanistan ## 188 Afghanistan ## 189 Afghanistan ## 190 Afghanistan ## 191 Afghanistan ## 192 Afghanistan ## 193 Afghanistan ## 194 Afghanistan ## 195 Afghanistan ## 196 Afghanistan ## 197 Afghanistan ## 198 Afghanistan ## 199 Afghanistan ## 200 Afghanistan ## 201 Afghanistan ## 202 Afghanistan ## 203 Afghanistan ## 204 Afghanistan ## 205 Afghanistan ## 206 Afghanistan ## 207 Afghanistan ## 208 Afghanistan ## 209 Afghanistan ## 210 Afghanistan ## 211 Afghanistan ## 212 Afghanistan ## 213 Afghanistan ## 214 Afghanistan ## 215 Afghanistan ## 216 Afghanistan ## 217 Afghanistan ## 218 Afghanistan ## 219 Afghanistan ## 220 Afghanistan ## 221 Afghanistan ## 222 Afghanistan ## 223 Afghanistan ## 224 Afghanistan ## 225 Afghanistan ## 226 Afghanistan ## 227 Afghanistan ## 228 Afghanistan ## 229 Afghanistan ## 230 Afghanistan ## 231 Afghanistan ## 232 Afghanistan ## 233 Afghanistan ## 234 Afghanistan ## 235 Afghanistan ## 236 Afghanistan ## 237 Afghanistan ## 238 Afghanistan ## 239 Afghanistan ## 240 Afghanistan ## 241 Afghanistan ## 242 Afghanistan ## 243 Afghanistan ## 244 Afghanistan ## 245 Afghanistan ## 246 Afghanistan ## 247 Afghanistan ## 248 Afghanistan ## 249 Afghanistan ## 250 Afghanistan ## 251 Afghanistan ## 252 Afghanistan ## 253 Afghanistan ## 254 Afghanistan ## 255 Afghanistan ## 256 Afghanistan ## 257 Afghanistan ## 258 Afghanistan ## 259 Afghanistan ## 260 Afghanistan ## 261 Afghanistan ## 262 Afghanistan ## 263 Afghanistan ## 264 Afghanistan ## 265 Afghanistan ## 266 Afghanistan ## 267 Afghanistan ## 268 Afghanistan ## 269 Afghanistan ## 270 Afghanistan ## 271 Afghanistan ## 272 Afghanistan ## 273 Afghanistan ## 274 Afghanistan ## 275 Afghanistan ## 276 Afghanistan ## 277 Afghanistan ## 278 Afghanistan ## 279 Afghanistan ## 280 Afghanistan ## 281 Afghanistan ## 282 Afghanistan ## 283 Afghanistan ## 284 Afghanistan ## 285 Afghanistan ## 286 Afghanistan ## 287 Afghanistan ## 288 Afghanistan ## 289 Afghanistan ## 290 Afghanistan ## 291 Afghanistan ## 292 Afghanistan ## 293 Afghanistan ## 294 Afghanistan ## 295 Afghanistan ## 296 Afghanistan ## 297 Afghanistan ## 298 Afghanistan ## 299 Afghanistan ## 300 Afghanistan ## 301 Afghanistan ## 302 Afghanistan ## 303 Afghanistan ## 304 Afghanistan ## 305 Afghanistan ## 306 Afghanistan ## 307 Afghanistan ## 308 Afghanistan ## 309 Afghanistan ## 310 Afghanistan ## 311 Afghanistan ## 312 Afghanistan ## 313 Afghanistan ## 314 Afghanistan ## 315 Afghanistan ## 316 Afghanistan ## 317 Afghanistan ## 318 Afghanistan ## 319 Afghanistan ## 320 Afghanistan ## 321 Afghanistan ## 322 Afghanistan ## 323 Afghanistan ## 324 Afghanistan ## 325 Afghanistan ## 326 Afghanistan ## 327 Afghanistan ## 328 Afghanistan ## 329 Afghanistan ## 330 Afghanistan ## 331 Afghanistan ## 332 Afghanistan ## 333 Afghanistan ## 334 Afghanistan ## 335 Afghanistan ## 336 Afghanistan ## 337 Afghanistan ## 338 Afghanistan ## 339 Afghanistan ## 340 Afghanistan ## 341 Afghanistan ## 342 Afghanistan ## 343 Afghanistan ## 344 Afghanistan ## 345 Afghanistan ## 346 Afghanistan ## 347 Afghanistan ## 348 Afghanistan ## 349 Afghanistan ## 350 Afghanistan ## 351 Afghanistan ## 352 Afghanistan ## 353 Afghanistan ## 354 Afghanistan ## 355 Afghanistan ## 356 Afghanistan ## 357 Afghanistan ## 358 Afghanistan ## 359 Afghanistan ## 360 Afghanistan ## 361 Afghanistan ## 362 Afghanistan ## 363 Afghanistan ## 364 Afghanistan ## 365 Afghanistan ## 366 Afghanistan ## 367 Afghanistan ## 368 Afghanistan ## 369 Afghanistan ## 370 Afghanistan ## 371 Afghanistan ## 372 Afghanistan ## 373 Afghanistan ## 374 Afghanistan ## 375 Afghanistan ## 376 Afghanistan ## 377 Afghanistan ## 378 Afghanistan ## 379 Afghanistan ## 380 Afghanistan ## 381 Afghanistan ## 382 Afghanistan ## 383 Afghanistan ## 384 Afghanistan ## 385 Afghanistan ## 386 Afghanistan ## 387 Afghanistan ## 388 Afghanistan ## 389 Afghanistan ## 390 Afghanistan ## 391 Afghanistan ## 392 Afghanistan ## 393 Afghanistan ## 394 Afghanistan ## 395 Afghanistan ## 396 Afghanistan ## 397 Afghanistan ## 398 Afghanistan ## 399 Afghanistan ## 400 Afghanistan ## 401 Afghanistan ## 402 Afghanistan ## 403 Afghanistan ## 404 Afghanistan ## 405 Afghanistan ## 406 Afghanistan ## 407 Afghanistan ## 408 Afghanistan ## 409 Afghanistan ## 410 Afghanistan ## 411 Afghanistan ## 412 Afghanistan ## 413 Afghanistan ## 414 Afghanistan ## 415 Afghanistan ## 416 Afghanistan ## 417 Afghanistan ## 418 Afghanistan ## 419 Afghanistan ## 420 Afghanistan ## 421 Afghanistan ## 423 Angola ## 424 Angola ## 425 Angola ## 426 Angola ## 427 Angola ## 428 Angola ## 429 Angola ## 430 Angola ## 431 Angola ## 432 Angola ## 433 Angola ## 434 Angola ## 435 Angola ## 436 Angola ## 437 Angola ## 438 Angola ## 439 Angola ## 440 Angola ## 441 Angola ## 442 Angola ## 443 Angola ## 444 Angola ## 445 Angola ## 446 Angola ## 447 Angola ## 448 Angola ## 449 Angola ## 450 Angola ## 451 Angola ## 452 Angola ## 453 Angola ## 454 Angola ## 455 Angola ## 456 Angola ## 457 Angola ## 458 Angola ## 459 Angola ## 460 Angola ## 461 Angola ## 462 Angola ## 463 Angola ## 464 Angola ## 465 Angola ## 466 Angola ## 467 Angola ## 468 Angola ## 469 Angola ## 470 Angola ## 471 Angola ## 472 Angola ## 473 Angola ## 474 Angola ## 475 Angola ## 476 Angola ## 477 Angola ## 478 Angola ## 479 Angola ## 480 Angola ## 481 Angola ## 482 Angola ## 483 Angola ## 484 Angola ## 485 Angola ## 486 Angola ## 487 Angola ## 488 Angola ## 489 Angola ## 490 Angola ## 491 Angola ## 492 Angola ## 493 Angola ## 494 Angola ## 495 Angola ## 496 Angola ## 497 Angola ## 498 Angola ## 499 Angola ## 500 Angola ## 501 Angola ## 502 Angola ## 503 Angola ## 504 Angola ## 505 Angola ## 506 Angola ## 507 Angola ## 508 Angola ## 509 Angola ## 510 Angola ## 511 Angola ## 512 Angola ## 513 Angola ## 514 Angola ## 515 Angola ## 516 Angola ## 517 Angola ## 518 Angola ## 519 Angola ## 520 Angola ## 521 Angola ## 522 Angola ## 523 Angola ## 524 Angola ## 525 Angola ## 526 Angola ## 527 Angola ## 528 Angola ## 529 Angola ## 530 Angola ## 531 Angola ## 532 Angola ## 533 Angola ## 534 Angola ## 535 Angola ## 536 Angola ## 537 Angola ## 538 Angola ## 539 Angola ## 540 Angola ## 541 Angola ## 542 Angola ## 543 Angola ## 544 Angola ## 545 Angola ## 546 Angola ## 547 Angola ## 548 Angola ## 549 Angola ## 550 Angola ## 551 Angola ## 552 Angola ## 553 Angola ## 554 Angola ## 555 Angola ## 556 Angola ## 557 Angola ## 558 Angola ## 559 Angola ## 560 Angola ## 561 Angola ## 562 Angola ## 563 Angola ## 564 Angola ## 565 Angola ## 566 Angola ## 567 Angola ## 568 Angola ## 569 Angola ## 570 Angola ## 571 Angola ## 572 Angola ## 573 Angola ## 574 Angola ## 575 Angola ## 576 Angola ## 577 Angola ## 578 Angola ## 579 Angola ## 580 Angola ## 581 Angola ## 582 Angola ## 583 Angola ## 584 Angola ## 585 Angola ## 586 Angola ## 587 Angola ## 588 Angola ## 589 Angola ## 590 Angola ## 591 Angola ## 592 Angola ## 593 Angola ## 594 Angola ## 595 Angola ## 596 Angola ## 597 Angola ## 598 Angola ## 599 Angola ## 600 Angola ## 601 Angola ## 602 Angola ## 603 Angola ## 604 Angola ## 605 Angola ## 606 Angola ## 607 Angola ## 608 Angola ## 609 Angola ## 610 Angola ## 611 Angola ## 612 Angola ## 613 Angola ## 614 Angola ## 615 Angola ## 616 Angola ## 617 Angola ## 618 Angola ## 619 Angola ## 620 Angola ## 621 Angola ## 622 Angola ## 623 Angola ## 624 Angola ## 625 Angola ## 626 Angola ## 627 Angola ## 628 Angola ## 629 Angola ## 630 Angola ## 631 Angola ## 632 Angola ## 633 Angola ## 634 Angola ## 635 Angola ## 636 Angola ## 637 Angola ## 638 Angola ## 639 Angola ## 640 Angola ## 641 Angola ## 642 Angola ## 643 Angola ## 644 Angola ## 645 Angola ## 646 Angola ## 647 Angola ## 648 Angola ## 649 Angola ## 650 Angola ## 651 Angola ## 652 Angola ## 653 Angola ## 654 Angola ## 655 Angola ## 656 Angola ## 657 Angola ## 658 Angola ## 659 Angola ## 660 Angola ## 661 Angola ## 662 Angola ## 663 Angola ## 664 Angola ## 665 Angola ## 666 Angola ## 667 Angola ## 668 Angola ## 669 Angola ## 670 Angola ## 671 Angola ## 672 Angola ## 673 Angola ## 674 Angola ## 675 Angola ## 676 Angola ## 677 Angola ## 678 Angola ## 679 Angola ## 680 Angola ## 681 Angola ## 682 Angola ## 683 Angola ## 684 Angola ## 685 Angola ## 686 Angola ## 687 Angola ## 688 Angola ## 689 Angola ## 690 Angola ## 691 Angola ## 692 Angola ## 693 Angola ## 694 Angola ## 695 Angola ## 696 Angola ## 697 Angola ## 698 Angola ## 699 Angola ## 700 Angola ## 701 Angola ## 702 Angola ## 703 Angola ## 704 Angola ## 705 Angola ## 706 Angola ## 707 Angola ## 708 Angola ## 709 Angola ## 710 Angola ## 711 Angola ## 712 Angola ## 713 Angola ## 714 Angola ## 715 Angola ## 716 Angola ## 717 Angola ## 718 Angola ## 720 Angola ## 721 Angola ## 722 Angola ## 723 Angola ## 724 Angola ## 725 Angola ## 726 Angola ## 727 Angola ## 728 Angola ## 729 Angola ## 730 Angola ## 731 Angola ## 732 Angola ## 733 Angola ## 734 Angola ## 735 Angola ## 736 Angola ## 737 Angola ## 738 Angola ## 739 Angola ## 740 Angola ## 741 Angola ## 742 Angola ## 743 Angola ## 744 Angola ## 745 Angola ## 746 Angola ## 747 Angola ## 748 Angola ## 749 Angola ## 750 Angola ## 751 Angola ## 752 Angola ## 753 Angola ## 754 Angola ## 755 Angola ## 756 Angola ## 757 Angola ## 758 Angola ## 759 Angola ## 760 Angola ## 761 Angola ## 763 Anguilla ## 764 Anguilla ## 765 Anguilla ## 766 Anguilla ## 767 Anguilla ## 768 Anguilla ## 770 Albania ## 771 Albania ## 772 Albania ## 773 Albania ## 774 Albania ## 775 Albania ## 776 Albania ## 777 Albania ## 778 Albania ## 779 Albania ## 780 Albania ## 781 Albania ## 782 Albania ## 783 Albania ## 784 Albania ## 785 Albania ## 786 Albania ## 787 Albania ## 788 Albania ## 789 Albania ## 790 Albania ## 791 Albania ## 792 Albania ## 793 Albania ## 794 Albania ## 795 Albania ## 796 Albania ## 797 Albania ## 798 Albania ## 799 Albania ## 800 Albania ## 801 Albania ## 802 Albania ## 803 Albania ## 804 Albania ## 805 Albania ## 806 Albania ## 807 Albania ## 808 Albania ## 809 Albania ## 810 Albania ## 811 Albania ## 812 Albania ## 813 Albania ## 814 Albania ## 815 Albania ## 816 Albania ## 817 Albania ## 818 Albania ## 819 Albania ## 820 Albania ## 821 Albania ## 822 Albania ## 823 Albania ## 824 Albania ## 825 Albania ## 826 Albania ## 827 Albania ## 828 Albania ## 829 Albania ## 830 Albania ## 831 Albania ## 832 Albania ## 833 Albania ## 834 Albania ## 835 Albania ## 836 Albania ## 837 Albania ## 838 Albania ## 839 Albania ## 840 Albania ## 841 Albania ## 842 Albania ## 843 Albania ## 844 Albania ## 845 Albania ## 846 Albania ## 847 Albania ## 848 Albania ## 849 Albania ## 850 Albania ## 851 Albania ## 852 Albania ## 853 Albania ## 854 Albania ## 855 Albania ## 856 Albania ## 857 Albania ## 858 Albania ## 859 Albania ## 860 Albania ## 861 Albania ## 862 Albania ## 863 Albania ## 864 Albania ## 865 Albania ## 866 Albania ## 867 Albania ## 868 Albania ## 869 Albania ## 870 Albania ## 871 Albania ## 872 Albania ## 873 Albania ## 874 Albania ## 875 Albania ## 876 Albania ## 877 Albania ## 878 Albania ## 879 Albania ## 880 Albania ## 881 Albania ## 882 Albania ## 884 Finland ## 885 Finland ## 886 Finland ## 887 Finland ## 888 Finland ## 889 Finland ## 890 Finland ## 891 Finland ## 892 Finland ## 893 Finland ## 895 Finland ## 896 Finland ## 897 Finland ## 898 Finland ## 899 Finland ## 900 Finland ## 901 Finland ## 902 Finland ## 903 Finland ## 904 Finland ## 906 Finland ## 907 Finland ## 908 Finland ## 909 Finland ## 910 Finland ## 911 Finland ## 912 Finland ## 913 Finland ## 914 Finland ## 915 Finland ## 916 Finland ## 917 Finland ## 918 Finland ## 919 Finland ## 920 Finland ## 921 Finland ## 922 Finland ## 923 Finland ## 924 Finland ## 925 Finland ## 926 Finland ## 927 Finland ## 928 Finland ## 929 Finland ## 930 Finland ## 931 Finland ## 932 Finland ## 933 Finland ## 934 Finland ## 935 Finland ## 936 Finland ## 937 Finland ## 939 Andorra ## 940 Andorra ## 941 Andorra ## 942 Andorra ## 943 Andorra ## 944 Andorra ## 945 Andorra ## 946 Andorra ## 947 Andorra ## 948 Andorra ## 949 Andorra ## 950 Andorra ## 951 Andorra ## 952 Andorra ## 953 Andorra ## 954 Andorra ## 955 Andorra ## 956 Andorra ## 957 Andorra ## 959 United Arab Emirates ## 960 United Arab Emirates ## 961 United Arab Emirates ## 962 United Arab Emirates ## 963 United Arab Emirates ## 964 United Arab Emirates ## 965 United Arab Emirates ## 966 United Arab Emirates ## 967 United Arab Emirates ## 968 United Arab Emirates ## 970 United Arab Emirates ## 971 United Arab Emirates ## 972 United Arab Emirates ## 973 United Arab Emirates ## 974 United Arab Emirates ## 975 United Arab Emirates ## 976 United Arab Emirates ## 978 United Arab Emirates ## 979 United Arab Emirates ## 980 United Arab Emirates ## 981 United Arab Emirates ## 982 United Arab Emirates ## 983 United Arab Emirates ## 984 United Arab Emirates ## 985 United Arab Emirates ## 986 United Arab Emirates ## 987 United Arab Emirates ## 989 United Arab Emirates ## 990 United Arab Emirates ## 991 United Arab Emirates ## 992 United Arab Emirates ## 993 United Arab Emirates ## 994 United Arab Emirates ## 995 United Arab Emirates ## 996 United Arab Emirates ## 997 United Arab Emirates ## 999 United Arab Emirates ## 1000 United Arab Emirates ## 1001 United Arab Emirates ## 1002 United Arab Emirates ## 1003 United Arab Emirates ## 1004 United Arab Emirates ## 1005 United Arab Emirates ## 1006 United Arab Emirates ## 1007 United Arab Emirates ## 1008 United Arab Emirates ## 1009 United Arab Emirates ## 1010 United Arab Emirates ## 1011 United Arab Emirates ## 1012 United Arab Emirates ## 1013 United Arab Emirates ## 1014 United Arab Emirates ## [ reached 'max' / getOption("max.print") -- omitted 98338 rows ] ``` ] --- ## Join fisheries and world map ```r fisheries_map <- left_join(fisheries, world_map, * by = c("country" = "region")) ``` ```r glimpse(fisheries_map) ``` ``` ## Rows: 72,685 ## Columns: 11 ## $ country <chr> "Angola", "Angola", "Angola", "Angola", "Angola", "Angola", "A⦠## $ capture <dbl> 486490, 486490, 486490, 486490, 486490, 486490, 486490, 486490⦠## $ aquaculture <dbl> 655, 655, 655, 655, 655, 655, 655, 655, 655, 655, 655, 655, 65⦠## $ total <dbl> 487145, 487145, 487145, 487145, 487145, 487145, 487145, 487145⦠## $ continent <chr> "Africa", "Africa", "Africa", "Africa", "Africa", "Africa", "A⦠## $ aquaculture_perc <dbl> 0.001344569, 0.001344569, 0.001344569, 0.001344569, 0.00134456⦠## $ long <dbl> 23.96650, 23.98828, 24.01006, 24.02559, 24.04141, 24.04668, 24⦠## $ lat <dbl> -10.87178, -11.00283, -11.18477, -11.31563, -11.37412, -11.405⦠## $ group <dbl> 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,⦠## $ order <int> 423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 43⦠## $ subregion <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA⦠``` --- ## Mapping fisheries .midi[ ```r ggplot(fisheries_map, mapping = aes(x = long, y = lat, group = group)) + geom_polygon(aes(fill = capture)) + scale_fill_viridis_c() + theme_minimal() ``` ![](04-join-data_files/figure-html/unnamed-chunk-55-1.png)<!-- --> ] --- .discussion[ What is misleading about the following map? ] <img src="04-join-data_files/figure-html/unnamed-chunk-56-1.png" style="display: block; margin: auto;" /> --- ## Putting it altogether ```r ggplot() + # layer 1 geom_polygon(world_map, mapping = aes(x = long, y = lat, group = group), * fill = "lightgray") + # layer 2 geom_polygon(fisheries_map, mapping = aes(x = long, y = lat, group = group, * fill = capture)) + scale_fill_viridis_c() + theme_minimal() + theme(legend.position = "bottom") + labs( x = "", y = "", title = "Fisheries harvest by capture, 2016", subtitle = "Capture measured in tonnes", caption = "Source: bit.ly/2VrawTt" ) ``` --- <img src="04-join-data_files/figure-html/unnamed-chunk-58-1.png" style="display: block; margin: auto;" /> --- ## Log scale ```r ggplot() + geom_polygon(world_map, mapping = aes(x = long, y = lat, group = group), fill = "lightgray") + geom_polygon(fisheries_map, mapping = aes(x = long, y = lat, group = group, * fill = log(capture))) + scale_fill_viridis_c() + theme_minimal() + theme(legend.position = "bottom") + labs( x = "", y = "", title = "Fisheries harvest by capture, 2016", subtitle = "Capture measured in logged tonnes", caption = "Source: bit.ly/2VrawTt" ) ``` --- <img src="04-join-data_files/figure-html/unnamed-chunk-60-1.png" style="display: block; margin: auto;" /> --- ## Aquaculture ```r ggplot() + geom_polygon(world_map, mapping = aes(x = long, y = lat, group = group), fill = "lightgray") + geom_polygon(fisheries_map, mapping = aes(x = long, y = lat, group = group, * fill = log(aquaculture+1))) + scale_fill_viridis_c() + theme_minimal() + theme(legend.position = "bottom") + labs( x = "", y = "", title = "Fisheries harvest by aquaculture, 2016", subtitle = "Aquaculture measured in logged tonnes", fill = "log(aquaculture)", caption = "Source: bit.ly/2VrawTt" ) ``` --- <img src="04-join-data_files/figure-html/unnamed-chunk-62-1.png" style="display: block; margin: auto;" /> --- <img src="04-join-data_files/figure-html/unnamed-chunk-63-1.png" style="display: block; margin: auto;" />