Skip to content

Commit

Permalink
Merge pull request #63 from jakobdanel/results/density
Browse files Browse the repository at this point in the history
Results/density
  • Loading branch information
jakobdanel authored Jan 23, 2024
2 parents 06d6bb3 + fb03516 commit 288ae8a
Show file tree
Hide file tree
Showing 10 changed files with 359 additions and 16 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export(get_retile_dir)
export(get_tile_dir)
export(get_tiles_path)
export(is_tile_existing)
export(lfa_calculate_patch_density)
export(lfa_capitalize_first_char)
export(lfa_check_flag)
export(lfa_combine_sf_obj)
Expand All @@ -12,6 +13,7 @@ export(lfa_count_returns_all_areas)
export(lfa_count_returns_per_tree)
export(lfa_create_boxplot)
export(lfa_create_density_plots)
export(lfa_create_grouped_bar_plot)
export(lfa_create_neighbor_mean_curves)
export(lfa_create_plot_per_area)
export(lfa_create_stacked_distributions_plot)
Expand Down
47 changes: 47 additions & 0 deletions R/calculate_patch_density.R
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)
}
38 changes: 38 additions & 0 deletions R/create_bar_plot.R
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.

37 changes: 37 additions & 0 deletions man/lfa_calculate_patch_density.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions man/lfa_create_grouped_bar_plot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions results/_freeze/report/execute-results/html.json

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
185 changes: 185 additions & 0 deletions results/appendix/package-docs/docs.qmd
Original file line number Diff line number Diff line change
@@ -1,3 +1,53 @@
### `lfa_calculate_patch_density`

Calculate patch density for specified areas based on detection data


#### Arguments

Argument |Description
------------- |----------------
`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".
`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().


#### Description

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.


#### Value

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

```{r}
#| eval: false
# 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)
```


#### Usage

```{r}
#| eval: false
lfa_calculate_patch_density(
areas_location = "research_areas.shp",
detections = lfa::lfa_get_detections()
)
```



### `lfa_capitalize_first_char`

Capitalize First Character of a String
Expand Down Expand Up @@ -393,6 +443,141 @@ lfa_create_density_plots(



### `lfa_create_grouped_bar_plot`

Create a barplot using ggplot2


#### Arguments

Argument |Description
------------- |----------------
`df` | A data frame containing the relevant columns for the barplot.
`value_column` | The column containing the values to be plotted.
`label_column` | The column used for labeling the bars on the x-axis. Default is "name".
`grouping_column` | The column used for grouping the bars. Default is "species".


#### Description

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.


#### Value

A ggplot2 barplot.


#### Examples

```{r}
#| eval: false
# 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")
```


#### Usage

```{r}
#| eval: false
lfa_create_grouped_bar_plot(data, grouping_var, value_col, label_col)
```



### `lfa_create_neighbor_mean_curves`

Create neighbor mean curves for specified areas


#### Arguments

Argument |Description
------------- |----------------
`neighbors` | A data frame containing information about neighbors, where each column represents a specific neighbor, and each row corresponds to an area.
`use_avg` | Logical. If TRUE, the function computes average curves across all neighbors. If FALSE, it computes curves for individual neighbors.


#### Description

This function generates mean curves for a specified set of areas based on neighbor data.
The user can choose to compute mean curves for individual neighbors or averages across neighbors.


#### Value

A data frame with mean curves for each specified area.
Columns represent areas, and rows represent index values.


#### Examples

```{r}
#| eval: false
# Assuming you have a data frame 'your_neighbors_data' with neighbor information
mean_curves <- lfa_create_neighbor_mean_curves(your_neighbors_data, use_avg = TRUE)
print(mean_curves)
```


#### Usage

```{r}
#| eval: false
lfa_create_neighbor_mean_curves(neighbors, use_avg = FALSE)
```



### `lfa_create_plot_per_area`

Create a line plot per area with one color per specie


#### Arguments

Argument |Description
------------- |----------------
`data` | A data frame with numeric columns and a column named 'specie' for species information.


#### Description

This function takes a data frame containing numeric columns and creates a line plot
using ggplot2. Each line in the plot represents a different area, with one color per specie.


#### Value

A ggplot2 line plot.


#### Examples

```{r}
#| eval: false
data <- data.frame(
specie = rep(c("Species1", "Species2", "Species3"), each = 10),
column1 = rnorm(30),
column2 = rnorm(30),
column3 = rnorm(30)
)
lfa_create_plot_per_area(data)
```


#### Usage

```{r}
#| eval: false
lfa_create_plot_per_area(data)
```



### `lfa_create_stacked_distributions_plot`

Create a stacked distribution plot for tree detections, visualizing the distribution
Expand Down
15 changes: 1 addition & 14 deletions results/report.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,10 @@ This report documents the analysis of forest data for different tree species.
{{< include results/researched-areas.qmd >}}
{{< include results/z-distribution.qmd >}}
{{< include results/number_of_returns.qmd >}}
{{< include results/density.qmd >}}



|specie |area | density (1/m²)|
|:------|:-------------------|---------:|
|beech |bielefeld_brackwede | 0.0089399|
|beech |billerbeck | 0.0093175|
|beech |wuelfenrath | 0.0079259|
|oak |hamm | 0.0090610|
|oak |muenster | 0.0077384|
|oak |rinkerode | 0.0082641|
|pine |greffen | 0.0103807|
|pine |mesum | 0.0124200|
|pine |telgte | 0.0122860|
|spruce |brilon | 0.0158030|
|spruce |oberhundem | 0.0162678|
|spruce |osterwald | 0.0129892|



Expand Down
Loading

0 comments on commit 288ae8a

Please sign in to comment.