# Using your census API key, capture the median age of males (P013002) for all census tracts from the 2010 decennial census database in Texas. (1’)
library(tidycensus)
library(ggplot2)
avg_male_age <- get_decennial(geography = "tract",
state = "Texas",
variables = "P013002",
year = 2010)
## Getting data from the 2010 decennial Census
## Using Census Summary File 1
# Export the data collected in step 2 to a .xlsx file. (using function ‘write_xlsx’)
library(writexl)
write_xlsx(x = avg_male_age, path = "C:/Users/cruzs/Downloads/New folder/avgmaledata.xlsx", col_names = TRUE)
# Using your census API key, capture the median household income (B19013_001E) for all census tracts in Bexar County, TX, from the 2018 ACS database in Texas.
bexar_county <- get_acs(geography = "tract", variables = "B19013_001E",
state = "TX", county = "Bexar", year = 2018, geometry = TRUE)
## Getting data from the 2014-2018 5-year ACS
## Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
# Using your census API key, capture the Hispanic population (B03002_012E) for all counties in Texas from the 2018 ACS database.
hispanic_pop <- get_acs(geography = "county", variables = "B03002_012E",
state = "TX", year = 2018, geometry = TRUE)
## Getting data from the 2014-2018 5-year ACS
## Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
# Following step 5, rename the column of ‘estimate’ to ‘HispanicPop', and show all the column names of the dataframe.
names(hispanic_pop)[4] <- "HispanicPop"
# Make a boxplot to show the distribution of Hispanic population across TX counties.
ggplot(hispanic_pop, aes(y = HispanicPop)) +
geom_boxplot(fill = "navyblue", color = "black") +
scale_y_log10() + # I had to chatgpt this part for outliers
labs(title = "Hispanic Population Across TX Counties",
y = "Population",
x = "")

# Using your census API key, capture the population in poverty, for all census tracts in Bexar County, TX, from the 2018 ACS database in Texas.
poverty_pop <- get_acs(geography = "tract", variables = "B17001_002E",
state = "TX", county = "Bexar", year = 2018, geometry = TRUE)
## Getting data from the 2014-2018 5-year ACS
## Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
# Using your census API key, capture the Hispanic population, White population, and Black Population, for all census tracts in Bexar County, TX, from the 2018 ACS database in Texas.
var <- c(poptotal='B03002_001E',
hispanic='B03002_012E',
white='B03002_003E',
black='B03002_004E',
poptotal2='B17017_001E',
poverty='B17017_002E')
h_w_b_pop <- get_acs(geography = "block group",
variables = var,
state = "TX",
county = "Bexar",
year = 2018,
output="wide",
geometry = TRUE)
## Getting data from the 2014-2018 5-year ACS
## Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
h_w_b_pop$black_pct <- h_w_b_pop$black / h_w_b_pop$poptotal
h_w_b_pop$white_pct <- h_w_b_pop$white / h_w_b_pop$poptotal
h_w_b_pop$hispanic_pct <- h_w_b_pop$hispanic / h_w_b_pop$poptotal
h_w_b_pop$poverty_pct <- h_w_b_pop$poverty / h_w_b_pop$poptotal2
h_w_b_pop$Poor <- ifelse(h_w_b_pop$poverty_pct > 0.3, "Poor", "Nonpoor")
h_w_b_pop$Race <- "Other" # Default value
h_w_b_pop$Race[h_w_b_pop$white_pct > 0.5] <- "White"
h_w_b_pop$Race[h_w_b_pop$black_pct > 0.5] <- "Black"
h_w_b_pop$Race[h_w_b_pop$hispanic_pct > 0.5] <- "Hispanic"
# I had to chatgpt part of this because I could not figure out how to filter certain categories and the bar graph appeared to be incorrect
h_w_b_pop$race_poverty <- paste0(h_w_b_pop$Poor, h_w_b_pop$Race)
unique(h_w_b_pop$race_poverty)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
filtered_data <- h_w_b_pop %>%
filter(!grepl("Nonpoor", race_poverty))
unique(filtered_data$race_poverty)
ggplot(filtered_data, aes(x=race_poverty, fill=race_poverty)) +
geom_bar()

# This was my original result
ggplot(h_w_b_pop, aes(x=race_poverty, fill=race_poverty )) +
geom_bar()
