Refactor Location Analysis: add narrative descriptions, clarify map/table context, and improve accessibility.

This commit is contained in:
Nick Heppler 2025-05-07 12:03:05 -04:00
parent 4042c36fff
commit eb279c8f5a

View File

@ -580,6 +580,9 @@ survey_data %>%
``` ```
# Location Analysis {.tabset} # Location Analysis {.tabset}
[Back to Top](#)
```{r func-plot_geographic_data, echo=TRUE} ```{r func-plot_geographic_data, echo=TRUE}
plot_geographic_data <- function(joined_data, plot_geographic_data <- function(joined_data,
title, title,
@ -593,54 +596,42 @@ plot_geographic_data <- function(joined_data,
na_fill_color = "lightgrey") { na_fill_color = "lightgrey") {
current_date <- format(Sys.Date(), "%B %d, %Y") current_date <- format(Sys.Date(), "%B %d, %Y")
# If subtitle is not provided, use the current date as subtitle
subtitle_text <- ifelse(is.null(subtitle), paste("Date:", current_date), subtitle) subtitle_text <- ifelse(is.null(subtitle), paste("Date:", current_date), subtitle)
# Handle missing data by filling with a specified color
joined_data[is.na(joined_data$total_trees), "total_trees"] <- NA joined_data[is.na(joined_data$total_trees), "total_trees"] <- NA
# Select the color scale based on the user's input
if (color_scale == "viridis") { if (color_scale == "viridis") {
fill_color <- scale_fill_viridis_c(option = fill_option, na.value = na_fill_color) # Use na.value to fill NA fill_color <- scale_fill_viridis_c(option = fill_option, na.value = na_fill_color)
} else if (color_scale == "RColorBrewer") { } else if (color_scale == "RColorBrewer") {
fill_color <- scale_fill_brewer(palette = "Set3") # Default RColorBrewer palette fill_color <- scale_fill_brewer(palette = "Set3")
} else { } else {
fill_color <- scale_fill_manual(values = color_scale) # Custom color scale fill_color <- scale_fill_manual(values = color_scale)
} }
# Create the plot
plot <- ggplot(data = joined_data) + plot <- ggplot(data = joined_data) +
geom_sf(aes(fill = total_trees), color = "white") + geom_sf(aes(fill = total_trees), color = "white") +
fill_color + # Color scale for the plot fill_color +
theme_options + # Apply custom theme theme_options +
labs(title = title, labs(title = title,
subtitle = subtitle_text, # Subtitle is handled here subtitle = subtitle_text,
fill = legend) + fill = legend) +
theme(axis.text = element_blank(), axis.title = element_blank(), theme(axis.text = element_blank(),
axis.title = element_blank(),
legend.position = legend_position) legend.position = legend_position)
# If save_path is provided, save the plot to file
if (!is.null(save_path)) { if (!is.null(save_path)) {
ggsave(save_path, plot = plot, width = 10, height = 6) ggsave(save_path, plot = plot, width = 10, height = 6)
} }
# Return the plot
return(plot) return(plot)
} }
``` ```
[Back to Top](#)
## By Region ## By Region
This map displays the **total number of trees planted** across each economic region in **New York State**. The counties are color-coded, with darker shades representing areas where more trees have been planted. This allows users to quickly see which counties have had the most extensive tree planting efforts. This map shows the **total number of trees planted** in each of New Yorks economic development regions. The shading reflects the volume of planting activity, with darker areas representing higher totals.
- **What to look for**: Use this map to identify which regions are leading in planting activity, and where more outreach or support might be beneficial.
- **Dark colors**: Indicate regions with a higher number of trees planted.
- **Lighter colors**: Represent regions with fewer trees planted.
The map provides a visual overview of tree planting distribution across New York, making it easier to identify areas with the highest impact or need for further action.
```{r create-region-choropleth-map, echo=TRUE, message=FALSE, fig.height=6, fig.width=8} ```{r create-region-choropleth-map, echo=TRUE, message=FALSE, fig.height=6, fig.width=8}
survey_data_aggregated <- survey_data %>% survey_data_aggregated <- survey_data %>%
@ -672,19 +663,19 @@ plot_geographic_data(joined_data = survey_data_joined,
na_fill_color = "lightgrey") na_fill_color = "lightgrey")
``` ```
### Regional Planting Summary
The table below breaks down the total number of trees planted by region. It also shows each regions percentage contribution to overall planting activity across New York State.
```{r create-summary-table-region, echo=TRUE, message=FALSE, fig.height=6, fig.width=8} ```{r create-summary-table-region, echo=TRUE, message=FALSE, fig.height=6, fig.width=8}
create_summary_table(survey_data, "Region", "Number of Trees Planted (Required)", remove_na = FALSE, table_font_size = 16) create_summary_table(survey_data, "Region", "Number of Trees Planted (Required)", remove_na = FALSE, table_font_size = 16)
``` ```
## By County ## By County
This map displays the **total number of trees planted** across each county in **New York State**. The counties are color-coded, with darker shades representing areas where more trees have been planted. This allows users to quickly see which counties have had the most extensive tree planting efforts. This map provides a county-level view of total trees planted. Darker counties indicate higher planting activity.
- **What to look for**: This visual helps uncover local patterns within regions, and may guide localized support, outreach, or reporting strategies.
- **Dark colors**: Indicate counties with a higher number of trees planted.
- **Lighter colors**: Represent counties with fewer trees planted.
The map provides a visual overview of tree planting distribution across New York, making it easier to identify areas with the highest impact or need for further action.
```{r create-county-choropleth-map, echo=TRUE, message=FALSE, fig.height=6, fig.width=8} ```{r create-county-choropleth-map, echo=TRUE, message=FALSE, fig.height=6, fig.width=8}
survey_data_aggregated <- survey_data %>% survey_data_aggregated <- survey_data %>%
@ -693,12 +684,11 @@ survey_data_aggregated <- survey_data %>%
geographic_data <- counties(state = "NY", cb = TRUE, progress = FALSE) %>% geographic_data <- counties(state = "NY", cb = TRUE, progress = FALSE) %>%
st_as_sf() %>% st_as_sf() %>%
mutate(NAME = str_replace(NAME, "\\.", "")) # Remove period from "St. Lawrence" mutate(NAME = str_replace(NAME, "\\.", ""))
survey_data_joined <- geographic_data %>% survey_data_joined <- geographic_data %>%
left_join(survey_data_aggregated, by = c("NAME" = "County")) left_join(survey_data_aggregated, by = c("NAME" = "County"))
# Example of calling the function with enhancements
plot_geographic_data(joined_data = survey_data_joined, plot_geographic_data(joined_data = survey_data_joined,
title = "Number of Trees Planted by County in New York", title = "Number of Trees Planted by County in New York",
legend = "Total Trees Planted", legend = "Total Trees Planted",
@ -706,13 +696,16 @@ plot_geographic_data(joined_data = survey_data_joined,
subtitle = "Generated: March 13, 2025", subtitle = "Generated: March 13, 2025",
theme_options = theme_minimal(), theme_options = theme_minimal(),
legend_position = "right", legend_position = "right",
color_scale = "viridis", # Default viridis scale color_scale = "viridis",
na_fill_color = "lightgrey") # Color for NA values na_fill_color = "lightgrey")
``` ```
```{r create-summary-table-county, echo=TRUE, message=FALSE, , fig.height=6, fig.width=8} ### County-Level Planting Summary
create_summary_table(survey_data, "County", "Number of Trees Planted (Required)", remove_na = FALSE, table_font_size = 16)
This table provides a detailed breakdown of trees planted by county. Use it alongside the map to validate trends or investigate specific areas.
```{r create-summary-table-county, echo=TRUE, message=FALSE, fig.height=6, fig.width=8}
create_summary_table(survey_data, "County", "Number of Trees Planted (Required)", remove_na = FALSE, table_font_size = 16)
``` ```
# Tree Analysis {.tabset} # Tree Analysis {.tabset}