# Calculate summary statistics by region and year
regional_summary <- respiratory_data |>
group_by(region, year) |>
summarise(
avg_rate = mean(value, na.rm = TRUE),
total_death = sum(count, na.rm = TRUE),
.groups = "drop"
)
# View the summary
regional_summary |>
head(10) |>
kable()| region | year | avg_rate | total_death |
|---|---|---|---|
| East Midlands region (statistical) | 2001 | 40.55736 | 10446.786 |
| East Midlands region (statistical) | 2002 | 39.58696 | 10318.359 |
| East Midlands region (statistical) | 2003 | 40.40365 | 10538.725 |
| East Midlands region (statistical) | 2004 | 37.69815 | 10033.879 |
| East Midlands region (statistical) | 2005 | 38.39410 | 10165.317 |
| East Midlands region (statistical) | 2006 | 35.57484 | 9820.415 |
| East Midlands region (statistical) | 2007 | 36.29523 | 10159.907 |
| East Midlands region (statistical) | 2008 | 35.90586 | 10204.842 |
| East Midlands region (statistical) | 2009 | 35.08595 | 10121.897 |
| East Midlands region (statistical) | 2010 | 33.14834 | 9816.854 |
Interactive: Trends in respiratory mortality by region (hover for details)
# Plot trends over time
plot_day1 <- ggplot(regional_summary,
aes(
x = year,
y = avg_rate,
color = region,
text = paste("Region", region,
"<br>Year", year,
"<br>Rate", round(avg_rate, 1),
"<br>Death", round(total_death, 0)
))) +
geom_line(aes(group = region), linewidth = .5) +
geom_point(size = 1) +
scale_color_viridis_d() +
labs(
title = "Respiratory mortality trends by region",
subtitle = "Hover over lines for exact values. Click legend items to toggle regions.",
x = "Year",
y = "Rate per 100,000",
color = "Region"
) +
theme_minimal()+
theme(legend.position = "bottom")
# Convert to interactive plotly
ggplotly(plot_day1, tooltip = "text") |>
layout(
title = list(
text = paste0("<b>Respiratory mortality trends by region</b><br>",
"<sup>Hover over lines for values. Click legend items to toggle regions.</sup>")
),
legend = list(
orientation = "h",
xanchor = "center",
x = 0.5,
y = -0.3,
traceorder = "normal",
font = list(size = 10),
itemsizing = "small",
tracegroupgap = 5
)) |>
layout(legend = list(itemclick = "toggle", groupclick = "toggle"))Interactive: Trends in respiratory mortality by region (hover for details)