-
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 #61 from jakobdanel/results/direct-neigbours
Results/direct neigbours
- Loading branch information
Showing
23 changed files
with
940 additions
and
8 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
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,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) | ||
} |
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,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() | ||
) | ||
} |
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.
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.
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.
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.