Complete the problems below and submit your work as a knitted docx file.
library(devtools)
#devtools::install_github("tidyverse/ggplot2")
#require(ggplot2)
library(tidyverse)
## Loading tidyverse: ggplot2
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Warning: package 'tibble' was built under R version 3.3.3
## Warning: package 'purrr' was built under R version 3.3.3
## Conflicts with tidy packages ----------------------------------------------
## filter(): dplyr, stats
## lag(): dplyr, stats
library(tidycensus)
## Warning: package 'tidycensus' was built under R version 3.3.3
library(viridis)
## Warning: package 'viridis' was built under R version 3.3.3
## Loading required package: viridisLite
## Warning: package 'viridisLite' was built under R version 3.3.3
library(stringr)
## Warning: package 'stringr' was built under R version 3.3.3
library(rgdal)
## Warning: package 'rgdal' was built under R version 3.3.3
## Loading required package: sp
## Warning: package 'sp' was built under R version 3.3.3
## rgdal: version: 1.2-13, (SVN revision 686)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 2.2.0, released 2017/04/28
## Path to GDAL shared files: C:/Users/Tin/Documents/R/win-library/3.3/rgdal/gdal
## Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
## Path to PROJ.4 shared files: C:/Users/Tin/Documents/R/win-library/3.3/rgdal/proj
## Linking to sp version: 1.2-5
library(RColorBrewer)
library(leaflet)
## Warning: package 'leaflet' was built under R version 3.3.3
#census_api_key("7491cf955005ef7221778ebe1228115c53a12df6", install = TRUE)
Select any geographical area in the US and any numerical variable that interests you. Use Kyle Walker’s choropleth method to create a choropleth based on your choices. Describe your choices.
v15 <- load_variables(2015, "acs5", cache = TRUE)
options(tigris_use_cache = TRUE)
bentoninc <- get_acs(state = "WA", county = "Benton", geography = "tract", variables = "B19013_001", geometry = TRUE)
bentoninc %>%
mutate(NAME = str_replace(NAME,'Benton County, Washington','')) %>%
mutate(NAME = str_replace(NAME, "Census Tract",""))
## # A tibble: 37 x 6
## GEOID NAME variable estimate moe geometry
## <chr> <chr> <chr> <dbl> <dbl> <S3: sfc_MULTIPOLYGON>
## 1 53005011100 111, B19013_001 54530 6318 <S3: sfc_MULTIPOLYGON>
## 2 53005010500 105, B19013_001 57228 8047 <S3: sfc_MULTIPOLYGON>
## 3 53005011002 110.02, B19013_001 39103 3838 <S3: sfc_MULTIPOLYGON>
## 4 53005010400 104, B19013_001 43605 3942 <S3: sfc_MULTIPOLYGON>
## 5 53005011401 114.01, B19013_001 35250 6995 <S3: sfc_MULTIPOLYGON>
## 6 53005010202 102.02, B19013_001 51402 7977 <S3: sfc_MULTIPOLYGON>
## 7 53005011800 118, B19013_001 50372 9534 <S3: sfc_MULTIPOLYGON>
## 8 53005012000 120, B19013_001 NA NA <S3: sfc_MULTIPOLYGON>
## 9 53005010201 102.01, B19013_001 78894 15119 <S3: sfc_MULTIPOLYGON>
## 10 53005010705 107.05, B19013_001 96026 8985 <S3: sfc_MULTIPOLYGON>
## # ... with 27 more rows
head(bentoninc)
## Simple feature collection with 6 features and 5 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -119.2977 ymin: 46.17746 xmax: -119.095 ymax: 46.35108
## epsg (SRID): 4269
## proj4string: +proj=longlat +datum=NAD83 +no_defs
## # A tibble: 6 x 6
## GEOID NAME variable
## <chr> <chr> <chr>
## 1 53005011100 Census Tract 111, Benton County, Washington B19013_001
## 2 53005010500 Census Tract 105, Benton County, Washington B19013_001
## 3 53005011002 Census Tract 110.02, Benton County, Washington B19013_001
## 4 53005010400 Census Tract 104, Benton County, Washington B19013_001
## 5 53005011401 Census Tract 114.01, Benton County, Washington B19013_001
## 6 53005010202 Census Tract 102.02, Benton County, Washington B19013_001
## # ... with 3 more variables: estimate <dbl>, moe <dbl>, geometry <S3:
## # sfc_MULTIPOLYGON>
pal <- colorQuantile("viridis", NULL, n = 5)
leaflet(data = bentoninc) %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(fillColor = ~pal(estimate),
fillOpacity = 0.8,
color = "#BDBDC3",
weight = 1
)
## Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
## Need '+proj=longlat +datum=WGS84'
#decided to stay in sate and use benton county because it was relatively big but still not as common as the other counties and used the same variable estimate from our exercise in class.
Use the bubble map technique that I demonstrated in class with the same choices I made in Problem 1.
library(openintro)
## Please visit openintro.org for free statistics materials
##
## Attaching package: 'openintro'
## The following object is masked from 'package:datasets':
##
## cars
centroids <- read_delim("C:/Users/Tin/Downloads/centroid.txt","\t", escape_double = FALSE, col_types = cols(GEOID = col_character()), trim_ws = TRUE)
cc <- countyComplete %>%
filter(state == "Washington") %>%
select(FIPS,pop2010) %>%
rename(GEOID = FIPS) %>%
mutate(GEOID = as.character(GEOID))
select(centroids, GEOID, INTPTLAT, INTPTLONG) %>%
full_join(cc) %>%
full_join(bentoninc) -> bentoninc
## Joining, by = "GEOID"
## Joining, by = "GEOID"
bentoninc %>% ggplot(aes(x = INTPTLONG, y = INTPTLAT)) +
geom_sf() +
geom_point(aes(color = estimate, size = pop2010)) +
scale_color_viridis(option = "magma")
## Warning: Removed 281 rows containing non-finite values (stat_sf).
## Warning: Removed 318 rows containing missing values (geom_point).
#The graphic is incredibly small because of how it is scaled with geom_sf
Use Jula Silge’s method to make a leaflet-based choropleth with the same choices.
v15 <- load_variables(2015, "acs5", cache = TRUE)
options(tigris_use_cache = TRUE)
bentoninc <- get_acs(state = "WA", county = "Benton", geography = "tract", variables = "B19013_001", geometry = TRUE)
bentoninc %>%
mutate(NAME = str_replace(NAME,'Benton County, Washington','')) %>%
mutate(NAME = str_replace(NAME, "Census Tract",""))
## # A tibble: 37 x 6
## GEOID NAME variable estimate moe geometry
## <chr> <chr> <chr> <dbl> <dbl> <S3: sfc_MULTIPOLYGON>
## 1 53005011100 111, B19013_001 54530 6318 <S3: sfc_MULTIPOLYGON>
## 2 53005010500 105, B19013_001 57228 8047 <S3: sfc_MULTIPOLYGON>
## 3 53005011002 110.02, B19013_001 39103 3838 <S3: sfc_MULTIPOLYGON>
## 4 53005010400 104, B19013_001 43605 3942 <S3: sfc_MULTIPOLYGON>
## 5 53005011401 114.01, B19013_001 35250 6995 <S3: sfc_MULTIPOLYGON>
## 6 53005010202 102.02, B19013_001 51402 7977 <S3: sfc_MULTIPOLYGON>
## 7 53005011800 118, B19013_001 50372 9534 <S3: sfc_MULTIPOLYGON>
## 8 53005012000 120, B19013_001 NA NA <S3: sfc_MULTIPOLYGON>
## 9 53005010201 102.01, B19013_001 78894 15119 <S3: sfc_MULTIPOLYGON>
## 10 53005010705 107.05, B19013_001 96026 8985 <S3: sfc_MULTIPOLYGON>
## # ... with 27 more rows
head(bentoninc)
## Simple feature collection with 6 features and 5 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -119.2977 ymin: 46.17746 xmax: -119.095 ymax: 46.35108
## epsg (SRID): 4269
## proj4string: +proj=longlat +datum=NAD83 +no_defs
## # A tibble: 6 x 6
## GEOID NAME variable
## <chr> <chr> <chr>
## 1 53005011100 Census Tract 111, Benton County, Washington B19013_001
## 2 53005010500 Census Tract 105, Benton County, Washington B19013_001
## 3 53005011002 Census Tract 110.02, Benton County, Washington B19013_001
## 4 53005010400 Census Tract 104, Benton County, Washington B19013_001
## 5 53005011401 Census Tract 114.01, Benton County, Washington B19013_001
## 6 53005010202 Census Tract 102.02, Benton County, Washington B19013_001
## # ... with 3 more variables: estimate <dbl>, moe <dbl>, geometry <S3:
## # sfc_MULTIPOLYGON>
library(sf)
## Warning: package 'sf' was built under R version 3.3.3
## Linking to GEOS 3.6.1, GDAL 2.2.0, proj.4 4.9.3
pal <- colorQuantile(palette = "viridis", domain = bentoninc$estimate, n = 10)
bentoninc %>%
st_transform(crs = "+init=epsg:4326") %>%
leaflet(width = "100%") %>%
addProviderTiles(provider = "Esri.WorldStreetMap") %>%
addPolygons(popup = ~ str_extract(NAME, "^([^,]*)"),
stroke = FALSE,
smoothFactor = 0,
fillOpacity = 0.4,
color = ~ pal(estimate)) %>%
addLegend("bottomright",
pal = pal,
values = ~ estimate,
title = "Income percenticiles",
opacity = 1)