-
Notifications
You must be signed in to change notification settings - Fork 1
/
indicator_total_year.R
240 lines (227 loc) · 8.6 KB
/
indicator_total_year.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
#' Create cumulative number of alien species indicator plot.
#'
#' This function calculates the cumulative number of taxa introduced per year.
#' To do this, a column of input dataframe containing temporal information about
#' year of introduction is required.
#' @param df df. Contains the data as produced by the Trias pipeline, with
#' minimal columns.
#' @param start_year_plot numeric. Limit to use as start year of the plot. For
#' scientific usage, the entire period could be relevant, but for policy
#' purpose, focusing on a more recent period could be required. Default: 1940.
#' @param x_major_scale_stepsize integer. On which year interval labels are
#' placed on the x axis. Default: 10.
#' @param x_minor_scale_stepsize integer. On which year interval minor breaks
#' are placed on the x axis. Default: 5.
#' @param facet_column NULL or character. Name of the column to use to create
#' additional facet wrap plots underneath the main graph. When NULL, no facet
#' graph is included. It is typically one of the highest taxonomic ranks, e.g.
#' \code{"kingdom"}, \code{"phylum"}, \code{"class"}, \code{"order"},
#' \code{"family"}. Other typical breakwdowns could be geographically related,
#' e.g. \code{"country"}, \code{"locality"}, \code{"pathway"} of introduction
#' or \code{"habitat"}. Default: `NULL`.
#' @param taxon_key_col character. Name of the column of \code{df} containing
#' unique taxon IDs. Default: \code{key}.
#' @param first_observed character. Name of the column of \code{df} containing
#' information about year of introduction. Default: \code{"first_observed"}.
#' @param x_lab NULL or character. To personalize or remove the x-axis label.
#' Default: "Year.
#' @param y_lab NULL or character. To personalize or remove the y-axis label.
#' Default: "Cumulative number of alien species".
#'
#' @return A list with three slots:
#' - `plot`: ggplot2 object (or egg object if facets are used).
#' - `data_top_graph`: data.frame (tibble) with data used for the main plot (top graph) in `plot`.
#' - `data_facet_graph`: data.frame (tibble) with data used for the faceting
#' plot in `plot`. If `facet_column` is NULL, NULL is returned.
#'
#' @export
#' @importFrom dplyr %>% .data
#' @importFrom rlang !! !!!
#'
#' @examples
#' \dontrun{
#' library(readr)
#' datafile <- paste0(
#' "https://raw.githubusercontent.com/trias-project/indicators/master/data/",
#' "interim/data_input_checklist_indicators.tsv"
#' )
#' data <- read_tsv(datafile,
#' na = "",
#' col_types = cols(
#' .default = col_character(),
#' key = col_double(),
#' nubKey = col_double(),
#' speciesKey = col_double(),
#' first_observed = col_double(),
#' last_observed = col_double()
#' )
#' )
#' start_year_plot <- 1900
#' x_major_scale_stepsize <- 25
#' x_minor_scale_stepsize <- 5
#' # without facets
#' indicator_total_year(data, start_year_plot, x_major_scale_stepsize)
#' # with facets
#' indicator_total_year(data, start_year_plot, facet_column = "kingdom")
#' # specify name of column containing year of introduction (first_observed)
#' indicator_total_year(data, first_observed = "first_observed")
#' # specify axis labels
#' indicator_total_year(data, x_lab = "YEAR", y_lab = NULL)
#' }
indicator_total_year <- function(df, start_year_plot = 1940,
x_major_scale_stepsize = 10,
x_minor_scale_stepsize = 5,
facet_column = NULL,
taxon_key_col = "key",
first_observed = "first_observed",
x_lab = "Year",
y_lab = "Cumulative number of alien species") {
# initial input checks
assertthat::assert_that(is.data.frame(df))
assertthat::assert_that(is.numeric(start_year_plot),
msg = "Argument start_year_plot has to be a number."
)
assertthat::assert_that(start_year_plot < as.integer(format(Sys.Date(), "%Y")),
msg = paste(
"Argument start_year_plot has to be less than",
format(Sys.Date(), "%Y")
)
)
assertthat::assert_that(is.numeric(x_major_scale_stepsize),
msg = "Argument x_major_scale_stepsize has to be a number."
)
assertthat::assert_that(is.numeric(x_minor_scale_stepsize),
msg = "Argument x_minor_scale_stepsize has to be a number."
)
assertthat::assert_that(x_major_scale_stepsize >= x_minor_scale_stepsize,
msg = paste0(
"x_major_scale_stepsize should be greater ",
"than x_minor_scale_stepsize."
)
)
assertthat::assert_that(is.null(facet_column) | is.character(facet_column),
msg = "Argument facet_column has to be NULL or a character."
)
if (is.character(facet_column)) {
assertable::assert_colnames(df, facet_column, only_colnames = FALSE)
}
# check for valid facet options
valid_facet_options <- c(
"family", "order", "class", "phylum",
"kingdom", "pathway_level1", "locality",
"native_range", "habitat"
)
if (!is.null(facet_column)) {
facet_column <- match.arg(facet_column, valid_facet_options)
}
assertthat::assert_that(is.character(taxon_key_col),
msg = "Argument taxon_key_col has to be a character."
)
assertable::assert_colnames(df, taxon_key_col, only_colnames = FALSE)
assertthat::assert_that(is.character(first_observed),
msg = "Argument first_observed has to be a character."
)
assertable::assert_colnames(df, first_observed, only_colnames = FALSE)
assertthat::assert_that(is.null(x_lab) | is.character(x_lab),
msg = "Argument x_lab has to be a character or NULL."
)
assertthat::assert_that(is.null(y_lab) | is.character(y_lab),
msg = "Argument y_lab has to be a character or NULL."
)
# rename to default column name
df <-
df %>%
dplyr::rename(first_observed = !!first_observed) %>%
dplyr::rename(key = !!taxon_key_col)
# Provide warning messages for first_observed NA values
n_first_observed_not_present <-
df %>%
dplyr::filter(is.na(.data$first_observed)) %>%
nrow()
if (n_first_observed_not_present) {
warning(paste0(
n_first_observed_not_present,
" records have no information about year of introduction ",
"(empty values in column ",
first_observed,
") and are not taken into account.\n"
))
}
# filter the incoming data
df <-
df %>%
dplyr::filter(!is.na(.data$first_observed))
# Distinct values in columns of interest
if (is.null(facet_column)) {
df <-
df %>%
dplyr::distinct(.data$key, .data$first_observed)
} else {
df <-
df %>%
dplyr::distinct(.data$key, .data$first_observed, .data[[facet_column]])
}
# Make individual records for each year up to now
maxDate <- as.integer(format(Sys.Date(), "%Y"))
df_extended <- df %>%
dplyr::rowwise() %>%
dplyr::do(year = .data$first_observed:maxDate) %>%
dplyr::bind_cols(df) %>%
tidyr::unnest("year")
# calculate numbers to plot
counts_ias_grouped_by_year <-
df_extended %>%
dplyr::group_by(.data$year) %>%
dplyr::count() %>%
dplyr::ungroup()
top_graph <-
ggplot2::ggplot(counts_ias_grouped_by_year,
ggplot2::aes(x = .data$year, y = .data$n)
) +
ggplot2::geom_line(stat = "identity") +
ggplot2::xlab(x_lab) +
ggplot2::ylab(y_lab) +
ggplot2::scale_x_continuous(
breaks = seq(
start_year_plot,
maxDate,
x_major_scale_stepsize
),
minor_breaks = seq(
start_year_plot,
maxDate,
x_minor_scale_stepsize
)
) +
ggplot2::coord_cartesian(xlim = c(start_year_plot, maxDate))
if (is.null(facet_column)) {
return(list(plot = top_graph,
data_top_graph = counts_ias_grouped_by_year,
data_facet_graph = NULL))
} else {
# calculate numbers
counts_ias_grouped <-
df_extended %>%
dplyr::group_by(!!!dplyr::syms(c("year", facet_column))) %>%
dplyr::count() %>%
dplyr::ungroup()
facet_graph <-
ggplot2::ggplot(
counts_ias_grouped,
ggplot2::aes(x = .data$year, y = .data$n)
) +
ggplot2::geom_line(stat = "identity") +
ggplot2::xlab(x_lab) +
ggplot2::ylab(y_lab) +
ggplot2::facet_wrap(facet_column) +
ggplot2::scale_x_continuous(
breaks = seq(start_year_plot, maxDate, x_major_scale_stepsize),
minor_breaks = seq(start_year_plot, maxDate, x_minor_scale_stepsize)
) +
ggplot2::coord_cartesian(xlim = c(start_year_plot, maxDate))
return(list(plot = egg::ggarrange(top_graph, facet_graph, draw = FALSE),
data_top_graph = counts_ias_grouped_by_year,
data_facet_graph = counts_ias_grouped)
)
}
}