Refactor Tree Analysis section: enhance narratives, clarify genus/species summaries, and format output tables.
This commit is contained in:
parent
eb279c8f5a
commit
6a95bef585
70
report.Rmd
70
report.Rmd
@ -712,83 +712,89 @@ create_summary_table(survey_data, "County", "Number of Trees Planted (Required)"
|
|||||||
|
|
||||||
[Back to Top](#)
|
[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}
|
```{r func-create_species_summary_table, echo=TRUE}
|
||||||
create_species_summary_table <- function(data, field, field_label = NULL) {
|
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 %>%
|
data <- data %>%
|
||||||
mutate(
|
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 %>%
|
data <- data %>%
|
||||||
mutate(
|
mutate(
|
||||||
!!sym(field) := gsub("_", " ", !!sym(field)), # Replace underscores with spaces
|
!!sym(field) := gsub("_", " ", !!sym(field)),
|
||||||
!!sym(field) := tools::toTitleCase(!!sym(field)) # Convert to title case
|
!!sym(field) := tools::toTitleCase(!!sym(field))
|
||||||
)
|
)
|
||||||
|
|
||||||
# Summarize the data based on the field (e.g., Generic.Species.of.Tree)
|
# Summarize
|
||||||
summary_data <- data %>%
|
summary_data <- data %>%
|
||||||
group_by(!!sym(field)) %>%
|
group_by(!!sym(field)) %>%
|
||||||
summarise(
|
summarise(
|
||||||
submissions = n(), # Count of surveys for each species (or category)
|
submissions = n(),
|
||||||
.groups = "drop" # To prevent issues with group structure
|
.groups = "drop"
|
||||||
) %>%
|
) %>%
|
||||||
mutate(
|
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 %>%
|
summary_data_formatted <- summary_data %>%
|
||||||
mutate(
|
mutate(
|
||||||
submissions = scales::comma(submissions), # Format the submission counts with commas
|
submissions = scales::comma(submissions),
|
||||||
submissions_percentage = paste0(round(submissions_percentage, 1), "%") # Round percentage and append '%'
|
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)
|
label <- ifelse(is.null(field_label), field, field_label)
|
||||||
|
|
||||||
# Create and style the table
|
# Return table
|
||||||
summary_data_formatted %>%
|
summary_data_formatted %>%
|
||||||
knitr::kable(col.names = c(label, "Number of Surveys", "Proportion of Surveys (%)"),
|
knitr::kable(
|
||||||
|
col.names = c(label, "Number of Surveys", "Proportion of Surveys (%)"),
|
||||||
caption = paste("Summary of Surveys by", label),
|
caption = paste("Summary of Surveys by", label),
|
||||||
align = c("l", "c", "c")) %>% # Align the columns (left for the field, center for others)
|
align = c("l", "c", "c")
|
||||||
|
) %>%
|
||||||
kableExtra::kable_styling(
|
kableExtra::kable_styling(
|
||||||
full_width = F,
|
full_width = F,
|
||||||
position = "center",
|
position = "center",
|
||||||
bootstrap_options = c("striped", "hover"),
|
bootstrap_options = c("striped", "hover"),
|
||||||
font_size = 14
|
font_size = 14
|
||||||
) %>%
|
) %>%
|
||||||
kableExtra::column_spec(1, width = "20em", bold = TRUE) %>% # First column (Species) bold and wider
|
kableExtra::column_spec(1, width = "20em", bold = TRUE) %>%
|
||||||
kableExtra::column_spec(2, width = "12em") %>% # Number of Surveys column
|
kableExtra::column_spec(2, width = "12em") %>%
|
||||||
kableExtra::column_spec(3, width = "12em") %>% # Proportion column
|
kableExtra::column_spec(3, width = "12em") %>%
|
||||||
kableExtra::add_footnote("The proportions represent the percentage of surveys for each species relative to the total surveys.")
|
kableExtra::add_footnote("The proportions represent the percentage of surveys for each group relative to the total number of surveys.")
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## By Genus
|
## 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.
|
* **"Number of Surveys"**: Total surveys that mention each genus.
|
||||||
2. **Proportion of Surveys (%)**: The percentage of total surveys that reported this genus, relative to the entire dataset.
|
* **"Proportion of Surveys (%)"**: Share of each 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.
|
* **"Not Provided"**: Includes submissions where the genus was not specified.
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
```{r create-summary-table-genus, echo=TRUE, message=FALSE, fig.height=6, fig.width=8}
|
```{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")
|
create_species_summary_table(species_planted, "Generic Type of Tree (Optional)", "Tree Genus")
|
||||||
```
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## By Species
|
## 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.
|
* **"Number of Surveys"**: Total submissions for each species.
|
||||||
2. **Proportion of Surveys (%)**: The percentage of total surveys that reported this species, relative to the entire dataset.
|
* **"Proportion of Surveys (%)"**: Percentage of all surveys reporting the species.
|
||||||
3. **"Not Provided" Category**: Any surveys that did not specify a species are grouped under the "Not Provided" category.
|
* **"Not Provided"**: Surveys that omitted species details.
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
```{r create-summary-table-species, echo=TRUE, message=FALSE, fig.height=6, fig.width=8}
|
```{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")
|
create_species_summary_table(species_planted, "Tree Species (Optional)", "Tree Species")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user