class: center, middle, inverse, title-slide # Lab 4: Spatial Data and Merge Conflicts ### 09.20.2021 --- layout: true <div class="my-footer"> <span> <a href="https://introds.org" target="_blank">introds.org</a> </span> </div> --- ## Overview of tidy functions - pipe operator - used to link functions together - filter() - used to filter a dataframe by values of a variable - select() - select columns - mutate() - create a new column or overwrite an existing one - group_by() - create groups based on values of a column - summarise() - calculate specified summary statistic over a group(s) - pivot_wider() - each row has multiple observations for a certain group - pivot_longer() - each row is a completely unique observation - etc. [R4DS](https://r4ds.had.co.nz/introduction.html) is your best friend!! You can search for keywords and it will suggest pages that explain the functions Time for a quick demo! --- ## Getting Started Log in to GitHub to determine your team number and members for Lab 4. - Find a one hour block of time outside of class that you can use to work on the lab / project if needed. You may not need to use this, but it is good to have it in reserve. Tools like [Doodle](https://doodle.com/en/) and [When2Meet](https://www.when2meet.com/) are helpful. - Determine how your group will communicate (email, text, slack, discord, etc). --- # Merge conflicts <img src="04/merge-no-conflict.png" width="60%" style="display: block; margin: auto;" /> You may have seen this already through the course of your collaboration last week in Lab 03. When two collaborators make changes to a file and push the file to their repository, git merges these two files. --- <img src="04/merge-conflict.png" width="60%" style="display: block; margin: auto;" /> If these two files have conflicting content on the same line, git will produce a **merge conflict**. Merge conflicts need to be resolved manually, as they require a human intervention: --- <img src="04/merge-conflict-identifiers.png" width="60%" style="display: block; margin: auto;" /> To resolve the merge conflict, decide if you want to keep only your text/code, the text/code on GitHub, or incorporate changes from both sets. Delete the conflict markers `<<<<<<<`, `=======`, `>>>>>>>` and make the changes you want in the final merge. --- **Assign numbers 1, 2, and 3 to each of your team members** (if only 2 team members, members 1-2 can share the work of 3). Go through the following steps in detail, which simulate a merge conflict. Completing this exercise will be part of the lab grade. --- ## Resolving a merge conflict **Step 1**: *Everyone* clone your team lab and open the Rmd file. *Member 3* should look at the group’s repo on GitHub to ensure that the other members’ files are pushed to GitHub after every step. **Step 2**: *Member 1* should change the team name to your team name. Knit, commit, and push. **Step 3**: *Member 2* should change the team name to something different (i.e., not your team name). Knit, commit, and push. Member 2 should get an error on the attempted push. --- Pull and review the document with the merge conflict. Member 2 should display and read the error to the entire team. A merge conflict occurred because Member 2 edited the same part of the document as Member 1. Resolve the conflict with whichever name you want to keep (please keep your real team name), then knit, commit with a message that clearly states you fixed the merge conflict, and push again. **Step 4**: *Member 3* verifies the commit shows the merge conflict on GitHub. Then Member 3 writes some narrative below the last code chunk in your Rmd file. Knit, commit, and push. This time, no merge conflicts should occur, since you edited a different part of the document from Members 1 and 2. Member 3 should display and read the message to the entire team. Everyone pull and delete the narrative. All team members should have the same content in the Rmd file before proceeding to the exercises. --- ## Packages and Data ```r life <- readr::read_csv("04/lifeexpectancy_infant.csv") names(life) ``` ``` ## [1] "location" "sex" "year" "lifeexp" ``` --- ## R packages The R packages `ggplot2` and `sf` (for "simple features") have made it relatively straightforward to make great spatial maps. We'll use these packages along with `rnaturalearth` and `rnaturalearthdata` to access free spatial mapping tools for world maps. You'll need to install these yourself before proceeding. --- ```r # install.packages("rnaturalearth") # install.packages("rnaturalearthdata") # install.packages("sf") # install.packages("rgeos") # needed to pull countries for plot # ggplot2 is part of the tidyverse, so we don't have to call it library(sf) library(rnaturalearth) library(rnaturalearthdata) library(rgeos) library(scales) # the classic dark-on-light theme for ggplot2 is nice for maps theme_set(theme_bw()) # world contains the country information for plotting in addition to a lot of other information about the countries world <- ne_countries(scale = "medium", returnclass = "sf") names(world) ``` ``` ## [1] "scalerank" "featurecla" "labelrank" "sovereignt" ## [5] "sov_a3" "adm0_dif" "level" "type" ## [9] "admin" "adm0_a3" "geou_dif" "geounit" ## [13] "gu_a3" "su_dif" "subunit" "su_a3" ## [17] "brk_diff" "name" "name_long" "brk_a3" ## [21] "brk_name" "brk_group" "abbrev" "postal" ## [25] "formal_en" "formal_fr" "note_adm0" "note_brk" ## [29] "name_sort" "name_alt" "mapcolor7" "mapcolor8" ## [33] "mapcolor9" "mapcolor13" "pop_est" "gdp_md_est" ## [37] "pop_year" "lastcensus" "gdp_year" "economy" ## [41] "income_grp" "wikipedia" "fips_10" "iso_a2" ## [45] "iso_a3" "iso_n3" "un_a3" "wb_a2" ## [49] "wb_a3" "woe_id" "adm0_a3_is" "adm0_a3_us" ## [53] "adm0_a3_un" "adm0_a3_wb" "continent" "region_un" ## [57] "subregion" "region_wb" "name_len" "long_len" ## [61] "abbrev_len" "tiny" "homepart" "geometry" ``` --- Now let's get started! ```r ggplot(data=world) + geom_sf() ``` <img src="lab04slides_jd_edits_files/figure-html/firstworldplot-1.png" width="60%" style="display: block; margin: auto;" /> --- OK, that's the world (or one projection of it!). Let's dress up the map a bit. ```r ggplot(data = world) + geom_sf() + labs(x = "Longitude", y = "Latitude", title = "World Map") ``` <img src="lab04slides_jd_edits_files/figure-html/worldmap-1.png" width="60%" style="display: block; margin: auto;" /> --- The standard projection is a Mercator projection, with latitude and longitude at right angles. As you can see, this distorts the size of land masses at the poles. The ETRS89 Lambert Azimuthal Equal-Area projection is focused instead on Europe and Africa and does not force the map into a rectangular shape. ```r ggplot(data = world) + geom_sf() + coord_sf(crs = st_crs(3035)) + labs(title = "World Map") ``` <img src="lab04slides_jd_edits_files/figure-html/eurocentric-1.png" width="50%" style="display: block; margin: auto;" /> --- We could also center our map on Asia. ```r ggplot(data = world) + geom_sf() + coord_sf(crs = st_crs(8859)) + labs(title = "World Map") ``` <img src="lab04slides_jd_edits_files/figure-html/asiacentric-1.png" width="60%" style="display: block; margin: auto;" /> --- We can color the world Duke blue... ```r ggplot(data = world) + geom_sf(color="black", fill="#00539B") + labs(x = "Longitude", y = "Latitude", title = "World Map") ``` <img src="lab04slides_jd_edits_files/figure-html/worldmapblue-1.png" width="60%" style="display: block; margin: auto;" /> --- but perhaps it's more informative to use color to convey information. In this plot, color is mapped to the square root of population (you can remove trans="sqrt" to see why the square root is used). ```r ggplot(data = world) + geom_sf(aes(fill = pop_est)) + scale_fill_viridis_c(option = "plasma", #color scheme trans = "sqrt", #map color to sqrt(pop_est) labels = label_comma()) + #avoid 1e9 notation labs(x = "Longitude", y = "Latitude", fill = "Population", title = "World Map") ``` <img src="lab04slides_jd_edits_files/figure-html/worldmappop-1.png" width="30%" style="display: block; margin: auto;" /> Pretty nifty! --- class: middle ## You're now ready to complete the rest of lab! <br> ## Please raise your hand if you need help as you work on the lab.