Skip to content

Commit

Permalink
Apply #86
Browse files Browse the repository at this point in the history
  • Loading branch information
damianooldoni committed Jun 9, 2022
1 parent 1c1e2be commit 19ab153
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 55 deletions.
5 changes: 0 additions & 5 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ importFrom(dplyr,case_when)
importFrom(dplyr,count)
importFrom(dplyr,desc)
importFrom(dplyr,distinct)
importFrom(dplyr,do)
importFrom(dplyr,enquo)
importFrom(dplyr,filter)
importFrom(dplyr,filter_at)
Expand All @@ -58,15 +57,13 @@ importFrom(dplyr,select)
importFrom(dplyr,starts_with)
importFrom(dplyr,summarize)
importFrom(dplyr,sym)
importFrom(dplyr,syms)
importFrom(dplyr,tally)
importFrom(dplyr,tibble)
importFrom(dplyr,ungroup)
importFrom(dplyr,vars)
importFrom(egg,ggarrange)
importFrom(forcats,fct_rev)
importFrom(ggplot2,aes)
importFrom(ggplot2,coord_cartesian)
importFrom(ggplot2,coord_flip)
importFrom(ggplot2,element_text)
importFrom(ggplot2,facet_wrap)
Expand All @@ -78,7 +75,6 @@ importFrom(ggplot2,ggplot)
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)
Expand Down Expand Up @@ -149,7 +145,6 @@ importFrom(stringr,str_to_lower)
importFrom(svDialogs,dlgInput)
importFrom(tibble,as_tibble)
importFrom(tibble,tibble)
importFrom(tidyr,unnest)
importFrom(tidyselect,all_of)
importFrom(tidyselect,ends_with)
importFrom(tidyselect,one_of)
Expand Down
2 changes: 1 addition & 1 deletion R/indicator_introduction_year.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#' plot in `plot`. If `facet_column` is NULL, NULL is returned.
#'
#' @export
#' @importFrom dplyr %>%
#' @importFrom dplyr %>% .data
#' @importFrom rlang !!!
#'
#' @examples
Expand Down
91 changes: 42 additions & 49 deletions R/indicator_total_year.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,8 @@
#' plot in `plot`. If `facet_column` is NULL, NULL is returned.
#'
#' @export
#' @importFrom assertthat assert_that
#' @importFrom assertable assert_colnames
#' @importFrom dplyr %>% filter rowwise do bind_cols group_by count ungroup
#' rename_at distinct .data syms
#' @importFrom tidyr unnest
#' @importFrom dplyr %>% .data
#' @importFrom rlang !!!
#' @importFrom ggplot2 coord_cartesian ggplot geom_line aes xlab ylab scale_x_continuous
#' facet_wrap
#' @importFrom egg ggarrange
#'
#' @examples
#' \dontrun{
Expand Down Expand Up @@ -85,33 +78,33 @@ indicator_total_year <- function(df, start_year_plot = 1940,
y_lab = "Cumulative number of alien species") {

# initial input checks
assert_that(is.data.frame(df))
assert_that(is.numeric(start_year_plot),
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."
)
assert_that(start_year_plot < as.integer(format(Sys.Date(), "%Y")),
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")
)
)
assert_that(is.numeric(x_major_scale_stepsize),
assertthat::assert_that(is.numeric(x_major_scale_stepsize),
msg = "Argument x_major_scale_stepsize has to be a number."
)
assert_that(is.numeric(x_minor_scale_stepsize),
assertthat::assert_that(is.numeric(x_minor_scale_stepsize),
msg = "Argument x_minor_scale_stepsize has to be a number."
)
assert_that(x_major_scale_stepsize >= x_minor_scale_stepsize,
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."
)
)
assert_that(is.null(facet_column) | is.character(facet_column),
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)) {
assert_colnames(df, facet_column, only_colnames = FALSE)
assertable::assert_colnames(df, facet_column, only_colnames = FALSE)
}

# check for valid facet options
Expand All @@ -124,19 +117,19 @@ indicator_total_year <- function(df, start_year_plot = 1940,
facet_column <- match.arg(facet_column, valid_facet_options)
}

assert_that(is.character(taxon_key_col),
assertthat::assert_that(is.character(taxon_key_col),
msg = "Argument taxon_key_col has to be a character."
)
assert_colnames(df, taxon_key_col, only_colnames = FALSE)
assert_that(is.character(first_observed),
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."
)
assert_colnames(df, first_observed, only_colnames = FALSE)
assertable::assert_colnames(df, first_observed, only_colnames = FALSE)

assert_that(is.null(x_lab) | is.character(x_lab),
assertthat::assert_that(is.null(x_lab) | is.character(x_lab),
msg = "Argument x_lab has to be a character or NULL."
)
assert_that(is.null(y_lab) | is.character(y_lab),
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
Expand All @@ -148,7 +141,7 @@ indicator_total_year <- function(df, start_year_plot = 1940,
# Provide warning messages for first_observed NA values
n_first_observed_not_present <-
df %>%
filter(is.na(.data$first_observed)) %>%
dplyr::filter(is.na(.data$first_observed)) %>%
nrow()
if (n_first_observed_not_present) {
warning(paste0(
Expand All @@ -160,35 +153,35 @@ indicator_total_year <- function(df, start_year_plot = 1940,
))
}

# Filter the incoming data
# filter the incoming data
df <-
df %>%
filter(!is.na(.data$first_observed))
dplyr::filter(!is.na(.data$first_observed))

# Distinct values in columns of interest
if (is.null(facet_column)) {
df <-
df %>%
distinct(.data$key, .data$first_observed)
dplyr::distinct(.data$key, .data$first_observed)
} else {
df <-
df %>%
distinct(.data$key, .data$first_observed, .data[[facet_column]])
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 %>%
rowwise() %>%
do(year = .data$first_observed:maxDate) %>%
bind_cols(df) %>%
unnest(.data$year)
dplyr::rowwise() %>%
dplyr::do(year = .data$first_observed:maxDate) %>%
dplyr::bind_cols(df) %>%
tidyr::unnest(.data$year)

top_graph <- ggplot(df_extended, aes(x = .data$year)) +
geom_line(stat = "count") +
xlab(x_lab) +
ylab(y_lab) +
scale_x_continuous(
top_graph <- ggplot2::ggplot(df_extended, ggplot2::aes(x = .data$year)) +
ggplot2::geom_line(stat = "count") +
ggplot2::xlab(x_lab) +
ggplot2::ylab(y_lab) +
ggplot2::scale_x_continuous(
breaks = seq(
start_year_plot,
maxDate,
Expand All @@ -200,7 +193,7 @@ indicator_total_year <- function(df, start_year_plot = 1940,
x_minor_scale_stepsize
)
) +
coord_cartesian(xlim = c(start_year_plot, maxDate))
ggplot2::coord_cartesian(xlim = c(start_year_plot, maxDate))

if (is.null(facet_column)) {
return(list(plot = top_graph,
Expand All @@ -211,26 +204,26 @@ indicator_total_year <- function(df, start_year_plot = 1940,
# calculate numbers
counts_ias_grouped <-
df_extended %>%
group_by(!!!syms(c("year", facet_column))) %>%
count() %>%
ungroup()
dplyr::group_by(!!!dplyr::syms(c("year", facet_column))) %>%
dplyr::count() %>%
dplyr::ungroup()

facet_graph <-
ggplot(
ggplot2::ggplot(
counts_ias_grouped,
aes(x = .data$year, y = .data$n)
ggplot2::aes(x = .data$year, y = .data$n)
) +
geom_line(stat = "identity") +
xlab(x_lab) +
ylab(y_lab) +
facet_wrap(facet_column) +
scale_x_continuous(
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)
) +
coord_cartesian(xlim = c(start_year_plot, maxDate))
ggplot2::coord_cartesian(xlim = c(start_year_plot, maxDate))

return(list(plot = ggarrange(top_graph, facet_graph),
return(list(plot = egg::ggarrange(top_graph, facet_graph),
data_top_graph = df_extended,
data_facet_graph = counts_ias_grouped)
)
Expand Down

0 comments on commit 19ab153

Please sign in to comment.