add metrics table and flatten vector graphics

This commit is contained in:
2025-12-20 22:56:53 -05:00
parent 4f779ce1ad
commit 704d475139
+107 -67
View File
@@ -1,18 +1,23 @@
---
title: "`r params$hurdat_id` `r params$storm_name` `r params$storm_year` STORM REPORT"
title: "`r paste(params$hurdat_id, params$storm_name, params$storm_year)` STORM REPORT"
subtitle: "Generated on: `r Sys.Date()`"
format:
pdf:
theme: cosmo
keep-tex: false
dev: png
geometry:
- top=0.75in
- bottom=0.75in
- left=0.75in
- right=0.75in
- footskip=0.3in
embed-resourced: true
fig-format: png
execute:
warning: false
message: false
dpi: 100
params:
storm_basin: AL
storm_year: 1900
@@ -42,7 +47,7 @@ storm <- list(
storm_name = params$storm_name
)
latest_normalization_year <- get_latest_normalization_year()
latest_normalization_year <- get_latest_normalization_year()$latest_year
unique_lfs <- get_unique_lf_ids(storm)
hurdat_id <- get_hurdat_id(storm)
@@ -182,10 +187,8 @@ storm_track <- get_hurdat_track(storm)
panel.background = element_rect(fill = "#D4E6F1"),
panel.grid.major = element_line(color = "#BBBBBB", size = 0.2),
legend.position = "right",
legend.key.size = unit(0.4, "cm"),
legend.text = element_text(size = 8)
) +
plot_theme
legend.text = element_text(size = 10)
)
if (nrow(storm_track) >= 2) {
for (i in 1:(nrow(storm_track) - 1)) {
@@ -259,7 +262,8 @@ storm_track <- get_hurdat_track(storm)
title = paste(params$storm_name, params$storm_year),
x = "Longitude",
y = "Latitude"
)
) +
plot_theme
print(p)
```
@@ -268,6 +272,7 @@ storm_track <- get_hurdat_track(storm)
```{r track_table, echo=FALSE}
landfall_rows <- which(storm_track$record_identifier == "L")
landfall_colors <- paste0(storm_track$line_color[landfall_rows], "A0")
storm_track %>%
select(
@@ -297,7 +302,7 @@ storm_track %>%
kable_styling(
#latex_options = c("striped"),
font_size = 11,
position = "center"
position = "left"
) %>%
column_spec(
2,
@@ -308,7 +313,7 @@ storm_track %>%
row_spec(
landfall_rows,
color = "white",
background = paste0(storm_track$line_color[landfall_rows], "A0")
background = landfall_colors
) %>%
column_spec(
6,
@@ -339,27 +344,53 @@ storm_track %>%
base_yearly_econ <- get_yearly_economics(storm$storm_year)
base_yearly_usa <- get_yearly_usa_pop_hu(storm$storm_year)
# year, ccn, gdp, pop, housing
base_yearly_metrics <- base_yearly_usa %>%
left_join(base_yearly_econ, by = "year") %>%
select(-aggregate_storm_loss)
select(-aggregate_storm_loss)
ref_yearly_econ <- get_yearly_economics(latest_normalization_year)
ref_yearly_usa <- get_yearly_usa_pop_hu(latest_normalization_year)
ref_yearly_metrics <- ref_yearly_usa %>%
left_join(ref_yearly_econ, by = "year") %>%
ref_yearly_metrics <- ref_yearly_usa %>%
left_join(ref_yearly_econ, by = "year") %>%
select(-aggregate_storm_loss)
# year, ccn, gdp, ag loss
# base...
# ref...
metrics_table <- data.frame(
Metric = c("CCNSFACDG", "GDP Deflator", "US Population", "US Housing"),
Base_Year = c(
base_yearly_metrics$ccnsfacdg,
base_yearly_metrics$gdp_deflator,
base_yearly_metrics$population,
base_yearly_metrics$housing
),
Latest_Year = c(
ref_yearly_metrics$ccnsfacdg,
ref_yearly_metrics$gdp_deflator,
ref_yearly_metrics$population,
ref_yearly_metrics$housing
)
) %>%
mutate(
Ratio = Latest_Year / Base_Year
)
# table cols metric, base year, ref year, ratio
# ccn
# gdp
# us pop
# us hous
# rwpc/hu
metrics_table %>%
kable(
col.names = c(
"Metric",
paste("Base Year (", storm$storm_year, ")", sep = ""),
paste("Ref Year (", latest_normalization_year, ")", sep = ""),
"Ratio"
),
align = c("l", "r", "r", "r"),
booktabs = TRUE,
digits = 2
) %>%
kable_styling(
font_size = 11,
position = "left"
)
costs <- get_all_normalized_cost_index(storm)
@@ -372,31 +403,60 @@ for(lf in unique_lf_ids) {
filter(
full_lf_id == lf
)
last_year <- filtered_costs %>% filter(normalization_year == max(normalization_year))
cost_plot <- ggplot(filtered_costs, aes(x = normalization_year)) +
geom_line(aes(y = mmh_loss), color = "blue") +
geom_line(aes(y = mmp_loss), color = "red") +
geom_text(
data = last_year,
aes(x = normalization_year + 1, y = mmh_loss, label = "MMH"),
hjust = 0,
size = 3,
color = "blue"
) +
geom_text_repel(
data = last_year,
aes(x = normalization_year + 1, y = mmp_loss, label = "MMP"),
hjust = 0,
size = 3,
color = "red"
) +
scale_y_continuous(labels = scales::label_dollar(scale_cut = cut_short_scale())) +
coord_cartesian(clip = "off") +
labs(
title = paste(lf, "MMH and MMP Loss"),
x = "Year",
y = "Aggregate Loss (USD)"
) +
theme_minimal() +
plot_theme +
theme(plot.margin = margin(5.5, 40, 5.5, 5.5))
mmh_plot <- ggplot(filtered_costs, aes(x = normalization_year, y = mmh_loss)) +
geom_line(color = "blue") +
scale_y_continuous(labels = scales::label_dollar(scale_cut = cut_short_scale())) +
labs(
title = paste(lf, "MMH Loss"),
x = "Year",
y = "Aggregate Loss (USD)"
) +
theme_minimal() +
plot_theme
#mmh_plot <- ggplot(filtered_costs, aes(x = normalization_year, y = mmh_loss)) +
# geom_line(color = "blue") +
# scale_y_continuous(labels = scales::label_dollar(scale_cut = cut_short_scale())) +
# labs(
# title = paste(lf, "MMH Loss"),
# x = "Year",
# y = "Aggregate Loss (USD)"
# ) +
# theme_minimal() +
# plot_theme
mmp_plot <- ggplot(filtered_costs, aes(x = normalization_year, y = mmp_loss)) +
geom_line(color = "red") +
scale_y_continuous(labels = scales::label_dollar(scale_cut = cut_short_scale())) +
labs(
title = paste(lf, "MMP Loss"),
x = "Year",
y = "Aggregate Loss (USD)"
) +
theme_minimal() +
plot_theme
#mmp_plot <- ggplot(filtered_costs, aes(x = normalization_year, y = mmp_loss)) +
# geom_line(color = "red") +
# scale_y_continuous(labels = scales::label_dollar(scale_cut = cut_short_scale())) +
# labs(
# title = paste(lf, "MMP Loss"),
# x = "Year",
# y = "Aggregate Loss (USD)"
# ) +
# theme_minimal() +
# plot_theme
print(mmh_plot)
print(mmp_plot)
print(cost_plot)
}
```
@@ -510,9 +570,10 @@ for(lf in unique_lf_ids) {
group = county_fips)) +
geom_line() +
scale_color_manual(values = housing_color_values) +
geom_text(
geom_text_repel(
data = growth_colored %>% filter(year == max(year)),
aes(label = name, x = year + 1),
direction = "y",
hjust = 0,
size = 3
) +
@@ -542,28 +603,6 @@ for(lf in unique_lf_ids) {
expand = FALSE
)
p_growth <- ggplot() +
geom_sf(data = world, fill = "#E5E5E5", color = "#999999", size = 0.3) +
geom_sf(data = states, fill = NA, color = "#CCCCCC", size = 0.2) +
geom_sf(data = growth_sf, fill = "red", color = "#E5E5E5", size = 0.5, alpha = growth_sf$population_opacity) +
geom_sf_label(data = growth_sf, aes(label = growth_sf$name)) +
coord_sf(
xlim = c(lon_range[1] - lon_padding, lon_range[2] + lon_padding),
ylim = c(lat_range[1] - lat_padding, lat_range[2] + lat_padding),
expand = FALSE
) +
labs(
title = paste(lf, "Housing Growth Map By County"),
x = "",
y = ""
)
theme_minimal() +
theme(
panel.background = element_rect(fill = "#D4E6F1"),
panel.grid.major = element_line(color = "#BBBBBB", size = 0.2),
) +
plot_theme
population_colors <- latest_opacity %>%
distinct(name, population_opacity) %>%
arrange(desc(population_opacity)) %>%
@@ -582,9 +621,10 @@ for(lf in unique_lf_ids) {
group = county_fips)) +
geom_line() +
scale_color_manual(values = population_color_values) +
geom_text(
geom_text_repel(
data = growth_colored %>% filter(year == max(year)),
aes(label = name, x = year + 1),
direction = "y",
hjust = 0,
size = 3
) +