added weather and city maps
This commit is contained in:
parent
7cfa2de2e1
commit
52a85d4d50
2 changed files with 80 additions and 19 deletions
|
|
@ -5,3 +5,4 @@ Portland,city,OR,United States
|
||||||
Port Angeles,city,WA,United States
|
Port Angeles,city,WA,United States
|
||||||
Boston,city,MA,United States
|
Boston,city,MA,United States
|
||||||
Boise City,city,ID,United States
|
Boise City,city,ID,United States
|
||||||
|
Salt Lake City,city,UT,United States
|
||||||
|
|
|
||||||
|
|
|
@ -20,6 +20,9 @@ date()
|
||||||
rm(list=ls())
|
rm(list=ls())
|
||||||
library(tidyverse)
|
library(tidyverse)
|
||||||
library(tidycensus)
|
library(tidycensus)
|
||||||
|
library(sf)
|
||||||
|
library(openmeteo)
|
||||||
|
library(maps)
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -30,12 +33,19 @@ census_api_key(key = substr(read_file(file = "api_keys/census_api_key"), 1, 40))
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Date ranges
|
||||||
|
```{r date_range, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE}
|
||||||
|
date_start <- "2010-01-01"
|
||||||
|
date_end <- "2024-12-31"
|
||||||
|
|
||||||
|
```
|
||||||
## Cities to compare
|
## Cities to compare
|
||||||
|
|
||||||
```{r cities, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE}
|
```{r cities, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE}
|
||||||
cities <- read_csv(file = "cities.csv")
|
cities <- read_csv(file = "cities.csv")
|
||||||
cities <- cities %>%
|
cities <- cities %>%
|
||||||
mutate(city_name = paste0(City, " ", Type))
|
mutate(city_name = paste0(City, " ", Type))
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
# Get data
|
# Get data
|
||||||
|
|
@ -49,14 +59,25 @@ for(city in cities$city_name){
|
||||||
geography = "place",
|
geography = "place",
|
||||||
variables = "B01003_001",
|
variables = "B01003_001",
|
||||||
state = state,
|
state = state,
|
||||||
year = 2023
|
year = 2023,
|
||||||
|
geometry = TRUE
|
||||||
) %>%
|
) %>%
|
||||||
filter(str_detect(NAME, city))
|
filter(str_detect(NAME, city))
|
||||||
}
|
}
|
||||||
|
|
||||||
populations <- bind_rows(populations)
|
populations <- bind_rows(populations)
|
||||||
|
|
||||||
cities <- bind_cols(cities, populations)
|
city_center <- populations %>%
|
||||||
|
st_centroid() %>%
|
||||||
|
st_transform(4326) %>% # Convert to WGS84 (standard lat/lon)
|
||||||
|
mutate(
|
||||||
|
lon = st_coordinates(.)[,1],
|
||||||
|
lat = st_coordinates(.)[,2]
|
||||||
|
) %>%
|
||||||
|
st_drop_geometry() %>%
|
||||||
|
select(lat, lon)
|
||||||
|
|
||||||
|
cities <- bind_cols(cities, populations, city_center)
|
||||||
|
|
||||||
ggplot(cities) +
|
ggplot(cities) +
|
||||||
geom_col(aes(x = City,
|
geom_col(aes(x = City,
|
||||||
|
|
@ -64,26 +85,65 @@ ggplot(cities) +
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Weather
|
## Map cities
|
||||||
|
```{r cities_map, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE}
|
||||||
|
ggplot(data = cities) +
|
||||||
|
geom_polygon(data = map_data(map = "state"),
|
||||||
|
aes(long, lat, group = group),
|
||||||
|
fill = "white", colour = "grey50") +
|
||||||
|
geom_point(aes(x = lon,
|
||||||
|
y = lat),
|
||||||
|
shape = 21,
|
||||||
|
fill = "lightgreen",
|
||||||
|
color = "black",
|
||||||
|
size = 4)
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
## weather
|
||||||
```{r weather, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE}
|
```{r weather, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE}
|
||||||
populations <- list(NULL)
|
weather <- list(NULL)
|
||||||
for(city in cities$city_name){
|
for(city in cities$City){
|
||||||
state <- cities %>% filter(city_name == city) %>% pull(State)
|
city_info <- cities %>% filter(City == city)
|
||||||
populations[[city]] <- get_acs(
|
city_run <- weather_history(
|
||||||
geography = "place",
|
location = c(city_info %>% pull(lat), city_info %>% pull(lon)),
|
||||||
variables = "B01003_001",
|
start = date_start,
|
||||||
state = state,
|
end = date_end,
|
||||||
year = 2023
|
daily = c("apparent_temperature_max", "apparent_temperature_min"),
|
||||||
) %>%
|
response_units = list(temperature_unit = "fahrenheit")
|
||||||
filter(str_detect(NAME, city))
|
)
|
||||||
|
city_run$city <- city
|
||||||
|
weather[[city]] <- city_run
|
||||||
}
|
}
|
||||||
|
|
||||||
populations <- bind_rows(populations)
|
weather <- bind_rows(weather)
|
||||||
|
|
||||||
cities <- bind_cols(cities, populations)
|
weather_summary <- weather %>%
|
||||||
|
mutate(year = year(ymd(date)),
|
||||||
|
month = month(ymd(date))) %>%
|
||||||
|
group_by(year, city) %>%
|
||||||
|
summarise(days_above_80 = sum(daily_apparent_temperature_max > 80)) %>%
|
||||||
|
group_by(city) %>%
|
||||||
|
summarise(median_days_above_80 = median(days_above_80))
|
||||||
|
|
||||||
ggplot(cities) +
|
ggplot(data = weather_summary) +
|
||||||
geom_col(aes(x = City,
|
geom_col(aes(x = city,
|
||||||
y = estimate))
|
y = median_days_above_80)) +
|
||||||
|
labs(title = "Days above 80°F",
|
||||||
|
y = "Median days per year",
|
||||||
|
x = "City",
|
||||||
|
fill = NULL)
|
||||||
|
|
||||||
|
ggplot(data = weather %>% pivot_longer(cols = starts_with("daily"), names_to = "max_min", values_to = "temperature") %>% filter(max_min %in% c("daily_apparent_temperature_min", "daily_apparent_temperature_max"))) +
|
||||||
|
geom_violin(aes(x = city,
|
||||||
|
y = temperature,
|
||||||
|
fill = max_min)) +
|
||||||
|
scale_fill_manual(labels = c("daily max", "daily min"),
|
||||||
|
values = c("firebrick", "dodgerblue")) +
|
||||||
|
labs(title = "Apparent Temperature",
|
||||||
|
y = "°F",
|
||||||
|
x = "City",
|
||||||
|
fill = NULL)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue