Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order trait coverage labels in decreasing order #95

Merged
merged 2 commits into from
Jan 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions R/fb_plot_site_traits_completeness.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ fb_plot_site_traits_completeness <- function(
function(x) site_species[, c("site", x), drop = FALSE]
)


# Compute site-level trait coverage for all traits
all_coverage <- lapply(
site_species_categories,
function(x) fb_get_all_trait_coverages_by_site(
Expand Down Expand Up @@ -98,10 +100,11 @@ fb_plot_site_traits_completeness <- function(
)


# Get average coverage
# Compute average trait coverage across all sites for each trait
avg_coverage <- lapply(
all_coverage,
function(x) {

avg_coverage <- by(
x, x$coverage_name,
function(y) mean(y$coverage_value, na.rm = TRUE)
Expand All @@ -118,13 +121,21 @@ fb_plot_site_traits_completeness <- function(
)
)

avg_coverage <- avg_coverage[, c("cov_label", "coverage_name")]
avg_coverage <- t(utils::unstack(avg_coverage))
# Re-order to make sure of coverage is decreasing
avg_coverage <- avg_coverage[
order(avg_coverage$avg_coverage, decreasing = TRUE),
]

# Transform into factor to keep order
avg_coverage$cov_label <- factor(
avg_coverage$cov_label, levels = avg_coverage$cov_label
)

return(avg_coverage)
}
)


# Add categories back into data.frame
# Add species categories back into final data.frame
all_coverage <- lapply(
names(all_coverage),
function(x) {
Expand All @@ -143,10 +154,7 @@ fb_plot_site_traits_completeness <- function(
names(avg_coverage),
function(x) {

given_coverage <- data.frame(
coverage_name = colnames(avg_coverage[[x]]),
avg_coverage = as.character(avg_coverage[[x]])
)
given_coverage <- avg_coverage[[x]]

given_coverage[category_name] <- x

Expand Down Expand Up @@ -183,8 +191,8 @@ fb_plot_site_traits_completeness <- function(
ggplot2::ggplot(
all_coverage,
ggplot2::aes(
interaction(.data$avg_coverage, .data[[category_name]], sep = "__"), .data$site,
fill = .data$coverage_value
interaction(.data$cov_label, .data[[category_name]], sep = "__"),
.data$site, fill = .data$coverage_value
)
) +
ggplot2::geom_tile() +
Expand Down
Loading