options(repos = c(CRAN = "https://cloud.r-project.org/"))
library(tidycensus)
library(ggplot2)
census_api_key("2d16fc12c9988ebe2747aafb68b71f5886df9b42", install=TRUE, overwrite=TRUE)
## 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] "2d16fc12c9988ebe2747aafb68b71f5886df9b42"
#Q1
median_age <- get_decennial(geography = "tract", state = "Tx",
variables = "P013002",
year = 2010)
## Getting data from the 2010 decennial Census
## Using Census Summary File 1
#Q2
install.packages("writexl")
## Installing package into 'C:/Users/dagob/AppData/Local/R/win-library/4.4'
## (as 'lib' is unspecified)
## package 'writexl' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'writexl'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\dagob\AppData\Local\R\win-library\4.4\00LOCK\writexl\libs\x64\writexl.dll
## to C:\Users\dagob\AppData\Local\R\win-library\4.4\writexl\libs\x64\writexl.dll:
## Permission denied
## Warning: restored 'writexl'
##
## The downloaded binary packages are in
## C:\Users\dagob\AppData\Local\Temp\RtmpuwFCkq\downloaded_packages
library(writexl)
write_xlsx(x = median_age, path = "C:/Users/dagob/OneDrive/Desktop/Data/median age.xlsx", col_names = TRUE)
#Q3
mhi <- get_acs(geography = "tract", state = "Tx", county = "bexar",
variables = "B19013_001E",
year = 2018)
## Getting data from the 2014-2018 5-year ACS
#Q4
hispanic_pop <- get_acs(geography = "county", state = "Tx",
variables = "B03002_012E",
year = 2018)
## Getting data from the 2014-2018 5-year ACS
#Q5
names(hispanic_pop)[4] <- "HispanicPop"
names(hispanic_pop)
## [1] "GEOID" "NAME" "variable" "HispanicPop" "moe"
#Q6
ggplot(hispanic_pop, aes(x =HispanicPop)) +
geom_boxplot()
#Q7
population_poverty <- get_acs(geography = "tract", state = "Tx", county = "bexar",
variables = "B17001F_001",
year = 2018)
## Getting data from the 2014-2018 5-year ACS
#Q8
race <- get_acs(geography = "tract", state = "Tx", county = "bexar",
variables =c(hispanic='B03002_012E',
white='B03002_003E',
black='B03002_004E') ,output = "wide",
year = 2018)
## Getting data from the 2014-2018 5-year ACS
#Q9
var <- c(poptotal='B03002_001E',
hispanic='B03002_012E',
white='B03002_003E',
black='B03002_004E',
asian='B03002_006E',
poptotal2='B17017_001E',
poverty='B17017_002E')
cbg <- get_acs(geography = "block group", variables = var, county ="Bexar",
state = "TX",output="wide", year = 2021, 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% | |= | 1% | |= | 2% | |== | 2% | |== | 3% | |== | 4% | |=== | 4% | |=== | 5% | |==== | 5% | |==== | 6% | |===== | 7% | |===== | 8% | |====== | 8% | |====== | 9% | |======= | 9% | |======= | 10% | |======== | 11% | |======== | 12% | |========= | 12% | |========= | 13% | |========= | 14% | |========== | 14% | |========== | 15% | |=========== | 15% | |=========== | 16% | |============ | 17% | |============= | 18% | |============= | 19% | |=============== | 21% | |================ | 23% | |================= | 24% | |=================== | 27% | |==================== | 28% | |===================== | 31% | |======================= | 33% | |========================= | 36% | |=========================== | 38% | |=========================== | 39% | |============================= | 41% | |============================== | 43% | |================================ | 45% | |================================= | 48% | |================================== | 48% | |==================================== | 51% | |==================================== | 52% | |====================================== | 54% | |====================================== | 55% | |======================================== | 57% | |========================================= | 58% | |========================================= | 59% | |========================================== | 60% | |====================================================== | 77% | |====================================================== | 78% | |============================================================ | 85% | |================================================================= | 92% | |======================================================================| 100%
cbg$black_pct <-cbg$black/cbg$poptotal
cbg$white_pct <- cbg$white/cbg$poptotal
cbg$hispanic_pct <- cbg$hispanic/cbg$poptotal
cbg$poverty_pct <- cbg$poverty /cbg$poptotal2
###define poverty neighborhood
cbg$Poor <- ifelse(cbg$poverty_pct > 0.3, "Poor", "Nonpoor")
###define minority neighborhood
cbg$Race <- "Other" # Default value
cbg$Race[cbg$white_pct > 0.5] <- "White"
cbg$Race[cbg$black_pct > 0.5] <- "Black"
cbg$Race[cbg$hispanic_pct > 0.5] <- "Hispanic"
cbg$race_poverty <- paste0(cbg$Poor, cbg$Race)
#Q10
ggplot(cbg, aes(x=race_poverty, fill=race_poverty )) +
geom_bar()