add metrics table and flatten vector graphics

This commit is contained in:
2025-12-20 22:56:53 -05:00
parent 4f779ce1ad
commit 704d475139
+98 -58
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()`" subtitle: "Generated on: `r Sys.Date()`"
format: format:
pdf: pdf:
theme: cosmo theme: cosmo
keep-tex: false
dev: png
geometry: geometry:
- top=0.75in - top=0.75in
- bottom=0.75in - bottom=0.75in
- left=0.75in - left=0.75in
- right=0.75in - right=0.75in
- footskip=0.3in - footskip=0.3in
embed-resourced: true
fig-format: png
execute: execute:
warning: false warning: false
message: false message: false
dpi: 100
params: params:
storm_basin: AL storm_basin: AL
storm_year: 1900 storm_year: 1900
@@ -42,7 +47,7 @@ storm <- list(
storm_name = params$storm_name 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) unique_lfs <- get_unique_lf_ids(storm)
hurdat_id <- get_hurdat_id(storm) hurdat_id <- get_hurdat_id(storm)
@@ -182,10 +187,8 @@ storm_track <- get_hurdat_track(storm)
panel.background = element_rect(fill = "#D4E6F1"), panel.background = element_rect(fill = "#D4E6F1"),
panel.grid.major = element_line(color = "#BBBBBB", size = 0.2), panel.grid.major = element_line(color = "#BBBBBB", size = 0.2),
legend.position = "right", legend.position = "right",
legend.key.size = unit(0.4, "cm"), legend.text = element_text(size = 10)
legend.text = element_text(size = 8) )
) +
plot_theme
if (nrow(storm_track) >= 2) { if (nrow(storm_track) >= 2) {
for (i in 1:(nrow(storm_track) - 1)) { 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), title = paste(params$storm_name, params$storm_year),
x = "Longitude", x = "Longitude",
y = "Latitude" y = "Latitude"
) ) +
plot_theme
print(p) print(p)
``` ```
@@ -268,6 +272,7 @@ storm_track <- get_hurdat_track(storm)
```{r track_table, echo=FALSE} ```{r track_table, echo=FALSE}
landfall_rows <- which(storm_track$record_identifier == "L") landfall_rows <- which(storm_track$record_identifier == "L")
landfall_colors <- paste0(storm_track$line_color[landfall_rows], "A0")
storm_track %>% storm_track %>%
select( select(
@@ -297,7 +302,7 @@ storm_track %>%
kable_styling( kable_styling(
#latex_options = c("striped"), #latex_options = c("striped"),
font_size = 11, font_size = 11,
position = "center" position = "left"
) %>% ) %>%
column_spec( column_spec(
2, 2,
@@ -308,7 +313,7 @@ storm_track %>%
row_spec( row_spec(
landfall_rows, landfall_rows,
color = "white", color = "white",
background = paste0(storm_track$line_color[landfall_rows], "A0") background = landfall_colors
) %>% ) %>%
column_spec( column_spec(
6, 6,
@@ -339,6 +344,7 @@ storm_track %>%
base_yearly_econ <- get_yearly_economics(storm$storm_year) base_yearly_econ <- get_yearly_economics(storm$storm_year)
base_yearly_usa <- get_yearly_usa_pop_hu(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 %>% base_yearly_metrics <- base_yearly_usa %>%
left_join(base_yearly_econ, by = "year") %>% left_join(base_yearly_econ, by = "year") %>%
select(-aggregate_storm_loss) select(-aggregate_storm_loss)
@@ -350,16 +356,41 @@ ref_yearly_metrics <- ref_yearly_usa %>%
left_join(ref_yearly_econ, by = "year") %>% left_join(ref_yearly_econ, by = "year") %>%
select(-aggregate_storm_loss) select(-aggregate_storm_loss)
# year, ccn, gdp, ag loss metrics_table <- data.frame(
# base... Metric = c("CCNSFACDG", "GDP Deflator", "US Population", "US Housing"),
# ref... 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 metrics_table %>%
# ccn kable(
# gdp col.names = c(
# us pop "Metric",
# us hous paste("Base Year (", storm$storm_year, ")", sep = ""),
# rwpc/hu 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) costs <- get_all_normalized_cost_index(storm)
@@ -373,30 +404,59 @@ for(lf in unique_lf_ids) {
full_lf_id == lf full_lf_id == lf
) )
mmh_plot <- ggplot(filtered_costs, aes(x = normalization_year, y = mmh_loss)) + last_year <- filtered_costs %>% filter(normalization_year == max(normalization_year))
geom_line(color = "blue") +
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())) + scale_y_continuous(labels = scales::label_dollar(scale_cut = cut_short_scale())) +
coord_cartesian(clip = "off") +
labs( labs(
title = paste(lf, "MMH Loss"), title = paste(lf, "MMH and MMP Loss"),
x = "Year", x = "Year",
y = "Aggregate Loss (USD)" y = "Aggregate Loss (USD)"
) + ) +
theme_minimal() + theme_minimal() +
plot_theme plot_theme +
theme(plot.margin = margin(5.5, 40, 5.5, 5.5))
mmp_plot <- ggplot(filtered_costs, aes(x = normalization_year, y = mmp_loss)) + #mmh_plot <- ggplot(filtered_costs, aes(x = normalization_year, y = mmh_loss)) +
geom_line(color = "red") + # geom_line(color = "blue") +
scale_y_continuous(labels = scales::label_dollar(scale_cut = cut_short_scale())) + # scale_y_continuous(labels = scales::label_dollar(scale_cut = cut_short_scale())) +
labs( # labs(
title = paste(lf, "MMP Loss"), # title = paste(lf, "MMH Loss"),
x = "Year", # x = "Year",
y = "Aggregate Loss (USD)" # y = "Aggregate Loss (USD)"
) + # ) +
theme_minimal() + # theme_minimal() +
plot_theme # plot_theme
print(mmh_plot) #mmp_plot <- ggplot(filtered_costs, aes(x = normalization_year, y = mmp_loss)) +
print(mmp_plot) # 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(cost_plot)
} }
``` ```
@@ -510,9 +570,10 @@ for(lf in unique_lf_ids) {
group = county_fips)) + group = county_fips)) +
geom_line() + geom_line() +
scale_color_manual(values = housing_color_values) + scale_color_manual(values = housing_color_values) +
geom_text( geom_text_repel(
data = growth_colored %>% filter(year == max(year)), data = growth_colored %>% filter(year == max(year)),
aes(label = name, x = year + 1), aes(label = name, x = year + 1),
direction = "y",
hjust = 0, hjust = 0,
size = 3 size = 3
) + ) +
@@ -542,28 +603,6 @@ for(lf in unique_lf_ids) {
expand = FALSE 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 %>% population_colors <- latest_opacity %>%
distinct(name, population_opacity) %>% distinct(name, population_opacity) %>%
arrange(desc(population_opacity)) %>% arrange(desc(population_opacity)) %>%
@@ -582,9 +621,10 @@ for(lf in unique_lf_ids) {
group = county_fips)) + group = county_fips)) +
geom_line() + geom_line() +
scale_color_manual(values = population_color_values) + scale_color_manual(values = population_color_values) +
geom_text( geom_text_repel(
data = growth_colored %>% filter(year == max(year)), data = growth_colored %>% filter(year == max(year)),
aes(label = name, x = year + 1), aes(label = name, x = year + 1),
direction = "y",
hjust = 0, hjust = 0,
size = 3 size = 3
) + ) +