-
Notifications
You must be signed in to change notification settings - Fork 39
/
Lab2_assignment.Rmd
109 lines (84 loc) · 4.24 KB
/
Lab2_assignment.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
---
title: "Lab2_assignment"
author: "Matt Harris"
date: "9/8/2021"
output: github_document
editor_options:
markdown:
wrap: sentence
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r setup_package, warning = FALSE, message = FALSE}
library(tidyverse)
library(tidycensus)
library(sf)
library(tmap) # mapping, install if you don't have it
set.seed(717)
```
This assignment if for you to complete a short version of the lab notes, but you have to complete a number of the steps yourself.
You will then knit this to a markdown (not an HTML) and push it to your GitHub repo.
Unlike HTML, the RMarkdown knit to `github_document` can be viewed directly on GitHub.
You will them email your lab instructor with a link to your repo.
Steps in this assignment:
1. Make sure you have successfully read, run, and learned from the `MUSA_508_Lab2_sf.Rmd` Rmarkdown
2. Find two new variables from the 2019 ACS data to load.
Use `vars <- load_variables(2019, "acs5")` and `View(vars)` to see all of the variable from that ACS.
Note that you should not pick something really obscure like count_38yo_cabinetmakers because you will get lots of NAs.
3. Pick a neighborhood of the City to map.
You will need to do some googling to figure this out.
Use the [PHL Track Explorer](https://data-phl.opendata.arcgis.com/datasets/census-tracts-2010/explore?location=40.002759%2C-75.119097%2C11.91) to get the `GEOID10` number from each parcel and add them to the `myTracts` object below.
This is just like what was done in the exercise, but with a different neighborhood of your choice.
Remember that all GEOIDs need to be 10-characters long.
4. In the first code chunk you will do that above and then edit the call-outs in the dplyr pipe sequence to `rename` and `mutate` your data.
5. You will transform the data to `WGS84` by adding the correct EPSG code.
This is discussed heavily in the exercise.
6. You will produce a map of one of the variables you picked and highlight the neighborhood you picked.
There are call-out within the `ggplot` code for you to edit.
7. You can run the code chunks and lines of code as you edit to make sure everything works.
8. Once you are done, hit the `knit` button at the top of the script window (little blue knitting ball) and you will see the output.
Once it is what you want...
9. Use the `Git` tab on the bottom left of right (depending on hour your Rstudio is laid out) and click the check box to `stage` all of your changes, write a commit note, hit the `commit` button, and then the `Push` button to push it to Github.
10. Check your Github repo to see you work in the cloud.
11. Email your lab instructor with a link!
12. Congrats!
You made a map in code!
## Load data from {tidycensus}
```{r acs_vars, cache = TRUE, message = FALSE, warning = FALSE, results=FALSE}
acs_vars <- c( "[PUT YOUR VAR CODES HERE]")
myTracts <- c("[YOUR TRACTS GEOIDs HERE]")
acsTractsPHL.2019.sf <- get_acs(geography = "tract",
year = 2019,
variables = acs_vars,
geometry = TRUE,
state = "PA",
county = "Philadelphia",
output = "wide") %>%
dplyr::select (GEOID, NAME, all_of(paste0(acs_vars,"E"))) %>%
rename ("[RENAME YOUR VARIABLES HERE]") %>%
mutate(Neighborhood = ifelse(GEOID %in% myTracts,
"[YOUR NEIGHBORHOOD NAME HERE]",
"REST OF PHILADELPHIA"))
```
## Transform to WGS84 with {sf}
```{r}
acsTractsPHL.2019.sf <- acsTractsPHL.2019.sf %>%
st_transform(crs = "[PUT THE WGS84 EPSG CODE HERE]")
```
## Plot with {ggplot2}
```{r ggplot_geom_sf, warning = FALSE, echo = FALSE}
ggplot()+
geom_sf(data = acsTractsPHL.2019.sf, aes(fill = "[YOUR VARIABLE NAME HERE]"),
color = "transparent")+
geom_sf(data = acsTractsPHL.2019.sf %>%
filter(Neighborhood == "[YOUR NEIGHBORHOOD NAME HERE]") %>%
st_union(),
color = "white",
fill = "transparent")+
# Bonus to figure out different color ramps with scale_fill_viridis()
labs(
title = "[YOUR TITLE HERE]",
subtitle = "[YOUR SUBTITLE HERE]",
caption = "[YOUR CAPTION HERE]")
```