Skip to content

Commit

Permalink
Merge pull request #61 from jakobdanel/results/direct-neigbours
Browse files Browse the repository at this point in the history
Results/direct neigbours
  • Loading branch information
jakobdanel authored Jan 22, 2024
2 parents d274223 + ecc1030 commit 0897272
Show file tree
Hide file tree
Showing 23 changed files with 940 additions and 8 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export(lfa_count_returns_all_areas)
export(lfa_count_returns_per_tree)
export(lfa_create_boxplot)
export(lfa_create_density_plots)
export(lfa_create_neighbor_mean_curves)
export(lfa_create_plot_per_area)
export(lfa_create_stacked_distributions_plot)
export(lfa_create_stacked_histogram)
export(lfa_create_tile_location_objects)
Expand Down
4 changes: 2 additions & 2 deletions R/create_density_plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
lfa_create_density_plots <-
function(data,
value_column,
category_column1,
category_column2,
category_column1 = "area",
category_column2 = "specie",
title = NULL,
xlims = NULL,
ylims = NULL) {
Expand Down
56 changes: 56 additions & 0 deletions R/create_neighbour_mean_curves.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#' Create neighbor mean curves for specified areas
#'
#' 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.
#'
#' @param neighbors A data frame containing information about neighbors, where each column represents
#' a specific neighbor, and each row corresponds to an area.
#' @param use_avg Logical. If TRUE, the function computes average curves across all neighbors.
#' If FALSE, it computes curves for individual neighbors.
#' @return A data frame with mean curves for each specified area.
#' Columns represent areas, and rows represent index values.
#'
#' @examples
#' # 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)
#'
#' @export
lfa_create_neighbor_mean_curves <- function(neighbors, use_avg = FALSE) {
# Get information about all areas
all_areas <- lfa::lfa_get_all_areas()

# Create a data frame with an 'index' column ranging from 1 to 100
df <- data.frame(index = 1:100)

# Define column names based on whether to use averages or individual neighbors
if (use_avg) {
names <- paste0("avg_", 1:100)
} else {
names <- paste0("Neighbor_", 1:100)
}

# Iterate over each area and compute mean curves
for (area in 1:nrow(all_areas)) {
area_name <- all_areas[area, "area"]

# Subset data for the current area
subset <- neighbors[neighbors$area == area_name, ]

# Initialize an empty vector to store mean values
vec <- NULL

# Compute mean values for each neighbor
for (name in names) {
vec <- c(vec, mean(subset[[name]], na.rm = TRUE))
}

# Add the vector as a new column to the data frame
df[[area_name]] <- vec
}

# Remove the 'index' column
df$index <- NULL

return(df)
}
44 changes: 44 additions & 0 deletions R/create_plot_per_area.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#' Create a line plot per area with one color per specie
#'
#' 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.
#'
#' @param data A data frame with numeric columns and a column named 'specie' for species information.
#' @return A ggplot2 line plot.
#' @export
#'
#' @examples
#' 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)
#'
#'@export
lfa_create_plot_per_area <- function(data) {
# Get all areas and species information
areas_specie <- lfa::lfa_get_all_areas()

# Create an "index" column using the row numbers
data$index <- seq_len(nrow(data))

# Reshape the data using tidyr's gather function
data_long <- tidyr::gather(data, key = "area", value = "value", -index)

# Perform a left join with areas_specie
data_long <- dplyr::left_join(data_long, areas_specie, by = "area")

# Create a line plot using ggplot2 with colors based on specie and one line per area
return(
ggplot2::ggplot(data_long, ggplot2::aes(x = index, y = value, color = specie, group = area)) +
ggplot2::geom_line() +
ggplot2::labs(
title = "Average Distance to n-nearest Neighbors across all patches",
x = "n",
y = "Average Distance to n-nearest Neighbor (m)"
) +
ggplot2::theme_minimal()
)
}
4 changes: 2 additions & 2 deletions man/lfa_create_density_plots.Rd

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

29 changes: 29 additions & 0 deletions man/lfa_create_neighbor_mean_curves.Rd

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

28 changes: 28 additions & 0 deletions man/lfa_create_plot_per_area.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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified results/_freeze/report/figure-html/unnamed-chunk-9-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions results/appendix/build_quantitativ_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ def main():
content = build_quantitativ_results("z-values", "Distribution of Z-Values", '"Z"', header_size=3)
write_file("z_values.qmd", content)

preprocessing_nearest ="data <- lfa::lfa_combine_sf_obj(lfa::lfa_get_neighbor_paths(),lfa::lfa_get_all_areas())"
write_file("nearest_1.qmd", build_quantitativ_results("nearest-neighbor-1", "Distribution of nearest neighbor distances", '"Neighbor_1"', header_size=4, preprocessing=preprocessing_nearest))
write_file("nearest_100.qmd", build_quantitativ_results("nearest-neighbor-100", "Distribution of distances to 100th nearest neighbor", '"Neighbor_100"', header_size=4, preprocessing=preprocessing_nearest))

preprocessing_nearest_avg= preprocessing_nearest+ "\n" +"""names <- paste0("Neighbor_",1:100)
data$avg = rowMeans(dplyr::select(as.data.frame(data),names))"""
write_file("nearest_avg.qmd", build_quantitativ_results("nearest-neighbor-avg", "Distribution of average nearest neighbor distances", '"avg"', header_size=4, preprocessing=preprocessing_nearest_avg))


preprocessing_number_of_returns = """data <- sf::st_read("data/tree_properties.gpkg")
neighbors <- lfa::lfa_get_neighbor_paths() |> lfa::lfa_combine_sf_obj(lfa::lfa_get_all_areas())
Expand Down
Loading

0 comments on commit 0897272

Please sign in to comment.