## R Markdown
##########################################################
# #
# Course: URP-5363-901-202410 #
# Assignment: 3 #
# #
# Student: Bradley Abbott #
# Student ID: Wuh344 #
# #
#####################################################################################################
# #
# The goal of this assignment is to collect and map the race/ethnicity data for Bexar county. #
# #
# Step 1: Import the census data library (tidycensus) and GIS libraries (sf)(tmap). (1’) #
# #
# Step 2: Using your census API key, capture the population of Hispanic, Non-Hispanic White, #
# Non-Hispanic Black or African American, total population, and median income for all #
# census tracts in Bexar County. (1’) #
# #
# Step 3: Rename the columns for estimates and remove the columns for MOE (1’) #
# #
# Step 4: Calculate the percentage of Hispanic, Non-Hispanic White, Non-Hispanic Black or #
# African American. (1’) #
# #
# Step 5: Save the data to csv file. (1’) #
# #
# Step 6: Map the percentage of Hispanic, Non-Hispanic White, Non-Hispanic Black or African #
# American. (1’) #
# #
# Step 7: Import library(ggplot2), plot the scatter plot between median income and percentage of #
# Hispanic. (1’) #
# #
# Step 8: Move your code to R Markdown and publish the output to Rpubs. (1’) #
# #
#####################################################################################################
#
# Load Base Pakcages
## --------- Load required packages
install.packages('pacman',repos="http://cran.us.r-project.org")
## Installing package into 'C:/Users/bradw/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'pacman' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\bradw\AppData\Local\Temp\RtmpE34oh9\downloaded_packages
library('pacman')
pacman::p_install("tidycensus")
## Installing package into 'C:/Users/bradw/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## Warning: unable to access index for repository http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/4.3:
## cannot open URL 'http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/4.3/PACKAGES'
## package 'tidycensus' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\bradw\AppData\Local\Temp\RtmpE34oh9\downloaded_packages
##
## tidycensus installed
pacman::p_install("ggplot2")
## Installing package into 'C:/Users/bradw/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## Warning: unable to access index for repository http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/4.3:
## cannot open URL 'http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/4.3/PACKAGES'
## package 'ggplot2' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\bradw\AppData\Local\Temp\RtmpE34oh9\downloaded_packages
##
## ggplot2 installed
pacman::p_load(tidycensus,ggplot2)
## Make sure your current packages are up to date
#update.packages()
## devtools is required
library('devtools')
## Loading required package: usethis
install_github("trinker/pacman")
## Downloading GitHub repo trinker/pacman@HEAD
##
## ── R CMD build ─────────────────────────────────────────────────────────────────
##
checking for file 'C:\Users\bradw\AppData\Local\Temp\RtmpE34oh9\remotes5a7025c62b30\trinker-pacman-ace0936/DESCRIPTION' ...
✔ checking for file 'C:\Users\bradw\AppData\Local\Temp\RtmpE34oh9\remotes5a7025c62b30\trinker-pacman-ace0936/DESCRIPTION'
##
─ preparing 'pacman': (839ms)
## checking DESCRIPTION meta-information ...
✔ checking DESCRIPTION meta-information
##
─ checking for LF line-endings in source and make files and shell scripts
##
─ checking for empty or unneeded directories
##
─ building 'pacman_0.5.3.tar.gz'
##
##
## Warning: package 'pacman' is in use and will not be installed
# Step 1: Import the census data library (tidycensus) and GIS libraries (sf)(tmap). (1’)
pacman::p_install("sfmap")
## Installing package into 'C:/Users/bradw/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## Warning: package 'sfmap' is not available for this version of R
##
## A version of this package for your version of R might be available elsewhere,
## see the ideas at
## https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages
## Warning: unable to access index for repository http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/4.3:
## cannot open URL 'http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/4.3/PACKAGES'
## Warning: 'BiocManager' not available. Could not check Bioconductor.
##
## Please use `install.packages('BiocManager')` and then retry.
## Warning in pacman::p_install("sfmap"):
pacman::p_install("tmap")
## Installing package into 'C:/Users/bradw/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## Warning: unable to access index for repository http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/4.3:
## cannot open URL 'http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/4.3/PACKAGES'
## package 'tmap' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\bradw\AppData\Local\Temp\RtmpE34oh9\downloaded_packages
##
## tmap installed
pacman::p_load(sfmap,tmap)
## Installing package into 'C:/Users/bradw/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## Warning: package 'sfmap' is not available for this version of R
##
## A version of this package for your version of R might be available elsewhere,
## see the ideas at
## https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages
## Warning: unable to access index for repository http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/4.3:
## cannot open URL 'http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/4.3/PACKAGES'
## Warning: 'BiocManager' not available. Could not check Bioconductor.
##
## Please use `install.packages('BiocManager')` and then retry.
## Warning in p_install(package, character.only = TRUE, ...):
## Warning in library(package, lib.loc = lib.loc, character.only = TRUE,
## logical.return = TRUE, : there is no package called 'sfmap'
## Warning in pacman::p_load(sfmap, tmap): Failed to install/load:
## sfmap
#####################################################################################################
#
# Step 2: Using your census API key, capture the population of Hispanic, Non-Hispanic White, #
# Non-Hispanic Black or African American, total population, and median income for all #
# census tracts in Bexar County. (1’) #
#
## --------- Install the census API key --------------------------------------------------------- #
census_api_key("a1d73f99315204015f70be17812bb1fb4237d804",install="TRUE",overwrite=TRUE) #this is mine, type ?census_api_key to get yours
## Your original .Renviron will be backed up and stored in your R HOME directory if needed.
## Your API key has been stored in your .Renviron and can be accessed by Sys.getenv("CENSUS_API_KEY").
## To use now, restart R or run `readRenviron("~/.Renviron")`
## [1] "a1d73f99315204015f70be17812bb1fb4237d804"
readRenviron("~/.Renviron")
Sys.getenv("CENSUS_API_KEY")
## [1] "a1d73f99315204015f70be17812bb1fb4237d804"
#'B03002_012E Estimate!!Total:!!Hispanic or Latino: HISPANIC OR LATINO ORIGIN BY RACE'
#'B03002_003E Estimate!!Total:!!Not Hispanic or Latino:!!White alone HISPANIC OR LATINO ORIGIN BY RACE'
#'B03002_004E Estimate!!Total:!!Not Hispanic or Latino:!!Black or African American alone HISPANIC OR LATINO ORIGIN BY RACE'
#'B03002_006E Estimate!!Total:!!Not Hispanic or Latino:!!Asian alone HISPANIC OR LATINO ORIGIN BY RACE'
var<-c('B03002_012E','B03002_003E','B03002_004E','B19013_001E','B01001_001E')
bexar_segregation <- get_acs(geography = "tract", variables = var, county = "Bexar",
state = "TX",output="wide", geometry = TRUE)
## Getting data from the 2017-2021 5-year ACS
## Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
##
|
| | 0%
|
|= | 1%
|
|= | 2%
|
|== | 2%
|
|== | 3%
|
|=== | 4%
|
|=== | 5%
|
|==== | 5%
|
|==== | 6%
|
|===== | 7%
|
|===== | 8%
|
|====== | 8%
|
|====== | 9%
|
|======= | 9%
|
|======= | 10%
|
|======== | 11%
|
|========= | 12%
|
|========= | 13%
|
|========== | 14%
|
|=========== | 15%
|
|=========== | 16%
|
|============ | 17%
|
|============ | 18%
|
|============= | 18%
|
|============= | 19%
|
|============== | 20%
|
|=============== | 21%
|
|=============== | 22%
|
|================ | 22%
|
|================ | 23%
|
|================= | 24%
|
|================== | 25%
|
|================== | 26%
|
|=================== | 27%
|
|==================== | 28%
|
|==================== | 29%
|
|===================== | 30%
|
|===================== | 31%
|
|====================== | 31%
|
|====================== | 32%
|
|======================= | 33%
|
|======================== | 34%
|
|======================== | 35%
|
|========================= | 35%
|
|========================= | 36%
|
|========================== | 37%
|
|=========================== | 38%
|
|=========================== | 39%
|
|============================ | 40%
|
|============================= | 41%
|
|============================= | 42%
|
|============================== | 43%
|
|=============================== | 44%
|
|=============================== | 45%
|
|================================ | 46%
|
|================================= | 47%
|
|================================= | 48%
|
|================================== | 48%
|
|================================== | 49%
|
|=================================== | 50%
|
|==================================== | 51%
|
|==================================== | 52%
|
|===================================== | 52%
|
|===================================== | 53%
|
|====================================== | 54%
|
|====================================== | 55%
|
|======================================= | 55%
|
|======================================= | 56%
|
|======================================== | 57%
|
|======================================== | 58%
|
|========================================= | 58%
|
|========================================= | 59%
|
|========================================== | 59%
|
|========================================== | 60%
|
|=========================================== | 61%
|
|=========================================== | 62%
|
|============================================ | 63%
|
|============================================= | 64%
|
|============================================= | 65%
|
|============================================== | 65%
|
|============================================== | 66%
|
|=============================================== | 67%
|
|=============================================== | 68%
|
|================================================ | 68%
|
|================================================ | 69%
|
|================================================= | 70%
|
|================================================= | 71%
|
|================================================== | 71%
|
|================================================== | 72%
|
|=================================================== | 73%
|
|==================================================== | 74%
|
|==================================================== | 75%
|
|===================================================== | 76%
|
|====================================================== | 77%
|
|====================================================== | 78%
|
|======================================================= | 78%
|
|======================================================= | 79%
|
|======================================================== | 80%
|
|======================================================== | 81%
|
|========================================================= | 82%
|
|========================================================== | 83%
|
|========================================================== | 84%
|
|=========================================================== | 84%
|
|=========================================================== | 85%
|
|============================================================ | 86%
|
|============================================================= | 86%
|
|============================================================= | 87%
|
|============================================================== | 88%
|
|============================================================== | 89%
|
|=============================================================== | 89%
|
|=============================================================== | 90%
|
|================================================================ | 91%
|
|================================================================ | 92%
|
|================================================================= | 92%
|
|================================================================= | 93%
|
|================================================================== | 94%
|
|================================================================== | 95%
|
|=================================================================== | 95%
|
|=================================================================== | 96%
|
|==================================================================== | 97%
|
|===================================================================== | 98%
|
|===================================================================== | 99%
|
|======================================================================| 99%
|
|======================================================================| 100%
names(bexar_segregation)[3] <- 'Hispanic'
names(bexar_segregation)[5] <- 'White'
names(bexar_segregation)[7] <- 'Black_or_African_American'
names(bexar_segregation)[9] <- 'Household Median Income'
names(bexar_segregation)[11] <- 'TotalPop'
bexar_segregation$B03002_012M <- NULL
bexar_segregation$B03002_003M <- NULL
bexar_segregation$B03002_004M <- NULL
bexar_segregation$B19013_001M <- NULL
bexar_segregation$B01001_001M <- NULL
# Step 4: Calculate the percentage of Hispanic, Non-Hispanic White, Non-Hispanic Black or #
# African American. (1’)
bexar_segregation$per_hispanic=bexar_segregation$Hispanic/bexar_segregation$TotalPop
bexar_segregation$per_white=bexar_segregation$White/bexar_segregation$TotalPop
bexar_segregation$per_black=bexar_segregation$Black_or_African_American/bexar_segregation$TotalPop
# Step 5: Save the data to csv file. (1’) #
#
library(writexl)
write_xlsx(x = bexar_segregation, path = "G:\\My Drive\\School\\URP 5363 R Intro\\Assignment 3\\bexar_segregation.xlsx", col_names = TRUE)
# Step 6: Map the percentage of Hispanic, Non-Hispanic White, Non-Hispanic Black or African #
# American. (1’) #
#
tm_shape(bexar_segregation) +tm_fill(col = "per_hispanic")+ tm_layout(title = "Hispanic Percent")
tm_shape(bexar_segregation) +tm_fill(col = "per_white")+ tm_layout(title = "White Percent")
tm_shape(bexar_segregation) +tm_fill(col = "per_black")+ tm_layout(title = "Black Percent")
# # Step 7: Import library(ggplot2), plot the scatter plot between median income and percentage of #
# Hispanic. (1’)
qplot(x =bexar_segregation$'Household Median Income', y =bexar_segregation$'per_hispanic', data = bexar_segregation, geom ="point")
## Warning: `qplot()` was deprecated in ggplot2 3.4.0.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: Use of `bexar_segregation$"Household Median Income"` is discouraged.
## ℹ Use `Household Median Income` instead.
## Warning: Use of `bexar_segregation$per_hispanic` is discouraged.
## ℹ Use `per_hispanic` instead.
## Warning: Removed 4 rows containing missing values (`geom_point()`).