#Macro Data
#Poverty_in_San_Antonio
library(tidycensus)
library(tidyverse)
library(sf)
library(tmap)
pov19<-get_acs(geography = "tract",
state="TX",
#county = "Bexar",
year = 2021,
variables = c("DP03_0119PE", "B01003_001E"), # pov and total pop
geometry = T,
output = "wide")
## 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)`.
## Fetching data by table type ("B/C", "S", "DP") and combining the result.
#Requested the data for the whole of Texas.
pov19 <- pov19%>%
mutate(ppov = DP03_0119PE,
ppov_er = DP03_0119PM/1.645,
ppov_cv =100* (ppov_er/ppov)) %>%
filter(complete.cases(ppov), is.finite(ppov_cv)==T)%>%
select(GEOID, ppov, ppov_er,ppov_cv)
head(pov19)
## Simple feature collection with 6 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -95.88063 ymin: 31.59742 xmax: -95.42022 ymax: 32.08447
## Geodetic CRS: NAD83
## # A tibble: 6 × 5
## GEOID ppov ppov_er ppov_cv geometry
## <chr> <dbl> <dbl> <dbl> <MULTIPOLYGON [°]>
## 1 48001950100 15.1 5.05 33.4 (((-95.69483 31.97972, -95.69343 31.98426, …
## 2 48001950500 13.3 5.29 39.8 (((-95.68779 31.81046, -95.68161 31.81061, …
## 3 48001950600 27.7 8.27 29.8 (((-95.70758 31.72563, -95.70396 31.72833, …
## 4 48001950700 11.9 5.90 49.6 (((-95.64951 31.7505, -95.64491 31.75575, -…
## 5 48001950800 7.2 3.59 49.8 (((-95.62881 31.75671, -95.62535 31.75762, …
## 6 48001950901 1.8 1.82 101. (((-95.88044 31.70335, -95.87942 31.70352, …
#“48029190800”, “48029190400”, “48029190200”,“48029170101”, “48029110600”, “48029110500”
pov19$GEOID<- as.numeric(pov19$GEOID) #need to convert to numeric data type for the purpose of filtering
pov19_2<- pov19 #creating a copy for later use
pov19_2 <- pov19_2 %>%
filter(GEOID == 48029190800 | GEOID == 48029190400 | GEOID == 48029190200 | GEOID == 48029170101 | GEOID == 48029110600 | GEOID == 48029110500 | GEOID == 48029110300 | GEOID == 48029191900 | GEOID == 48029130600 ) # Only san antonio
tmap_mode("plot") #This is for the map to be static
## tmap mode set to plotting
#tmap_mode("view") # This is fpr the map to be interactive
map1<- tm_shape(pov19_2) +
tm_polygons(c("ppov"),
title = c("% in Poverty"),
palette = "Blues",
style = "quantile",
n = 5) +
tm_text("GEOID",
size = 0.5) +
tm_scale_bar() +
tm_layout(title = "San Antonio Poverty Rate Estimates - Quantile Breaks",
title.size = 1.5,
legend.frame = TRUE,
title.position = c("right", "top")) +
tm_compass() +
tm_format("World",
legend.position = c("left", "bottom"),
main.title.position = c("center"))
tmap_save(map1, filename = "san_antonio_poverty_all.png", width = 8, height = 6, dpi = 300) # This will save the map in your working directory
## Map saved to C:\Users\bryan\Downloads\san_antonio_poverty_all.png
## Resolution: 2400 by 1800 pixels
## Size: 8 by 6 inches (300 dpi)
map1
pov19$GEOID<- as.numeric(pov19$GEOID) #need to convert to numeric data type for the purpose of filtering
pov19_2<- pov19
selected_geoids <- c(48029190800, 48029190400, 48029190200, 48029170101, 48029110600, 48029110500, 48029191900, 48029191900, 48029130600)
pov19_2 <- pov19_2 %>%
mutate(ppov = ifelse(GEOID %in% selected_geoids, ppov, 0))
pov19_2$GEOID<- as.numeric(pov19_2$GEOID)
pov21<-get_acs(geography = "tract",
state="TX",
#county = "Bexar",
year = 2021,
variables = c("B03002_001", "B15002_001"), # pov and total pop
geometry = T,
output = "wide")
## 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)`.
# Define your census tracts here
census_tracts <- c("48029190800", "48029190400", "48029190200", "48029170101", "48029110600", "48029110500", "48029110300", "48029191900", "48029130600")
# Filter the data for the specified census tracts
filtered_data <- pov21 %>%
filter(GEOID %in% census_tracts)
# View the filtered data
View(filtered_data)
# Load the knitr library
library(knitr)
# Assuming you've already filtered the data and removed unwanted columns
# Replace the following line with your actual data frame
# filtered_data <- ...
# Remove the specified columns
filtered_data <- filtered_data %>%
select(-geometry, -B03002_001M, -B15002_001M)
# Rename the specified columns
filtered_data <- filtered_data %>%
rename(
`HISPANIC OR LATINO ORIGIN BY RACE` = B03002_001E,
`SEX BY EDUCATIONAL ATTAINMENT FOR THE POPULATION 25 YEARS AND OLDER` = B15002_001E
)
# Create a table using kable
kable(filtered_data, format = "markdown")
| GEOID | NAME | HISPANIC OR LATINO ORIGIN BY RACE | SEX BY EDUCATIONAL ATTAINMENT FOR THE POPULATION 25 YEARS AND OLDER | geometry |
|---|---|---|---|---|
| 48029170101 | Census Tract 1701.01, Bexar County, Texas | 3438 | 2164 | MULTIPOLYGON (((-98.52568 2… |
| 48029190200 | Census Tract 1902, Bexar County, Texas | 4014 | 3475 | MULTIPOLYGON (((-98.49929 2… |
| 48029110600 | Census Tract 1106, Bexar County, Texas | 5384 | 4560 | MULTIPOLYGON (((-98.51358 2… |
| 48029110500 | Census Tract 1105, Bexar County, Texas | 2201 | 1130 | MULTIPOLYGON (((-98.51479 2… |
| 48029190400 | Census Tract 1904, Bexar County, Texas | 4382 | 1772 | MULTIPOLYGON (((-98.4993 29… |
| 48029190800 | Census Tract 1908, Bexar County, Texas | 2432 | 1708 | MULTIPOLYGON (((-98.49224 2… |
| 48029110300 | Census Tract 1103, Bexar County, Texas | 2930 | 2090 | MULTIPOLYGON (((-98.48895 2… |
| 48029191900 | Census Tract 1919, Bexar County, Texas | 4348 | 3370 | MULTIPOLYGON (((-98.48173 2… |
| 48029130600 | Census Tract 1306, Bexar County, Texas | 3971 | 2791 | MULTIPOLYGON (((-98.46103 2… |
# Load the necessary libraries
library(tidycensus)
library(dplyr)
# Set your Census API key (get your key from the Census Bureau)
census_api_key("a913e004cb91db94d0fe027333db6c7cda9be0cc", 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] "a913e004cb91db94d0fe027333db6c7cda9be0cc"
# Define the geography (census tracts in Bexar County, TX)
bexar_county <- get_acs(
geography = "tract",
year = 2021, # Use 2021 for the ACS 2021 dataset
survey = "acs5", # Use "acs5" for the 5-year dataset
state = "TX",
county = "Bexar",
table = "B01003" # Specify the ACS table containing variables
)
## Getting data from the 2017-2021 5-year ACS
## Loading ACS5 variables for 2021 from table B01003. To cache this dataset for faster access to ACS tables in the future, run this function with `cache_table = TRUE`. You only need to do this once per ACS dataset.
# Check the column names of the bexar_county data frame
column_names <- colnames(bexar_county)
print(column_names)
## [1] "GEOID" "NAME" "variable" "estimate" "moe"
variables <- load_variables(2021, "acs5")
# Search for variable descriptions
variable_description_P9 <- variables %>%
filter(name == "P9")
variable_description_B01001I <- variables %>%
filter(name == "B01001I")
# Print the descriptions
print(variable_description_P9)
## # A tibble: 0 × 4
## # ℹ 4 variables: name <chr>, label <chr>, concept <chr>, geography <chr>
print(variable_description_B01001I)
## # A tibble: 0 × 4
## # ℹ 4 variables: name <chr>, label <chr>, concept <chr>, geography <chr>
# Load the necessary libraries
library(tidycensus)
library(dplyr)
# Set your Census API key (get your key from the Census Bureau)
census_api_key("a913e004cb91db94d0fe027333db6c7cda9be0cc", 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] "a913e004cb91db94d0fe027333db6c7cda9be0cc"
# Define your census tracts here
census_tracts <- c("48029190800", "48029190400", "48029190200", "48029170101", "48029110600", "48029110500", "48029110300", "48029191900", "48029130600")
# Retrieve ACS data for income and health insurance status
acs_data <- get_acs(
geography = "tract",
year = 2021, # Use the 2021 ACS
survey = "acs5",
state = "tx", # Specify the state (Texas)
county = "Bexar", # Specify the county or place, if applicable
variables = c("B06011_001", "C27001A_006", "C27001A_005"), # Specify variables of interest
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)`.
# Convert to a data frame
acs_df <- as.data.frame(acs_data)
# Load the necessary libraries
library(tidyr)
# Pivot the data to wide format
wide_acs_data <- acs_df %>%
pivot_wider(
id_cols = GEOID,
names_from = variable,
values_from = estimate
)
# Print the wide-format data
print(wide_acs_data)
## # A tibble: 375 × 4
## GEOID B06011_001 C27001A_005 C27001A_006
## <chr> <dbl> <dbl> <dbl>
## 1 48029171929 21185 2351 1467
## 2 48029130500 18064 1178 840
## 3 48029171100 15119 1255 725
## 4 48029181731 39722 3034 2582
## 5 48029121904 52460 1137 1061
## 6 48029172005 51455 4025 3585
## 7 48029170102 19079 1060 693
## 8 48029131503 28147 927 852
## 9 48029191102 37019 583 569
## 10 48029170101 21961 1072 539
## # ℹ 365 more rows
# Filter the data for the selected census tracts
filtered_data <- acs_data %>%
filter(GEOID %in% census_tracts)
# Calculate descriptive statistics
wide_summary_stats <- wide_acs_data %>%
summarise(
Mean_Income = mean(B06011_001, na.rm = TRUE),
Median_Income = median(B06011_001, na.rm = TRUE),
Health_Insurance_Yes = sum(C27001A_006, na.rm = TRUE),
Health_Insurance_No = sum(C27001A_005, na.rm = TRUE)
)
# Print the summary statistics table
print(wide_summary_stats)
## # A tibble: 1 × 4
## Mean_Income Median_Income Health_Insurance_Yes Health_Insurance_No
## <dbl> <dbl> <dbl> <dbl>
## 1 33583. 30925 602433 754026
# Load the tidycensus package and authenticate with your API key
library(tidycensus)
census_api_key("a913e004cb91db94d0fe027333db6c7cda9be0cc")
## To install your API key for use in future sessions, run this function with `install = TRUE`.
# Specify the census tracts of interest
green_selected_tracts <- c(
"48029190800", "48029190400", "48029190200"
)
# Specify the census tracts of interest
red <- c(
"48029170101",
"48029110600", "48029110500", "48029110300", "48029191900", "48029130600"
)
# Fetch ACS data for selected census tracts
acs_data <- get_acs(
geography = "tract",
variables = c("B19013_001", "B03002_001", "B01002_001", "B06012_002", "B23006_023", "B23006_001", "B17001_002", "B17001_001"),
state = "TX",
county = "Bexar",
geometry = TRUE,
output = "wide"
)
## 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)`.
acs_data$bachelors_or_more_rate <- acs_data$B23006_023E/acs_data$B23006_001E
acs_data$poverty_rate <- acs_data$B17001_002E/acs_data$B17001_001E
#Load variable list from ACS
acs_variables <- load_variables(2021, "acs5")
# Calculate descriptive statistics for income, race/ethnicity, and age
green_summary_table <- acs_data %>%
filter(GEOID %in% green_selected_tracts) %>%
summarise(
Mean_Income = mean(B19013_001E, na.rm = TRUE),
Total_Population = sum(B03002_001E, na.rm = TRUE),
Mean_Age = mean(B01002_001E, na.rm = TRUE),
Mean_educate = mean(bachelors_or_more_rate),
Mean_poverty = mean(poverty_rate),
SD_poverty = sd(poverty_rate),
SD_educate = sd(bachelors_or_more_rate),
SD_Mean_Age = sd(B01002_001E)
)
# Display the descriptive statistics table
print(green_summary_table)
## Simple feature collection with 1 feature and 8 fields
## Geometry type: POLYGON
## Dimension: XY
## Bounding box: xmin: -98.49936 ymin: 29.44549 xmax: -98.47623 ymax: 29.48428
## Geodetic CRS: NAD83
## Mean_Income Total_Population Mean_Age Mean_educate Mean_poverty SD_poverty
## 1 98205 10828 36.46667 0.6405236 0.09723982 0.03873732
## SD_educate SD_Mean_Age geometry
## 1 0.1953571 12.96624 POLYGON ((-98.49931 29.4586...
# Calculate descriptive statistics for income, race/ethnicity, and age
red_summary_table <- acs_data %>%
filter(GEOID %in% red) %>%
summarise(
Mean_Income = mean(B19013_001E, na.rm = TRUE),
Total_Population = sum(B03002_001E, na.rm = TRUE),
Mean_Age = mean(B01002_001E, na.rm = TRUE),
Mean_educate = mean(bachelors_or_more_rate),
Mean_poverty = mean(poverty_rate),
SD_poverty = sd(poverty_rate),
SD_educate = sd(bachelors_or_more_rate),
SD_Mean_Age = sd(B01002_001E)
)
# Display the descriptive statistics table
print(red_summary_table)
## Simple feature collection with 1 feature and 8 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -98.52594 ymin: 29.40677 xmax: -98.4426 ymax: 29.44517
## Geodetic CRS: NAD83
## Mean_Income Total_Population Mean_Age Mean_educate Mean_poverty SD_poverty
## 1 33420.67 22272 35.15 0.1657479 0.3810605 0.1701233
## SD_educate SD_Mean_Age geometry
## 1 0.1566202 5.372057 MULTIPOLYGON (((-98.51383 2...