From 6a95bef5858a11551c099e2a0fdd0797e82d86fd Mon Sep 17 00:00:00 2001 From: Nick Heppler Date: Wed, 7 May 2025 12:41:59 -0400 Subject: [PATCH] Refactor Tree Analysis section: enhance narratives, clarify genus/species summaries, and format output tables. --- report.Rmd | 76 +++++++++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 35 deletions(-) diff --git a/report.Rmd b/report.Rmd index cd6d0dc..42c849a 100644 --- a/report.Rmd +++ b/report.Rmd @@ -712,83 +712,89 @@ create_summary_table(survey_data, "County", "Number of Trees Planted (Required)" [Back to Top](#) +This section analyzes the tree data submitted in surveys, broken down by both genus and species. It provides insight into which types of trees are most commonly planted and the completeness of species-level reporting. + +--- + ```{r func-create_species_summary_table, echo=TRUE} create_species_summary_table <- function(data, field, field_label = NULL) { - # Replace empty strings and NA values with "Not Provided" before summarization + # Replace empty strings and NA values with "Not Provided" data <- data %>% mutate( - !!sym(field) := ifelse(!!sym(field) == "" | is.na(!!sym(field)), "Not Provided", !!sym(field)) # Replace empty strings and NAs + !!sym(field) := ifelse(!!sym(field) == "" | is.na(!!sym(field)), "Not Provided", !!sym(field)) ) - # Clean up the species names: replace underscores with spaces and convert to title case + # Format values: replace underscores, convert to title case data <- data %>% mutate( - !!sym(field) := gsub("_", " ", !!sym(field)), # Replace underscores with spaces - !!sym(field) := tools::toTitleCase(!!sym(field)) # Convert to title case + !!sym(field) := gsub("_", " ", !!sym(field)), + !!sym(field) := tools::toTitleCase(!!sym(field)) ) - - # Summarize the data based on the field (e.g., Generic.Species.of.Tree) + + # Summarize summary_data <- data %>% group_by(!!sym(field)) %>% summarise( - submissions = n(), # Count of surveys for each species (or category) - .groups = "drop" # To prevent issues with group structure + submissions = n(), + .groups = "drop" ) %>% mutate( - submissions_percentage = submissions / sum(submissions) * 100 # Proportion of surveys for each category + submissions_percentage = submissions / sum(submissions) * 100 ) - # Format the table for display + # Format for table summary_data_formatted <- summary_data %>% mutate( - submissions = scales::comma(submissions), # Format the submission counts with commas - submissions_percentage = paste0(round(submissions_percentage, 1), "%") # Round percentage and append '%' + submissions = scales::comma(submissions), + submissions_percentage = paste0(round(submissions_percentage, 1), "%") ) - - # Determine the label for the field + + # Field label for header label <- ifelse(is.null(field_label), field, field_label) - # Create and style the table + # Return table summary_data_formatted %>% - knitr::kable(col.names = c(label, "Number of Surveys", "Proportion of Surveys (%)"), - caption = paste("Summary of Surveys by", label), - align = c("l", "c", "c")) %>% # Align the columns (left for the field, center for others) + knitr::kable( + col.names = c(label, "Number of Surveys", "Proportion of Surveys (%)"), + caption = paste("Summary of Surveys by", label), + align = c("l", "c", "c") + ) %>% kableExtra::kable_styling( full_width = F, position = "center", bootstrap_options = c("striped", "hover"), font_size = 14 ) %>% - kableExtra::column_spec(1, width = "20em", bold = TRUE) %>% # First column (Species) bold and wider - kableExtra::column_spec(2, width = "12em") %>% # Number of Surveys column - kableExtra::column_spec(3, width = "12em") %>% # Proportion column - kableExtra::add_footnote("The proportions represent the percentage of surveys for each species relative to the total surveys.") + kableExtra::column_spec(1, width = "20em", bold = TRUE) %>% + kableExtra::column_spec(2, width = "12em") %>% + kableExtra::column_spec(3, width = "12em") %>% + kableExtra::add_footnote("The proportions represent the percentage of surveys for each group relative to the total number of surveys.") } ``` +--- + ## By Genus -The following table shows a breakdown of survey submissions by **Genus**. For each genus, the table provides: +This table summarizes the number and percentage of surveys by **tree genus**. It helps identify which genera were most frequently planted or reported across all submissions. -1. **Number of Surveys**: The total number of surveys where this genus was reported. -2. **Proportion of Surveys (%)**: The percentage of total surveys that reported this genus, relative to the entire dataset. -3. **"Not Provided" Category**: Any surveys that did not specify a genus are grouped under the "Not Provided" category. - -These figures provide an understanding of which genus are most commonly reported, how prevalent each genus is, and the proportion of surveys where no genus was specified. +* **"Number of Surveys"**: Total surveys that mention each genus. +* **"Proportion of Surveys (%)"**: Share of each genus relative to the entire dataset. +* **"Not Provided"**: Includes submissions where the genus was not specified. ```{r create-summary-table-genus, echo=TRUE, message=FALSE, fig.height=6, fig.width=8} create_species_summary_table(species_planted, "Generic Type of Tree (Optional)", "Tree Genus") ``` +--- + ## By Species -The following table shows a breakdown of survey submissions by **Species**. For each species, the table provides: +This table provides a breakdown of survey submissions by **tree species**. It offers a more detailed view of which species were planted or reported most often. -1. **Number of Surveys**: The total number of surveys where this species was reported. -2. **Proportion of Surveys (%)**: The percentage of total surveys that reported this species, relative to the entire dataset. -3. **"Not Provided" Category**: Any surveys that did not specify a species are grouped under the "Not Provided" category. - -These figures provide an understanding of which species are most commonly reported, how prevalent each species is, and the proportion of surveys where no genus was specified. +* **"Number of Surveys"**: Total submissions for each species. +* **"Proportion of Surveys (%)"**: Percentage of all surveys reporting the species. +* **"Not Provided"**: Surveys that omitted species details. ```{r create-summary-table-species, echo=TRUE, message=FALSE, fig.height=6, fig.width=8} create_species_summary_table(species_planted, "Tree Species (Optional)", "Tree Species")