<jupyter_start><jupyter_text>Working with Census DataUsing the tidycensus R package to query and download Census data and the corresponding geographies.See more about tidycensus here: https://walker-data.com/tidycensus/  Enable R<jupyter_code>%load_ext rpy2.ipython<jupyter_output><empty_output><jupyter_text>Install Necessary R Libraries<jupyter_code>from rpy2.robjects.packages import importr
utils = importr('utils')
utils.install_packages('tidycensus', repos="https://cloud.r-project.org")
utils.install_packages('tidyverse', repos="https://cloud.r-project.org")
utils.install_packages('sf', repos="https://cloud.r-project.org")<jupyter_output><empty_output><jupyter_text>Load the necessary R Libraries<jupyter_code>%%R
library(tidycensus)
library(tidyverse)
library(sf)
library(tigris)<jupyter_output><empty_output><jupyter_text>Get Median HHI in NYC by Census Tract<jupyter_code>%%R

median_hhi_tract <- get_acs(
  geography = "tract",
  variables = "B19013_001E",
  state = "NY",
  county = c("New York", "Bronx","Kings", "Queens", "Kings", "Richmond"),
  year = 2020,
  geometry = TRUE
)
%%R
ggplot(median_hhi, aes(fill = estimate)) + 
  geom_sf() + 
  theme_void() + 
  scale_fill_viridis_c(labels = scales::dollar)<jupyter_output><empty_output><jupyter_text>Get Median HHI in NYC by Census Tract Clipped to Shoreline<jupyter_code>%%R

median_hhi_tract_clipped <- get_acs(
  geography = "tract",
  variables = "B19013_001E",
  state = "NY",
  county = c("New York", "Bronx","Kings", "Queens", "Kings", "Richmond"),
  year = 2020,
  geometry = TRUE
) %>%
  st_transform(6933) %>%
  erase_water(area_threshold = 0.75) %>%
    st_transform(4326)
%%R
ggplot(median_hhi_tract_clipped, aes(fill = estimate)) + 
  geom_sf() + 
  theme_void() + 
  scale_fill_viridis_c(labels = scales::dollar)<jupyter_output><empty_output><jupyter_text>Save Median HHI to GeoJSON<jupyter_code>%%R
st_write(median_hhi_tract_clipped, "data/nyc_median_hhi.geojson")<jupyter_output>Writing layer `nyc_median_hhi' to data source 
  `data/nyc_median_hhi.geojson' using driver `GeoJSON'
Writing 2324 features with 5 fields and geometry type Unknown (any).<jupyter_text>Read the Outputted GeoJSON into a GeoDataFrame If we are more comfortable working with the outputted Census data in Python, we can read the local GeoJSON file into a GeoDataFrame and proceed from there.<jupyter_code>import geopandas as gpd
import matplotlib.pyplot as plt
gdf = gpd.read_file("data/nyc_median_hhi.geojson")
fig, ax = plt.subplots(figsize=(12,12))
gdf.plot(ax=ax, column='estimate', scheme='quantiles')
ax.axis('off');
gdf.head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12628767645936664, 'fraction_numerical': 0.01755055322396032, 'mean_word_length': 6.873873873873874, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 31, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '661402', 'license': 'MIT', 'n_tokens_mistral': 1023, 'n_tokens_neox': 944, 'n_words': 199, 'path': 'modules/11-spatial-regression/census.ipynb', 'repo_name': 'willgeary/info615', 'size': '2621'}