From f05ef58e5c10647dd688afa798aae002dcd2a0c7 Mon Sep 17 00:00:00 2001 From: dylanbenzi Date: Thu, 9 Apr 2026 00:12:51 -0400 Subject: [PATCH] add pie chart for fatality data --- app/server.R | 47 +++++++++++++++++++++++++++++++++++++++++++++++ app/ui.R | 7 ++++--- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/app/server.R b/app/server.R index b25af77..3218a81 100644 --- a/app/server.R +++ b/app/server.R @@ -930,4 +930,51 @@ server <- function(input, output, session) { ) %>% config(displayModeBar = FALSE) }) + + output$fatality_pie <- renderPlotly({ + data <- fatality_heatmap_data() + + pie_data <- data %>% + mutate( + category = case_when( + fatality_type %in% c("wind", "tree_fall") ~ "Wind", + fatality_type %in% c("surf", "rip_current") ~ "Surf", + fatality_type == "freshwater_floods" ~ "Freshwater Flood", + fatality_type == "offshore" ~ "Offshore", + fatality_type == "storm_surge" ~ "Storm Surge", + fatality_type == "tornado" ~ "Tornado", + fatality_type == "unknown" ~ "Unknown", + TRUE ~ NA_character_ + ) + ) %>% + filter(!is.na(category)) %>% + group_by(category) %>% + summarise(n = sum(n), .groups = "drop") %>% + filter(n > 0) + + category_colors <- c( + "Wind" = "#cd2626", + "Surf" = "#4f94cd", + "Offshore" = "#5cacee", + "Storm Surge" = "#4682b4", + "Freshwater Flood" = "#36648b", + "Tornado" = "#8b1a1a", + "Unknown" = "#ffa500" + ) + + plot_ly( + data = pie_data, + labels = ~category, + values = ~n, + type = "pie", + marker = list(colors = unname(category_colors[pie_data$category])), + textinfo = "label+value", + hovertemplate = "%{label}: %{value}" + ) %>% + layout( + legend = list(orientation = "h", x = 0.5, xanchor = "center", y = -0.1), + margin = list(l = 10, r = 10, t = 10, b = 10) + ) %>% + config(displayModeBar = FALSE) + }) } diff --git a/app/ui.R b/app/ui.R index 033d929..954323a 100644 --- a/app/ui.R +++ b/app/ui.R @@ -243,7 +243,6 @@ ui <- page_navbar( ) ) ), - nav_panel("Perils"), nav_panel( "Fatality Map", layout_columns( @@ -262,9 +261,11 @@ ui <- page_navbar( layout_column_wrap( width = 1, card( - plotlyOutput("fatality_heatmap", height = "100%") + plotlyOutput("fatality_pie", height = "100%") ), - card() + card( + plotlyOutput("fatality_heatmap", height = "100%") + ) ) ) ),