diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bf41ddf --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.Rproj.user +.Rhistory +.RData +.Ruserdata + +#exclude API keys +api_keys/* + diff --git a/api_keys/.gitignore b/api_keys/.gitignore new file mode 100644 index 0000000..5e7d273 --- /dev/null +++ b/api_keys/.gitignore @@ -0,0 +1,4 @@ +# Ignore everything in this directory +* +# Except this file +!.gitignore diff --git a/cities.csv b/cities.csv new file mode 100644 index 0000000..778c300 --- /dev/null +++ b/cities.csv @@ -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 diff --git a/city-compare.Rproj b/city-compare.Rproj new file mode 100644 index 0000000..8e3c2eb --- /dev/null +++ b/city-compare.Rproj @@ -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 diff --git a/city_compare.Rmd b/city_compare.Rmd new file mode 100644 index 0000000..e6b0d81 --- /dev/null +++ b/city_compare.Rmd @@ -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)) + +``` \ No newline at end of file