added city list and populations

This commit is contained in:
Ben Varick 2025-11-17 11:27:52 -07:00
parent a16eb66088
commit 7cfa2de2e1
Signed by: ben
SSH key fingerprint: SHA256:jWnpFDAcacYM5aPFpYRqlsamlDyKNpSj3jj+k4ojtUo
5 changed files with 121 additions and 0 deletions

8
.gitignore vendored Normal file
View file

@ -0,0 +1,8 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
#exclude API keys
api_keys/*

4
api_keys/.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore

7
cities.csv Normal file
View file

@ -0,0 +1,7 @@
City,Type,State,Country
Madison,city,WI,United States
Bellingham,city,WA,United States
Portland,city,OR,United States
Port Angeles,city,WA,United States
Boston,city,MA,United States
Boise City,city,ID,United States
1 City Type State Country
2 Madison city WI United States
3 Bellingham city WA United States
4 Portland city OR United States
5 Port Angeles city WA United States
6 Boston city MA United States
7 Boise City city ID United States

13
city-compare.Rproj Normal file
View file

@ -0,0 +1,13 @@
Version: 1.0
RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default
EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8
RnwWeave: Sweave
LaTeX: pdfLaTeX

89
city_compare.Rmd Normal file
View file

@ -0,0 +1,89 @@
---
title: "City Compare"
output:
html_document:
toc: true
toc_depth: 5
toc_float:
collapsed: false
smooth_scroll: true
editor_options:
chunk_output_type: console
---
# Input Data & Configuration
## Libraries
```{r libs, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE}
date()
rm(list=ls())
library(tidyverse)
library(tidycensus)
```
## API keys
```{r api_keys, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE}
# load census api key
census_api_key(key = substr(read_file(file = "api_keys/census_api_key"), 1, 40))
```
## Cities to compare
```{r cities, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE}
cities <- read_csv(file = "cities.csv")
cities <- cities %>%
mutate(city_name = paste0(City, " ", Type))
```
# Get data
## Census data
```{r census, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE}
populations <- list(NULL)
for(city in cities$city_name){
state <- cities %>% filter(city_name == city) %>% pull(State)
populations[[city]] <- get_acs(
geography = "place",
variables = "B01003_001",
state = state,
year = 2023
) %>%
filter(str_detect(NAME, city))
}
populations <- bind_rows(populations)
cities <- bind_cols(cities, populations)
ggplot(cities) +
geom_col(aes(x = City,
y = estimate))
```
## Weather
```{r weather, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE}
populations <- list(NULL)
for(city in cities$city_name){
state <- cities %>% filter(city_name == city) %>% pull(State)
populations[[city]] <- get_acs(
geography = "place",
variables = "B01003_001",
state = state,
year = 2023
) %>%
filter(str_detect(NAME, city))
}
populations <- bind_rows(populations)
cities <- bind_cols(cities, populations)
ggplot(cities) +
geom_col(aes(x = City,
y = estimate))
```