added city list and populations
This commit is contained in:
parent
a16eb66088
commit
7cfa2de2e1
5 changed files with 121 additions and 0 deletions
89
city_compare.Rmd
Normal file
89
city_compare.Rmd
Normal 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))
|
||||
|
||||
```
|
||||
Loading…
Add table
Add a link
Reference in a new issue