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

Add indicator native range year #65

Merged
merged 63 commits into from
Sep 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
574f84b
Create indicator_native_range_year.R
SanderDevisscher Jun 30, 2020
d7373b9
Create test_indicator_native_range_year.R
SanderDevisscher Jul 7, 2020
c2ed621
Get the function working outside of reportinggrofwild & INBOtheme
SanderDevisscher Jul 7, 2020
2879cf5
add axis label customisation
SanderDevisscher Jul 7, 2020
2e5f002
add relative parameter
SanderDevisscher Jul 7, 2020
215f434
Update test_indicator_native_range_year.R
SanderDevisscher Jul 7, 2020
614106d
set dynamic hoverover text
SanderDevisscher Jul 7, 2020
323d1a5
Add translation from native_rang to native_continent
SanderDevisscher Aug 5, 2020
7c8ff30
rename function
SanderDevisscher Aug 13, 2020
c4bd355
translate to english
SanderDevisscher Aug 13, 2020
09958da
add data.table & plotly to description
SanderDevisscher Aug 13, 2020
b4f8fca
Add @sanderdevisscher to Authors
SanderDevisscher Aug 13, 2020
6878392
Add reshape 2 & scales to description
SanderDevisscher Aug 13, 2020
2ae86fd
cleanup roxygen part of function
SanderDevisscher Aug 13, 2020
eafc40e
stringify my email
SanderDevisscher Aug 13, 2020
9d0eacc
remove extra quote
SanderDevisscher Aug 13, 2020
b0e064b
rename function correctly
SanderDevisscher Aug 13, 2020
66a8584
add dplyr to import rules
SanderDevisscher Aug 13, 2020
2d280b5
add ggplot - plot to output & rename slots
SanderDevisscher Aug 13, 2020
55678f9
add case_when to import + remove plotly::plotly from import rules
SanderDevisscher Aug 13, 2020
655896b
Update roxygen items & text
SanderDevisscher Aug 13, 2020
84b14ad
remove required packages
SanderDevisscher Aug 13, 2020
405c0c1
Remove width & height parameters from ggplotly
SanderDevisscher Aug 13, 2020
36609c0
rename data slots
SanderDevisscher Aug 13, 2020
731271f
general layouting/indentations
SanderDevisscher Aug 13, 2020
81a4952
add pipe to import rules
SanderDevisscher Aug 13, 2020
bc4671b
Remove "," from import rules
SanderDevisscher Aug 13, 2020
946b1b2
complete export rule
SanderDevisscher Aug 13, 2020
5f247e3
rebuild NAMESPACE
SanderDevisscher Aug 13, 2020
b6305f5
Rewrite link to original code
SanderDevisscher Aug 17, 2020
1f49274
Fix title
SanderDevisscher Sep 7, 2020
42fbd0a
remove case_when for native_continent
SanderDevisscher Sep 7, 2020
29bc260
Correct typo form -> from
damianooldoni Sep 8, 2020
2b8f864
Remove NULL
damianooldoni Sep 8, 2020
824f2c8
Import %>% from dplyr instead of adding magrittr to pkgs
damianooldoni Sep 8, 2020
25fc430
Apply styling via styler pkg
damianooldoni Sep 10, 2020
947e0ea
devtools::document()
damianooldoni Sep 10, 2020
ddbe7bd
Update version number
damianooldoni Sep 10, 2020
11652f8
Remove test new function as it is not a test
damianooldoni Sep 10, 2020
06b25c0
Update fucntions using name_* functions from rgbif to be rgbif 3.x co…
damianooldoni Sep 11, 2020
3766a36
Solve new arised bug with empty df: logicals not set as chars
damianooldoni Sep 11, 2020
5727ec5
Use tibble/as_tibble as they are already exported by trias
damianooldoni Sep 11, 2020
24fc728
Use left_join instead of right_join to return same order of taxa
damianooldoni Sep 14, 2020
f7bc6d1
Solve order columns in output info new synonyms df
damianooldoni Sep 14, 2020
84c27d1
Add checks for unique taxon keys and no NAs
damianooldoni Sep 14, 2020
7f422a1
Make tubbles instead of dfs
damianooldoni Sep 14, 2020
cb57365
Add coumentation for data, years and relative
damianooldoni Sep 14, 2020
4662830
Add first_observed argument
damianooldoni Sep 14, 2020
e78d77a
Remove unused args in function
damianooldoni Sep 14, 2020
3655230
Set minimum requirement version for rgbif
damianooldoni Sep 14, 2020
0bfc5e5
Set tibble instead of data.frame
damianooldoni Sep 14, 2020
3696004
Update documentation devtools::document()
damianooldoni Sep 14, 2020
244cc1c
Remove typos in @param names and add description of first_observed
damianooldoni Sep 14, 2020
ef8266a
Remove a last return arg as it is defunct in rgbif 3
damianooldoni Sep 14, 2020
e5e361d
Set trias::tiblble to ùake tibble in test a recognizable function
damianooldoni Sep 14, 2020
bd09e4f
Put all tests for in/output together, avoid to source file with input…
damianooldoni Sep 14, 2020
47f978f
set dplyr::tibble instead of trias::tibble in test
damianooldoni Sep 14, 2020
b301f01
Solve typo in markdown syntax for link in documentation
damianooldoni Sep 14, 2020
4ce92ef
Apply styler::style_pg()
damianooldoni Sep 14, 2020
e500397
add xlab, ylab and title to static plot
SanderDevisscher Sep 15, 2020
f68ac0a
Remove title
SanderDevisscher Sep 15, 2020
2610334
add 90° angle axis text for static plot
SanderDevisscher Sep 15, 2020
43a5c84
Remove remote installation of rgbif
damianooldoni Sep 16, 2020
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
15 changes: 9 additions & 6 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
Package: trias
Title: Process Data for the Project Tracking Invasive Alien Species (TrIAS)
Version: 1.3.0.9000
Version: 1.4.0.9000
Description: TrIAS provides functionality to facilitate the data processing
for the project Tracking Invasive Alien Species (TrIAS
<http://trias-project.be>).
Authors@R: c(
person("Damiano", "Oldoni", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0003-3445-7562")),
person("Peter", "Desmet", role = "aut", email = "[email protected]", comment = c(ORCID = "0000-0002-8442-8025")),
person("Stijn", "Van Hoey", role = "ctb", email = "[email protected]", comment = c(ORCID = "0000-0001-6413-3185"))
person("Stijn", "Van Hoey", role = "ctb", email = "[email protected]", comment = c(ORCID = "0000-0001-6413-3185")),
person("Sander", "Devisscher", role = "ctb", email = "[email protected]", comment = c(ORCID = "0000-0003-2015-5731"))
)
License: MIT + file LICENSE
URL: https://github.com/trias-project/trias, https://trias-project.github.io/trias
Expand All @@ -17,6 +18,7 @@ Depends:
Imports:
assertthat,
assertable,
data.table,
dplyr,
egg,
forcats,
Expand All @@ -25,21 +27,22 @@ Imports:
lazyeval,
magrittr,
mgcv,
plotly,
purrr,
readr,
rgbif,
rgbif (>= 3.0),
rlang,
reshape2,
scales,
stringr,
tibble,
tidyr,
tidyselect
Suggests:
knitr,
testthat
Remotes:
ropensci/rgbif
LazyData: true
Encoding: UTF-8
VignetteBuilder: knitr
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.0
RoxygenNote: 7.1.1
7 changes: 6 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export(gbif_has_distribution)
export(gbif_verify_keys)
export(get_table_pathways)
export(indicator_introduction_year)
export(indicator_native_range_year)
export(indicator_total_year)
export(pathways_cbd)
export(update_download_list)
Expand Down Expand Up @@ -51,7 +52,6 @@ importFrom(dplyr,n_distinct)
importFrom(dplyr,pull)
importFrom(dplyr,rename)
importFrom(dplyr,rename_at)
importFrom(dplyr,right_join)
importFrom(dplyr,rowwise)
importFrom(dplyr,sample_n)
importFrom(dplyr,select)
Expand Down Expand Up @@ -80,6 +80,7 @@ importFrom(ggplot2,ggsave)
importFrom(ggplot2,ggtitle)
importFrom(ggplot2,scale_colour_manual)
importFrom(ggplot2,scale_x_continuous)
importFrom(ggplot2,scale_y_continuous)
importFrom(ggplot2,theme)
importFrom(ggplot2,xlab)
importFrom(ggplot2,ylab)
Expand All @@ -90,6 +91,8 @@ importFrom(lazyeval,interp)
importFrom(mgcv,gam)
importFrom(mgcv,nb)
importFrom(mgcv,summary.gam)
importFrom(plotly,ggplotly)
importFrom(plotly,layout)
importFrom(purrr,cross_df)
importFrom(purrr,map)
importFrom(purrr,map2)
Expand All @@ -100,6 +103,7 @@ importFrom(purrr,map_dfr)
importFrom(purrr,pmap_dfr)
importFrom(purrr,reduce)
importFrom(readr,read_delim)
importFrom(reshape2,melt)
importFrom(rgbif,name_lookup)
importFrom(rgbif,name_usage)
importFrom(rgbif,occ_download_meta)
Expand All @@ -108,6 +112,7 @@ importFrom(rlang,"!!")
importFrom(rlang,":=")
importFrom(rlang,.data)
importFrom(rlang,sym)
importFrom(scales,percent_format)
importFrom(stats,formula)
importFrom(stats,median)
importFrom(stats,predict)
Expand Down
12 changes: 5 additions & 7 deletions R/gbif_get_taxa.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ gbif_get_taxa <- function(
taxon_keys <- as.integer(taxon_keys[1:maxlimit])
taxon_keys_df <- as.data.frame(taxon_keys)
taxon_taxa <- map_dfr(
taxon_keys_df$taxon_keys, ~ name_usage(key = ., return = "data")
taxon_keys_df$taxon_keys, ~ name_usage(key = .)$data
)
taxon_taxa <- taxon_taxa %>%
ungroup() %>%
Expand Down Expand Up @@ -167,17 +167,15 @@ gbif_get_taxa <- function(
checklist_keys, ~ name_lookup(
datasetKey = .,
origin = origins,
limit = maxlimit,
return = "data"
)
limit = maxlimit
)$data
)
} else {
checklist_taxa <-
map_dfr(checklist_keys, ~ name_lookup(
datasetKey = .,
limit = maxlimit,
return = "data"
))
limit = maxlimit
)$data)
}

checklist_taxa <-
Expand Down
3 changes: 1 addition & 2 deletions R/gbif_has_distribution.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ gbif_has_distribution <- function(taxon_key, ...) {
# retrieve distribution properties from GBIF
distr_properties <- rgbif::name_usage(
key = as.integer(taxon_key),
return = "data",
data = "distribution"
)
)$data

# no ditribution properties values specified by user
if (is.null(names(user_properties))) {
Expand Down
2 changes: 1 addition & 1 deletion R/gbif_verify_keys.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ gbif_verify_keys <- function(keys, col_keys = "key") {
names(keys) <- as.character(keys)
gbif_info <-
keys %>%
map(~ tryCatch(name_usage(., return = "data")[1, ],
map(~ tryCatch(name_usage(.)$data[1, ],
error = function(e) {
print(paste("Key", ., "is an invalid GBIF taxon key."))
}
Expand Down
24 changes: 14 additions & 10 deletions R/get_table_pathways.R
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,19 @@ get_table_pathways <- function(df,
)
}
# Join pathways and samples together
pathway_data <-
if (nrow(pathway_data) == 0) {
tibble(
pathway_level1 = character(0),
pathway_level2 = character(0),
n = integer(0),
examples = character(0)
)
} else {
pathway_data %>%
# if pathway_data is empty
mutate_if(is.logical, as.character) %>%
left_join(samples,
by = c("pathway_level1", "pathway_level2")
) %>%
select(-.data$size_sample) %>%
ungroup()

pathway_data
left_join(samples,
by = c("pathway_level1", "pathway_level2")
) %>%
select(-.data$size_sample) %>%
ungroup()
}
}
6 changes: 4 additions & 2 deletions R/indicator_introduction_year.R
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ indicator_introduction_year <- function(df,
start_year_plot,
maxDate,
x_minor_scale_stepsize
)) +
)
) +
coord_cartesian(xlim = c(start_year_plot, maxDate))

if (is.null(facet_column)) {
Expand Down Expand Up @@ -232,7 +233,8 @@ indicator_introduction_year <- function(df,
start_year_plot,
maxDate,
x_minor_scale_stepsize
)) +
)
) +
coord_cartesian(xlim = c(start_year_plot, maxDate))

ggarrange(top_graph, facet_graph)
Expand Down
135 changes: 135 additions & 0 deletions R/indicator_native_range_year.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#' Create an interactive plot for the number of alien species per native region
#' and year of introduction
#'
#' Based on
#' [countYearProvince](https://github.com/inbo/reporting-rshiny-grofwildjacht/blob/exoten/reporting-grofwild/R/countYearProvince.R)
#' plot from reporting - rshiny - grofwildjacht
#' @param data input data.frame.
#' @param years (numeric) vector years we are interested to. If \code{NULL}
#' (default) all years from minimum and maximum of years of first observation
#' are taken into account.
#' @param type character, native_range level of interest should be one of
#' \code{c("native_continent", "native_range")}.
#' @param x_lab character string, label of the x-axis. Default: "year".
#' @param y_lab character string, label of the y-axis. Default: "number of alien
#' species".
#' @param relative (logical) if TRUE, each bar is standardised before stacking
#' @param first_observed (character) Name of the column in \code{data} containing temporal information about introduction of the alien species. Expressed as years.
#' @return list with: \itemize{ \item{'static_plot': }{ggplot object, for a
#' given species the observed number per year and per native range is plotted
#' in a stacked bar chart} \item{'interactive_plot': }{plotly object, for a
#' given species the observed number per year and per native range is plotted
#' in a stacked bar chart} \item{'data': }{data displayed in the plot, as
#' data.frame with: \itemize{ \item{'year': }{year at which the species were
#' introduced} \item{'native_range': }{native range of the introduced species}
#' \item{'n': }{number of species introduced from the native range for a given
#' year} \item{'total': }{total number of species, from all around the world,
#' introduced during a given year} \item{'perc': }{percentage of species
#' introduced from the native range for a given year. (n/total)*100} } } }
#' @export
#' @importFrom reshape2 melt
#' @importFrom ggplot2 ggplot geom_bar scale_y_continuous xlab ylab theme element_text
#' @importFrom plotly ggplotly layout
#' @importFrom scales percent_format
#' @importFrom dplyr %>% mutate group_by case_when rename_at

indicator_native_range_year <- function(data, years = NULL,
type = c("native_continent", "native_range"),
x_lab = "year",
y_lab = "alien species",
relative = FALSE,
first_observed = "first_observed") {
type <- match.arg(type)

# Rename to default column name
data <-
data %>%
rename_at(vars(first_observed), ~"first_observed")

if (is.null(years)) {
years <- sort(unique(data$first_observed))
}

plotData <- data

plotData$location <- switch(type,
native_range = plotData$native_range,
native_continent = plotData$native_continent
)

# Select data
plotData <- plotData[plotData$first_observed %in% years, c("first_observed", "location")]
plotData <- plotData[!is.na(plotData$first_observed) & !is.na(plotData$location), ]

# Exclude unused provinces
plotData$location <- as.factor(plotData$location)
plotData$location <- droplevels(plotData$location)

# Summarize data per native_range and year
plotData$first_observed <- with(plotData, factor(first_observed,
levels =
min(years):max(years)
))

summaryData <- melt(table(plotData), id.vars = "first_observed")
summaryData <- summaryData %>%
group_by(.data$first_observed) %>%
mutate(
total = sum(.data$value),
perc = round((.data$value / .data$total) * 100, 2)
)

# Summarize data per year
totalCount <- table(plotData$first_observed)


# For optimal displaying in the plot
summaryData$location <- as.factor(summaryData$location)
summaryData$location <- factor(summaryData$location, levels = rev(levels(summaryData$location)))
summaryData$first_observed <- as.factor(summaryData$first_observed)



# Create plot

if (relative == TRUE) {
position <- "fill"
text <- paste0(summaryData$location, "<br>", summaryData$perc, "%")
} else {
position <- "stack"
text <- paste0(summaryData$location, "<br>", summaryData$value)
}

pl <- ggplot(data = summaryData, aes(
x = .data$first_observed,
y = .data$value,
fill = .data$location,
text = text
)) +
geom_bar(position = position, stat = "identity") +
xlab(x_lab) +
ylab(y_lab) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

if (relative == TRUE) {
pl <- pl + scale_y_continuous(labels = percent_format())
}

pl_2 <- ggplotly(data = summaryData, pl, tooltip = "text") %>%
layout(
xaxis = list(title = x_lab, tickangle = "auto"),
yaxis = list(title = y_lab, tickformat = ",d"),
margin = list(b = 80, t = 100),
barmode = ifelse(nlevels(summaryData$first_observed) == 1, "group", "stack")
)

# To prevent warnings in UI
pl$elementId <- NULL

# Change variable name
names(summaryData)[names(summaryData) == "value"] <- "n"
names(summaryData)[names(summaryData) == "first_observed"] <- "year"
names(summaryData)[names(summaryData) == "location"] <- "native_range"

return(list(static_plot = pl, interactive_plot = pl_2, data = summaryData))
}
6 changes: 4 additions & 2 deletions R/indicator_total_year.R
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ indicator_total_year <- function(df, start_year_plot = 1940,
start_year_plot,
maxDate,
x_minor_scale_stepsize
)) +
)
) +
coord_cartesian(xlim = c(start_year_plot, maxDate))

if (is.null(facet_column)) {
Expand All @@ -220,7 +221,8 @@ indicator_total_year <- function(df, start_year_plot = 1940,
facet_wrap(facet_column) +
scale_x_continuous(
breaks = seq(start_year_plot, maxDate, x_major_scale_stepsize),
minor_breaks = seq(start_year_plot, maxDate, x_minor_scale_stepsize)) +
minor_breaks = seq(start_year_plot, maxDate, x_minor_scale_stepsize)
) +
coord_cartesian(xlim = c(start_year_plot, maxDate))

ggarrange(top_graph, facet_graph)
Expand Down
Loading