-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from jakobdanel/results/density
Results/density
- Loading branch information
Showing
10 changed files
with
359 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#' Calculate patch density for specified areas based on detection data | ||
#' | ||
#' This function calculates patch density for specified areas using detection data. | ||
#' It reads the spatial polygons from a shapefile, computes the area size for each patch, | ||
#' counts the number of detections in each patch, and calculates the patch density. | ||
#' | ||
#' @param areas_location The file path to a shapefile containing spatial polygons | ||
#' representing the areas for which patch density needs to be calculated. | ||
#' Default is "research_areas.shp". | ||
#' @param detections A data frame containing detection information, where each row represents | ||
#' a detection and includes the 'area' column specifying the corresponding area. | ||
#' Default is obtained using lfa_get_detections(). | ||
#' @return A data frame with patch density information for each specified area. | ||
#' Columns include 'name' (area name), 'geometry' (polygon geometry), 'area_size' (patch area size), | ||
#' 'detections' (number of detections in the patch), and 'density' (computed patch density). | ||
#' | ||
#' @examples | ||
#' # Assuming you have a shapefile 'your_research_areas.shp' and detection data | ||
#' # from lfa_get_detections() | ||
#' density_data <- lfa_calculate_patch_density(areas_location = "your_research_areas.shp") | ||
#' print(density_data) | ||
#' | ||
#' @export | ||
lfa_calculate_patch_density <- function(areas_location = "research_areas.shp", | ||
detections = lfa::lfa_get_detections()) { | ||
# Set S2 usage to FALSE | ||
sf::sf_use_s2(FALSE) | ||
|
||
# Read spatial polygons from the specified shapefile | ||
patch_data <- sf::st_read(areas_location) | ||
|
||
# Compute the area size for each patch | ||
patch_data$area_size <- sf::st_area(patch_data) | ||
|
||
# Initialize 'detections' column with NA | ||
patch_data$detections <- NA | ||
|
||
# Count the number of detections in each patch | ||
for (i in 1:nrow(patch_data)) { | ||
patch_data[i, "detections"]$detections <- nrow(detections[detections$area == patch_data[i, "name"]$name, ]) | ||
} | ||
|
||
# Calculate patch density | ||
patch_data$density <- patch_data$detections / patch_data$area_size | ||
|
||
return(patch_data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#' Create a barplot using ggplot2 | ||
#' | ||
#' This function generates a barplot using ggplot2 based on the specified data frame columns. | ||
#' The barplot displays the values from the specified column, grouped by another column. | ||
#' The grouping can be further differentiated by color if desired. | ||
#' | ||
#' @param df A data frame containing the relevant columns for the barplot. | ||
#' @param value_column The column containing the values to be plotted. | ||
#' @param label_column The column used for labeling the bars on the x-axis. Default is "name". | ||
#' @param grouping_column The column used for grouping the bars. Default is "species". | ||
#' @return A ggplot2 barplot. | ||
#' | ||
#' | ||
#' @examples | ||
#' # Assuming you have a data frame 'your_data_frame' with columns "name", "species", and "value" | ||
#' lfa_create_barplot(your_data_frame, value_column = "value", label_column = "name", grouping_column = "species") | ||
#' | ||
#'@export | ||
lfa_create_grouped_bar_plot <- function(data, grouping_var, value_col, label_col) { | ||
|
||
if (!(grouping_var %in% colnames(data) && value_col %in% colnames(data) && label_col %in% colnames(data))) { | ||
stop("Columns not found in the data.frame.") | ||
} | ||
|
||
# Create a grouped bar plot | ||
plot <- ggplot2::ggplot(data, ggplot2::aes(x = reorder(data[[label_col]], data[[value_col]]), y = data[[value_col]], fill = data[[grouping_var]])) + | ||
ggplot2::geom_bar(stat = "identity", position = "dodge") + | ||
ggplot2::labs(x = "Name of patch", y = "Density", fill = "Specie", title = "Tree density across the different patches, grouped by specie") + | ||
ggplot2::theme_minimal() + | ||
ggplot2::theme(axis.text.x = element_text(angle = 45, hjust = 1)) | ||
|
||
return(plot) | ||
} | ||
|
||
# Example usage: | ||
# Assuming 'my_data' is your data.frame, 'category' is the grouping variable, | ||
# 'value' is the column with values, and 'label' is the column with labels. | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.