diff --git a/NAMESPACE b/NAMESPACE index 8f373128..7cd2f204 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -62,9 +62,7 @@ importFrom(dplyr,tibble) importFrom(dplyr,ungroup) importFrom(dplyr,vars) importFrom(egg,ggarrange) -importFrom(forcats,fct_rev) importFrom(ggplot2,aes) -importFrom(ggplot2,coord_flip) importFrom(ggplot2,element_text) importFrom(ggplot2,facet_wrap) importFrom(ggplot2,geom_bar) diff --git a/R/visualize_pathways_level2.R b/R/visualize_pathways_level2.R index 24d4049f..f4b31327 100644 --- a/R/visualize_pathways_level2.R +++ b/R/visualize_pathways_level2.R @@ -8,59 +8,66 @@ #' @param chosen_pathway_level1 character. A pathway level 1. If CBD standard is #' followed (see argument `cbd_standard`), one of the level 1 pathways from #' `pathways_cbd()`. -#' @param category NULL or character. One of the kingdoms as given in GBIF and -#' `Chordata` (the phylum), `Not Chordata` (all other phyla of `Animalia`): 1. -#' `Plantae` 2. `Animalia` 3. `Fungi` 4. `Chromista` 5. `Archaea` 6. -#' `Bacteria` 7. `Protozoa` 8. `Viruses` 9. `incertae sedis` 10. `Chordata` -#' 11. `Not Chordata` Default: `NULL`. -#' @param from NULL or numeric. Year trade-off: if not \code{NULL} select only +#' @param category `NULL` (default) or character. One of the kingdoms as given +#' in GBIF or `Chordata` (the phylum) or `Not Chordata` (all other phyla of +#' `Animalia`): +#' 1. `Plantae` +#' 2. `Animalia` +#' 3. `Fungi` +#' 4. `Chromista` +#' 5. `Archaea` +#' 6. `Bacteria` +#' 7. `Protozoa` +#' 8. `Viruses` +#' 9. `incertae sedis` +#' 10. `Chordata` +#' 11. `Not Chordata` +#' @param from `NULL` or numeric. Year trade-off: if not `NULL` select only #' pathways related to taxa introduced during or after this year. Default: #' `NULL`. -#' @param facet_column NULL or character. The column to use to create additional -#' facet wrap bar graphs underneath the main graph. When NULL, no facet graph -#' are created. One of `family`, `order`, `class`, `phylum`, `locality`, -#' `native_range`, `habitat`. If column has another name, rename it before -#' calling this function. Default: `NULL`. -#' @param pathway_level1_names character. Name of the column of \code{df} +#' @param facet_column `NULL` or character. The column to use to create +#' additional facet wrap bar graphs underneath the main graph. When `NULL`, no +#' facet graph are created. One of `family`, `order`, `class`, `phylum`, +#' `locality`, `native_range`, `habitat`. If column has another name, rename +#' it before calling this function. Default: `NULL`. +#' @param pathway_level1_names character. Name of the column of `df` #' containing information about pathways at level 1. Default: #' `pathway_level1`. -#' @param pathway_level2_names character. Name of the column of \code{df} +#' @param pathway_level2_names character. Name of the column of `df` #' containing information about pathways at level 2. Default: #' `pathway_level2`. #' @param pathways character. Vector with pathways level 2 to visualize. The #' pathways are displayed following the order as in this vector. -#' @param taxon_names character. Name of the column of \code{df} containing +#' @param taxon_names character. Name of the column of `df` containing #' information about taxa. This parameter is used to uniquely identify taxa. -#' @param kingdom_names character. Name of the column of \code{df} containing -#' information about kingdom. Default: \code{"kingdom"}. -#' @param phylum_names character. Name of the column of \code{df} containing -#' information about phylum. This parameter is used only if \code{category} is -#' one of: \code{"Chordata"}, \code{"Not Chordata"}. Default: -#' \code{"phylum"}. -#' @param first_observed character. Name of the column of \code{df} containing -#' information about year of introduction. Default: \code{"first_observed"}. -#' @param cbd_standard logical. If TRUE the values of pathway level 2 are +#' @param kingdom_names character. Name of the column of `df` containing +#' information about kingdom. Default: `"kingdom"`. +#' @param phylum_names character. Name of the column of `df` containing +#' information about phylum. This parameter is used only if `category` is +#' one of: `"Chordata"`, `"Not Chordata"`. Default: +#' `"phylum"`. +#' @param first_observed character. Name of the column of `df` containing +#' information about year of introduction. Default: `"first_observed"`. +#' @param cbd_standard logical. If `TRUE` the values of pathway level 2 are #' checked based on CBD standard as returned by `pathways_cbd()`. Error is -#' returned if unmatched values are found. If FALSE, a warning is returned. -#' Default: TRUE. -#' @param title NULL or character. Title of the graph. Default: NULL. -#' @param x_lab NULL or character. x-axis label. Default: "Number of introduced -#' taxa". -#' @param y_lab NULL or character. Title of the graph. Default: "Pathways". -#' @return A ggplot2 object (or egg object if facets are used). NULL if there are -#' no data to plot. +#' returned if unmatched values are found. If `FALSE`, a warning is returned. +#' Default: `TRUE`. +#' @param title `NULL` or character. Title of the graph. Default: `NULL`. +#' @param x_lab `NULL` or character. x-axis label. Default: `"Number of +#' introduced taxa"`. +#' @param y_lab `NULL` or character. Title of the graph. Default: `"Pathways"`. +#' @return A list with three slots: +#' - `plot`: ggplot2 object (or egg object if facets are used). `NULL` if there +#' are no data to plot. +#' - `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`. `NULL` is returned if `facet_column` is `NULL`. #' #' @export -#' @importFrom assertthat assert_that -#' @importFrom assertable assert_colnames -#' @importFrom dplyr %>% anti_join distinct filter if_else mutate pull rename_at -#' sym -#' @importFrom ggplot2 ggplot geom_bar theme ggtitle xlab ylab coord_flip -#' facet_wrap -#' @importFrom tidyselect all_of -#' @importFrom forcats fct_rev +#' +#' @importFrom dplyr %>% #' @importFrom rlang !! -#' @importFrom egg ggarrange #' #' @examples #' \dontrun{ @@ -70,36 +77,35 @@ #' "interim/data_input_checklist_indicators.tsv" #' ) #' data <- read_tsv(datafile, -#' na = "NA", +#' na = "", #' col_types = cols( #' .default = col_character(), #' key = col_double(), #' nubKey = col_double(), #' speciesKey = col_double(), -#' acceptedKey = col_double(), #' first_observed = col_double(), #' last_observed = col_double() #' ) #' ) #' # All taxa -#' visualize_pathways_level2(t, chosen_pathway_level1 = "escape") +#' visualize_pathways_level2(data, chosen_pathway_level1 = "escape") #' #' # Animalia -#' visualize_pathways_level2(t, +#' visualize_pathways_level2(data, #' chosen_pathway_level1 = "escape", #' category = "Animalia" #' ) #' #' # Chordata #' visualize_pathways_level2( -#' df = t, +#' df = data, #' chosen_pathway_level1 = "escape", #' category = "Chordata" #' ) #' #' # facet phylum #' visualize_pathways_level2( -#' df = t, +#' df = data, #' chosen_pathway_level1 = "escape", #' category = "Animalia", #' facet_column = "phylum" @@ -107,21 +113,21 @@ #' #' # facet habitat #' visualize_pathways_level2( -#' df = t, +#' df = data, #' chosen_pathway_level1 = "escape", #' facet_column = "habitat" #' ) #' #' # Only taxa introduced from 1950 #' visualize_pathways_level2( -#' df = t, +#' df = data, #' chosen_pathway_level1 = "escape", #' from = 1950 #' ) #' #' # Add a title #' visualize_pathways_level2( -#' df = t, +#' df = data, #' chosen_pathway_level1 = "escape", #' category = "Plantae", #' from = 1950, @@ -130,7 +136,7 @@ #' #' # Personalize axis labels #' visualize_pathways_level2( -#' df = t, +#' df = data, #' chosen_pathway_level1 = "escape", #' x_lab = "Aantal taxa", #' y_lab = "pathways" @@ -155,59 +161,65 @@ visualize_pathways_level2 <- function(df, # initial input checks # Check df - assert_that(is.data.frame(df), msg = "`df` must be a data frame.") + assertthat::assert_that(is.data.frame(df), msg = "`df` must be a data frame.") # Check pathway_level1_names - assert_that(is.character(pathway_level1_names), + assertthat::assert_that(is.character(pathway_level1_names), msg = "`pathway_level1_names` must be a character." ) - assert_colnames(df, pathway_level1_names, only_colnames = FALSE) + assertthat::assert_that(length(pathway_level1_names) == 1) + assertable::assert_colnames(df, pathway_level1_names, only_colnames = FALSE) # Check pathway_level2_names - assert_that(is.character(pathway_level2_names), + assertthat::assert_that(is.character(pathway_level2_names), msg = "`pathway_level2_names` must be a character." ) - assert_colnames(df, pathway_level2_names, only_colnames = FALSE) + assertthat::assert_that(length(pathway_level2_names) == 1) + assertable::assert_colnames(df, pathway_level2_names, only_colnames = FALSE) # Check category if (!is.null(category)) { - assert_that(is.character(category), + valid_categories <- c("Chordata", "Not Chordata", "Plantae", "Animalia", + "Fungi", "Chromista", "Archaea", "Bacteria", + "Protozoa", "Viruses", "incertae sedis") + assertthat::assert_that(is.character(category), msg = paste0( - "`category` must be a character. One of: ", - paste(categories, collapse = ", "), + "`category` must be NULL or a character. One of: ", + paste(valid_categories, collapse = ", "), "." ) ) - assert_that(category %in% categories, + assertthat::assert_that(length(category) == 1) + assertthat::assert_that(category %in% valid_categories, msg = paste0( "`category` is not correct. Choose one of: ", - paste0(categories, collapse = ", "), + paste0(valid_categories, collapse = ", "), "." ) ) } # Check facet_column - 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) - } - # check for valid facet options - valid_facet_options <- c( - "family", "order", "class", "phylum", - "locality", "native_range", "habitat" - ) - if (is.character(facet_column)) { - facet_column <- match.arg(facet_column, valid_facet_options) - assert_that(is.null(category) || !(category == "Chordata" & + assertthat::assert_that(length(facet_column) == 1) + assertable::assert_colnames(df, facet_column, only_colnames = FALSE) + # check facet column + valid_facets <- c( + "family", "order", "class", "phylum", + "locality", "native_range", "habitat" + ) + facet_column <- match.arg(facet_column, valid_facets) + assertthat::assert_that(is.null(category) || !(category == "Chordata" & facet_column == "phylum"), msg = "You cannot use phylum as facet with category Chordata." ) } # Check chosen_pathway_level1 - assert_that(is.character(chosen_pathway_level1), + assertthat::assert_that(is.character(chosen_pathway_level1), msg = "Argument `chosen_pathway_level1` must be a character." ) + assertthat::assert_that(length(chosen_pathway_level1) == 1) pathways_level1 <- unique(df[[pathway_level1_names]]) - assert_that(chosen_pathway_level1 %in% pathways_level1, + assertthat::assert_that(chosen_pathway_level1 %in% pathways_level1, msg = paste0( "chosen_pathway_level1 ", chosen_pathway_level1, @@ -218,12 +230,13 @@ visualize_pathways_level2 <- function(df, ) # Check pathways if (!is.null(pathways)) { - assert_that(is.character(pathways), + assertthat::assert_that(is.character(pathways), msg = "`pathways` must be a vector of characters." ) + assertthat::assert_that(length(pathways) == 1) invalid_pathways <- pathways[!pathways %in% df[[pathway_level2_names]]] - assert_that(length(invalid_pathways) == 0, + assertthat::assert_that(length(invalid_pathways) == 0, msg = paste0( "Pathways in `pathways` not present in ", "data.frame: ", @@ -233,32 +246,37 @@ visualize_pathways_level2 <- function(df, ) } # Check taxon_names - assert_that(is.character(taxon_names), + assertthat::assert_that(is.character(taxon_names), msg = "`taxon_names` must be a character." ) - assert_colnames(df, taxon_names, only_colnames = FALSE) + assertthat::assert_that(length(taxon_names) == 1) + assertable::assert_colnames(df, taxon_names, only_colnames = FALSE) # Check kingdom_names - assert_that(is.character(kingdom_names), + assertthat::assert_that(is.character(kingdom_names), msg = "`kingdom_names` must be a character." ) - assert_colnames(df, kingdom_names, only_colnames = FALSE) + assertthat::assert_that(length(kingdom_names) == 1) + assertable::assert_colnames(df, kingdom_names, only_colnames = FALSE) # check parameter phylum - assert_that(is.character(phylum_names), + assertthat::assert_that(is.character(phylum_names), msg = "`phylum_names` must be a character." ) - assert_colnames(df, phylum_names, only_colnames = FALSE) + assertthat::assert_that(length(phylum_names) == 1) + assertable::assert_colnames(df, phylum_names, only_colnames = FALSE) # Check from if (!is.null(from)) { - assert_that(is.numeric(from), + assertthat::assert_that(is.numeric(from), msg = "`from` must be a number (year)." ) - assert_that(from > 0, + assertthat::assert_that(length(from) == 1) + assertthat::assert_that(from > 0, msg = "`from` must be a positive number." ) - assert_that(from == as.integer(from), + assertthat::assert_that(from == as.integer(from), msg = "`from` must be an integer." ) - assert_that(from <= as.numeric(substr(Sys.Date(), start = 1, stop = 4)), + assertthat::assert_that( + from <= as.numeric(substr(Sys.Date(), start = 1, stop = 4)), msg = paste0( "`from` must be less than ", format(Sys.Date(), "%Y"), @@ -267,53 +285,56 @@ visualize_pathways_level2 <- function(df, ) } # Check first_observed - assert_that(is.character(first_observed), + assertthat::assert_that(is.character(first_observed), msg = "`first_observed` must be a character." ) - assert_colnames(df, first_observed, only_colnames = FALSE) + assertthat::assert_that(length(first_observed) == 1) + assertable::assert_colnames(df, first_observed, only_colnames = FALSE) # Check title and labels if (!is.null(title)) { - assert_that(is.character(title), + assertthat::assert_that(is.character(title), msg = "`title` must be a character or NULL." ) + assertthat::assert_that(length(title) == 1) } if (!is.null(x_lab)) { - assert_that(is.character(x_lab), + assertthat::assert_that(is.character(x_lab), msg = "`x_lab` must be a character or NULL." ) } if (!is.null(y_lab)) { - assert_that(is.character(y_lab), + assertthat::assert_that(is.character(y_lab), msg = "`y_lab` must be a character or NULL." ) + assertthat::assert_that(length(y_lab) == 1) } # rename to default column name df <- df %>% - rename_at(vars(all_of(kingdom_names)), ~"group") %>% - rename_at(vars(all_of(taxon_names)), ~"taxonKey") %>% - rename_at(vars(all_of(first_observed)), ~"first_observed") %>% - rename_at(vars(all_of(pathway_level1_names)), ~"pathway_level1") %>% - rename_at(vars(all_of(pathway_level2_names)), ~"pathway_level2") + dplyr::rename_at(vars(tidyselect::all_of(kingdom_names)), ~"group") %>% + dplyr::rename_at(vars(tidyselect::all_of(taxon_names)), ~"taxonKey") %>% + dplyr::rename_at(vars(tidyselect::all_of(first_observed)), ~"first_observed") %>% + dplyr::rename_at(vars(tidyselect::all_of(pathway_level1_names)), ~"pathway_level1") %>% + dplyr::rename_at(vars(tidyselect::all_of(pathway_level2_names)), ~"pathway_level2") # Select data with the chosen pathway level 1 df <- df %>% - filter(.data$pathway_level1 == chosen_pathway_level1) + dplyr::filter(.data$pathway_level1 == chosen_pathway_level1) # handle asymmetric category system (Chordata, Not Chordta are not kingdoms) if (!is.null(category)) { if (!category %in% c("Chordata", "Not Chordata")) { - df <- df %>% filter(.data$group == category) + df <- df %>% dplyr::filter(.data$group == category) } else { df <- df %>% - rename_at(vars(phylum_names), ~"phylum_group") + dplyr::rename_at(vars(phylum_names), ~"phylum_group") if (category == "Chordata") { - df <- df %>% filter(.data$phylum_group == category) + df <- df %>% dplyr::filter(.data$phylum_group == category) } else { df <- df %>% - filter(.data$group == "Animalia") %>% - filter(.data$phylum_group != "Chordata") + dplyr::filter(.data$group == "Animalia") %>% + dplyr::filter(.data$phylum_group != "Chordata") } } } @@ -321,14 +342,14 @@ visualize_pathways_level2 <- function(df, if (!is.null(from)) { df <- df %>% - filter(.data$first_observed >= from) + dplyr::filter(.data$first_observed >= from) } # Handle NAs and "" nas_or_empty_pathway_level2 <- df %>% - filter(is.na(.data$pathway_level2) | + dplyr::filter(is.na(.data$pathway_level2) | .data$pathway_level2 == "") %>% - distinct(.data$taxonKey) + dplyr::distinct(.data$taxonKey) if (nrow(nas_or_empty_pathway_level2) > 0) { message_warning <- paste( nrow(nas_or_empty_pathway_level2), @@ -340,7 +361,7 @@ visualize_pathways_level2 <- function(df, df <- df %>% # Handle NAs and "unknown" - mutate(pathway_level2 = if_else(is.na(.data$pathway_level2) | + dplyr::mutate(pathway_level2 = dplyr::if_else(is.na(.data$pathway_level2) | .data$pathway_level2 == "", "unknown", .data$pathway_level2 @@ -348,18 +369,18 @@ visualize_pathways_level2 <- function(df, # Import all CBD pathways level 2 within chosen pathway level 1 pathways_level2_all <- pathways_cbd() %>% - filter(.data$pathway_level1 == chosen_pathway_level1) %>% - distinct(.data$pathway_level2) + dplyr::filter(.data$pathway_level1 == chosen_pathway_level1) %>% + dplyr::distinct(.data$pathway_level2) # Select pathways if (!is.null(pathways)) { pathways <- replace(pathways, is.na(pathways) | pathways == "", "unknown") pathways <- unique(pathways) df <- df %>% - filter(.data$pathway_level2 %in% pathways) + dplyr::filter(.data$pathway_level2 %in% pathways) } else { if (cbd_standard == TRUE) { - pathways <- pathways_level2_all %>% pull() + pathways <- pathways_level2_all %>% dplyr::pull() } else { pathways <- unique(df$pathway_level2) } @@ -367,11 +388,11 @@ visualize_pathways_level2 <- function(df, # Check values in column with pathways level 2 invalid_pathways <- df %>% - anti_join(pathways_level2_all, + dplyr::anti_join(pathways_level2_all, by = "pathway_level2" ) %>% - distinct(.data$pathway_level2) %>% - pull() + dplyr::distinct(.data$pathway_level2) %>% + dplyr::pull() message_invalid_pathways <- paste0( "No CBD standard pathways level 2 value(s) in column `", @@ -383,65 +404,69 @@ visualize_pathways_level2 <- function(df, "." ) if (cbd_standard == TRUE) { - assert_that(length(invalid_pathways) == 0, + assertthat::assert_that(length(invalid_pathways) == 0, msg = message_invalid_pathways ) } else { warning(message_invalid_pathways) } - # Distinct taxa + # dplyr::distinct taxa if (!is.null(facet_column)) { df <- df %>% - distinct(.data$taxonKey, .data$pathway_level2, !!sym(facet_column)) + dplyr::distinct(.data$taxonKey, .data$pathway_level2, !!dplyr::sym(facet_column)) } # Transform pathway level 2 column to factor to make ordering in graph easily df <- df %>% - mutate(pathway_level2 = factor(.data$pathway_level2, levels = pathways)) - # Distinct taxa without facet - df_top_graph <- + dplyr::mutate(pathway_level2 = factor(.data$pathway_level2, levels = pathways)) + # dplyr::distinct taxa without facet + data_top_graph <- df %>% - distinct( + dplyr::distinct( .data$taxonKey, .data$pathway_level2 ) # Plot number of taxa per pathway_level1 top_graph <- NULL - if (nrow(df_top_graph) > 0) { + if (nrow(data_top_graph) > 0) { top_graph <- - ggplot( - df_top_graph, - aes(x = fct_rev(.data$pathway_level2)) + ggplot2::ggplot( + data_top_graph, + aes(x = forcats::fct_rev(.data$pathway_level2)) ) + - geom_bar() + - xlab(y_lab) + - ylab(x_lab) + - coord_flip() + - ggtitle(title) + ggplot2::geom_bar() + + ggplot2::xlab(y_lab) + + ggplot2::ylab(x_lab) + + ggplot2::coord_flip() + + ggplot2::ggtitle(title) } if (is.null(facet_column)) { - return(top_graph) + return(list(plot = top_graph, + data_top_graph = data_top_graph, + data_facet_graph = NULL)) } else { facet_graph <- NULL if (nrow(df) > 0) { facet_graph <- - ggplot( + ggplot2::ggplot( df, - aes(x = fct_rev(.data$pathway_level2)) + aes(x = forcats::fct_rev(.data$pathway_level2)) ) + - geom_bar() + - xlab(y_lab) + # invert labels to get them right after flipping - ylab(x_lab) + - coord_flip() + - ggtitle(title) + - facet_wrap(facet_column) + ggplot2::geom_bar() + + ggplot2::xlab(y_lab) + # invert labels to get them right after flipping + ggplot2::ylab(x_lab) + + ggplot2::coord_flip() + + ggplot2::ggtitle(title) + + ggplot2::facet_wrap(facet_column) } if (all(!is.null(top_graph), !is.null(facet_graph))) { - ggarrange(top_graph, facet_graph) + return(list(plot = egg::ggarrange(top_graph, facet_graph), + data_top_graph = data_top_graph, + data_facet_graph = df)) } else { - NULL + return(list(plot = NULL, data_top_graph = NULL, data_facet_graph = NULL)) } } } diff --git a/man/visualize_pathways_level2.Rd b/man/visualize_pathways_level2.Rd index e0715cfe..58740fc7 100644 --- a/man/visualize_pathways_level2.Rd +++ b/man/visualize_pathways_level2.Rd @@ -30,21 +30,32 @@ visualize_pathways_level2( followed (see argument \code{cbd_standard}), one of the level 1 pathways from \code{pathways_cbd()}.} -\item{category}{NULL or character. One of the kingdoms as given in GBIF and -\code{Chordata} (the phylum), \verb{Not Chordata} (all other phyla of \code{Animalia}): 1. -\code{Plantae} 2. \code{Animalia} 3. \code{Fungi} 4. \code{Chromista} 5. \code{Archaea} 6. -\code{Bacteria} 7. \code{Protozoa} 8. \code{Viruses} 9. \verb{incertae sedis} 10. \code{Chordata} -11. \verb{Not Chordata} Default: \code{NULL}.} - -\item{from}{NULL or numeric. Year trade-off: if not \code{NULL} select only +\item{category}{\code{NULL} (default) or character. One of the kingdoms as given +in GBIF or \code{Chordata} (the phylum) or \verb{Not Chordata} (all other phyla of +\code{Animalia}): +\enumerate{ +\item \code{Plantae} +\item \code{Animalia} +\item \code{Fungi} +\item \code{Chromista} +\item \code{Archaea} +\item \code{Bacteria} +\item \code{Protozoa} +\item \code{Viruses} +\item \verb{incertae sedis} +\item \code{Chordata} +\item \verb{Not Chordata} +}} + +\item{from}{\code{NULL} or numeric. Year trade-off: if not \code{NULL} select only pathways related to taxa introduced during or after this year. Default: \code{NULL}.} -\item{facet_column}{NULL or character. The column to use to create additional -facet wrap bar graphs underneath the main graph. When NULL, no facet graph -are created. One of \code{family}, \code{order}, \code{class}, \code{phylum}, \code{locality}, -\code{native_range}, \code{habitat}. If column has another name, rename it before -calling this function. Default: \code{NULL}.} +\item{facet_column}{\code{NULL} or character. The column to use to create +additional facet wrap bar graphs underneath the main graph. When \code{NULL}, no +facet graph are created. One of \code{family}, \code{order}, \code{class}, \code{phylum}, +\code{locality}, \code{native_range}, \code{habitat}. If column has another name, rename +it before calling this function. Default: \code{NULL}.} \item{pathways}{character. Vector with pathways level 2 to visualize. The pathways are displayed following the order as in this vector.} @@ -65,27 +76,33 @@ information about kingdom. Default: \code{"kingdom"}.} \item{phylum_names}{character. Name of the column of \code{df} containing information about phylum. This parameter is used only if \code{category} is -one of: \code{"Chordata"}, \code{"Not Chordata"}. Default: +one of: \code{"Chordata"}, \code{"Not Chordata"}. Default: \code{"phylum"}.} \item{first_observed}{character. Name of the column of \code{df} containing information about year of introduction. Default: \code{"first_observed"}.} -\item{cbd_standard}{logical. If TRUE the values of pathway level 2 are +\item{cbd_standard}{logical. If \code{TRUE} the values of pathway level 2 are checked based on CBD standard as returned by \code{pathways_cbd()}. Error is -returned if unmatched values are found. If FALSE, a warning is returned. -Default: TRUE.} +returned if unmatched values are found. If \code{FALSE}, a warning is returned. +Default: \code{TRUE}.} -\item{title}{NULL or character. Title of the graph. Default: NULL.} +\item{title}{\code{NULL} or character. Title of the graph. Default: \code{NULL}.} -\item{x_lab}{NULL or character. x-axis label. Default: "Number of introduced -taxa".} +\item{x_lab}{\code{NULL} or character. x-axis label. Default: \code{"Number of introduced taxa"}.} -\item{y_lab}{NULL or character. Title of the graph. Default: "Pathways".} +\item{y_lab}{\code{NULL} or character. Title of the graph. Default: \code{"Pathways"}.} } \value{ -A ggplot2 object (or egg object if facets are used). NULL if there are -no data to plot. +A list with three slots: +\itemize{ +\item \code{plot}: ggplot2 object (or egg object if facets are used). \code{NULL} if there +are no data to plot. +\item \code{data_top_graph}: data.frame (tibble) with data used for the main plot (top +graph) in \code{plot}. +\item \code{data_facet_graph}: data.frame (tibble) with data used for the faceting +plot in \code{plot}. \code{NULL} is returned if \code{facet_column} is \code{NULL}. +} } \description{ Function to plot bar graph with number of taxa introduced by different