-
Notifications
You must be signed in to change notification settings - Fork 1
/
fb_plot_species_traits_completeness.R
243 lines (193 loc) · 6.91 KB
/
fb_plot_species_traits_completeness.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#' Plot Trait Coverage per Species for each Trait
#'
#' Display a binary heatmap visualizing the species x traits matrix with colors
#' displaying present and missing traits. Traits are ordered from the most to
#' the least known (left to right).
#' Species are ordered from the ones with most to the ones with least traits
#' (bottom to top). The proportion of species with non-missing traits is shown
#' on the x-axis labels.
#' An additional column at the very right of the plot named `"all_traits"`
#' shows a summary considering if all other traits are known.
#'
#' @inheritParams fb_get_all_trait_coverages_by_site
#' @param species_categories 2-columns `data.frame` giving species categories
#' `NULL` by default, with the first column describing the species name, and
#' the second column giving their corresponding categories
#'
#' @return a `ggplot2` object
#'
#' @examples
#' data(species_traits)
#' fb_plot_species_traits_completeness(species_traits)
#'
#' @importFrom rlang .data
#' @export
fb_plot_species_traits_completeness <- function(
species_traits, species_categories = NULL, all_traits = TRUE
) {
check_species_categories(species_categories)
# Make dataset long to get all trait values by rows
species_traits_long <- tidyr::pivot_longer(
species_traits, -"species", names_to = "trait",
values_to = "trait_value", values_transform = as.character
)
# Split species by categories (even when there are none)
species_traits_categories <- split_species_categories(
species_traits, species_categories
)
species_traits_long_categories <- species_traits_long
if (!is.null(species_categories)) {
species_traits_long_categories <- merge(
species_traits_long, species_categories,
by.x = "species", by.y = colnames(species_categories)[1]
)
}
# Count Number of Species per Trait (per category class)
number_species_per_trait <- lapply(
species_traits_categories, fb_count_species_by_trait
)
# Get Combination for All Traits (per category class)
number_trait_per_species <- lapply(
species_traits_categories, fb_count_traits_by_species
)
n_max_trait <- ncol(species_traits[, -1, drop = FALSE])
# Get number of species with maximum known trait (per category)
all_traits_list <- lapply(
number_trait_per_species,
function(x) {
with(x, sum(n_traits == n_max_trait))
}
)
# Rename list if they don't have names (e.g., no categories specified)
if (is.null(names(number_trait_per_species))) {
names(number_trait_per_species) <- seq_len(
length(number_trait_per_species)
)
names(number_species_per_trait) <- seq_len(
length(number_species_per_trait)
)
}
# Convert number of species with all traits into comparable row
all_traits_df <- list(number_species_per_trait[[1]][0,])
if (all_traits) {
all_traits_df <- mapply(
function(x, y) {
data.frame(
trait = "all_traits",
n_species = x,
coverage = x/nrow(y)
)
}, all_traits_list, species_traits_categories, SIMPLIFY = FALSE
)
}
# Add number of species with all traits known per category
number_species_per_trait <- mapply(
rbind, number_species_per_trait, all_traits_df, SIMPLIFY = FALSE
)
# Label with name of trait and proportion of species
number_species_per_trait <- lapply(
number_species_per_trait,
function(x) {
x$trait_label <- paste0(
x$trait, "\n(", round(x$coverage * 100, digits = 1), "%)"
)
return(x)
}
)
# Add all traits in long table
all_traits_subset <- lapply(
names(number_trait_per_species), function(x) {
single_category <- number_trait_per_species[[x]]
output_df <- data.frame(
species = single_category$species, trait = "all_traits",
trait_value = ifelse(single_category$n_traits == n_max_trait, TRUE, NA)
)
if (!is.null(species_categories)) {
output_df[[colnames(species_categories)[2]]] <- x
}
return(output_df)
}
)
all_traits_subset <- do.call(rbind, all_traits_subset)
species_traits_long_categories <- rbind(
species_traits_long_categories, all_traits_subset
)
# Add column for value
species_traits_long_categories$has_trait <- ifelse(
!is.na(species_traits_long_categories$trait_value) &
!(species_traits_long_categories$trait_value == "NaN"), TRUE, FALSE
)
# Merge all datasets before plotting
number_species_per_trait <- lapply(
names(number_species_per_trait),
function(x) {
single_category <- number_species_per_trait[[x]]
if (!is.null(species_categories)) {
single_category[[colnames(species_categories)[2]]] <- x
}
return(single_category)
}
)
number_species_per_trait <- do.call(rbind, number_species_per_trait)
number_trait_per_species <- lapply(
names(number_trait_per_species),
function(x) {
single_category <- number_trait_per_species[[x]]
if (!is.null(species_categories)) {
single_category[[colnames(species_categories)[2]]] <- x
}
return(single_category)
}
)
number_trait_per_species <- do.call(rbind, number_trait_per_species)
# Merge full dataset
common_colnames <- intersect(
colnames(species_traits_long_categories), colnames(number_species_per_trait)
)
species_traits_long_categories <- merge(
species_traits_long_categories, number_species_per_trait,
by.x = common_colnames, by.y = common_colnames
)
# Conditional facet
if (!is.null(species_categories)) {
category_facet <- ggplot2::facet_wrap(
ggplot2::vars(
!!rlang::sym(colnames(species_categories)[[2]])), scales = "free"
)
} else {
category_facet <- NULL
}
# Clean environment for clean ggplot2 object
rm(all_traits, all_traits_df, all_traits_list, all_traits_subset,
common_colnames, n_max_trait, species_traits_categories, species_traits,
species_traits_long)
# Plot Species x Trait completeness
ggplot2::ggplot(
species_traits_long_categories,
ggplot2::aes(
factor(
.data$trait_label, levels = unique(number_species_per_trait$trait_label)
),
factor(
.data$species, levels = unique(number_trait_per_species$species)
)
)
) +
ggplot2::geom_tile(ggplot2::aes(fill = .data$has_trait)) +
category_facet +
ggplot2::scale_x_discrete(
"Trait", guide = ggplot2::guide_axis(n.dodge = 2)
) +
ggplot2::scale_y_discrete("Species", labels = NULL) +
ggplot2::scale_fill_manual(
"Known Trait?",
values = c(`FALSE` = "#E41A1C", `TRUE` = "#377EB8"),
labels = c(`FALSE` = "No", `TRUE` = "Yes")
) +
ggplot2::coord_cartesian(expand = FALSE) +
ggplot2::theme_bw() +
ggplot2::theme(
axis.ticks.y = ggplot2::element_blank(),
legend.position = "top"
)
}