diff --git a/DESCRIPTION b/DESCRIPTION index cb9e84b..fc68d1f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: visualizeR Type: Package Title: What a color! What a viz! -Version: 0.1.0 +Version: 0.1.1.9000 Authors@R: c( person( 'Noblet', 'Guillaume', @@ -18,6 +18,6 @@ License: GPL (>= 3) Encoding: UTF-8 LazyData: true RoxygenNote: 7.1.2 -Imports: ggplot2, rlang, grDevices, simplevis -Suggests: knitr +Imports: ggplot2, rlang, grDevices, simplevis, glue, scales +Suggests: knitr, sf VignetteBuilder: knitr diff --git a/NEWS.md b/NEWS.md index c11824a..5fb51b7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,12 @@ +# visualizeR 0.1.1.9000 + +* Added two horizontal bar functions: `hbar()`, `hbar_percent()` (#3) +* Added some internals to check for missing columns and bad arguments (#3) +* Modified some `theme_reach()` documentation +* Add `bbox_buffer()` function to produce a buffered bbox, e.g. for use with `tmap` + + # visualizeR 0.1.0 -* Added a `NEWS.md` file to track changes to the package. +* Added a `NEWS.md` file to track changes to the package * Initiate repo diff --git a/R/bbox_buffer.R b/R/bbox_buffer.R new file mode 100644 index 0000000..be11e5a --- /dev/null +++ b/R/bbox_buffer.R @@ -0,0 +1,39 @@ +#' @title Bbbox buffer +#' +#' @param sf_obj A `sf` object +#' @param buffer A buffer, either one value or a vector of 4 values (left, bottom, right, top). Default to 0. +#' +#' @return A bbox with a buffer +#' +#' @export +buffer_bbox <- function(sf_obj, buffer = 0){ + + rlang::check_installed("sf", reason = "Package \"sf\" needed for `buffer_bbox()` to work. Please install it.") + + + if (!(length(buffer) %in% c(1,4)) | !is.numeric(buffer)) stop("Please provide a numeric buffer of length 1 or 4.") + + bbox <- sf::st_bbox(sf_obj) + xrange <- bbox$xmax - bbox$xmin # range of x values + yrange <- bbox$ymax - bbox$ymin # range of y values + + + bbox_with_buffer <- if (length(buffer) == 1) { + c( + bbox[1] - (buffer * xrange), # xmin - left + bbox[2] - (buffer * yrange), # ymin - bottom + bbox[3] + (buffer * xrange), # xmax - right + bbox[4] + (buffer * yrange) # ymax - top + ) + } else if (length(buffer) == 4) { + c( + bbox[1] - (buffer[1] * xrange), # xmin - left + bbox[2] - (buffer[2] * yrange), # ymin - bottom + bbox[3] + (buffer[3] * xrange), # xmax - right + bbox[4] + (buffer[4] * yrange) # ymax - top + ) + } else { + print("Missed something while writing the funtion.") + } + +} diff --git a/R/hbar.R b/R/hbar.R index ef2e453..aa992ab 100644 --- a/R/hbar.R +++ b/R/hbar.R @@ -12,7 +12,6 @@ #' @param group_title The group legend title. Defaut to NULL #' @param font_family The font family. Default to "Leelawadee" #' @param stack Should the chart be stacked? Default to "FALSE" (dodge) -#' @param reverse Boolean indicating whether the palette should be reversed #' @param ... Other arguments to be passed to "simplevis::gg_hbar" or "simplevis:gg_hbar_col" #' #' @return A horizontal bar chart @@ -21,7 +20,7 @@ hbar_percent <- function(.tbl, x, y, group = NULL, initiative = "reach", x_title = "", y_title = "", group_title = NULL, font_family = "Leelawadee", stack = FALSE, ...){ - if_vec_not_in_stop(initiative, c("reach", "agora", "impact"), "initiative") + if (!(initiative %in% c("reach", "agora", "impact"))) rlang::abort(c("Wrong `initiative` arg", "*" = paste0("Arg `initiative` cannot be: ", initiative), "i" = "It must be one of 'reach' or 'agora' or 'impact'")) if (initiative == "reach") main_col <- cols_reach("main_grey") @@ -30,12 +29,11 @@ hbar_percent <- function(.tbl, x, y, group = NULL, initiative = "reach", x_title if (initiative == "impact") rlang::abort("IMPACT colors are under development") if (is.null(group)) { - hbar <- data |> + hbar <- .tbl |> simplevis::gg_hbar( x_var = {{ x }}, y_var = {{ y }}, - title = title, - theme = gg_theme(font = font, pal_title = main_col), + theme = simplevis::gg_theme(font = font_family, pal_title = main_col), x_title = x_title, y_title = y_title, alpha_fill = 1, @@ -47,16 +45,15 @@ hbar_percent <- function(.tbl, x, y, group = NULL, initiative = "reach", x_title group_name <- rlang::as_name(rlang::enquo(group)) if_not_in_stop(.tbl, group_name) - hbar <- data |> + hbar <- .tbl |> simplevis::gg_hbar_col( x_var = {{ x }}, y_var = {{ y }}, col_var = {{ group }}, - title = title, - theme = gg_theme(font = font, pal_title = main_col), + theme = simplevis::gg_theme(font = font_family, pal_title = main_col), x_title = x_title, y_title = y_title, - col_title = col_title, + col_title = group_title, alpha_fill = 1, pal = main_col, x_labels = scales::percent, @@ -81,7 +78,6 @@ hbar_percent <- function(.tbl, x, y, group = NULL, initiative = "reach", x_title #' @param group_title The group legend title. Defaut to NULL #' @param font_family The font family. Default to "Leelawadee" #' @param stack Should the chart be stacked? Default to "FALSE" (dodge) -#' @param reverse Boolean indicating whether the palette should be reversed #' @param ... Other arguments to be passed to "simplevis::gg_hbar" or "simplevis:gg_hbar_col" #' #' @return A horizontal bar chart @@ -90,7 +86,7 @@ hbar_percent <- function(.tbl, x, y, group = NULL, initiative = "reach", x_title hbar <- function(.tbl, x, y, group = NULL, initiative = "reach", x_title = "", y_title = "", group_title = NULL, font_family = "Leelawadee", stack = FALSE, ...){ - if_vec_not_in_stop(initiative, c("reach", "agora", "impact"), "initiative") + if (!(initiative %in% c("reach", "agora", "impact"))) rlang::abort(c("Wrong `initiative` arg", "*" = paste0("Arg `initiative` cannot be: ", initiative), "i" = "It must be one of 'reach' or 'agora' or 'impact'")) if (initiative == "reach") main_col <- cols_reach("main_grey") @@ -99,12 +95,11 @@ hbar <- function(.tbl, x, y, group = NULL, initiative = "reach", x_title = "", y if (initiative == "impact") rlang::abort("IMPACT colors are under development") if (is.null(group)) { - hbar <- data |> + hbar <- .tbl |> simplevis::gg_hbar( x_var = {{ x }}, y_var = {{ y }}, - title = title, - theme = gg_theme(font = font, pal_title = main_col), + theme = simplevis::gg_theme(font = font_family, pal_title = main_col), x_title = x_title, y_title = y_title, alpha_fill = 1, @@ -115,16 +110,15 @@ hbar <- function(.tbl, x, y, group = NULL, initiative = "reach", x_title = "", y group_name <- rlang::as_name(rlang::enquo(group)) if_not_in_stop(.tbl, group_name) - hbar <- data |> + hbar <- .tbl |> simplevis::gg_hbar_col( x_var = {{ x }}, y_var = {{ y }}, col_var = {{ group }}, - title = title, - theme = gg_theme(font = font, pal_title = main_col), + theme = simplevis::gg_theme(font = font_family, pal_title = main_col), x_title = x_title, y_title = y_title, - col_title = col_title, + col_title = group_title, alpha_fill = 1, pal = main_col, stack = stack, diff --git a/R/internals.R b/R/internals.R index 3268434..12bbef3 100644 --- a/R/internals.R +++ b/R/internals.R @@ -85,3 +85,13 @@ if_vec_not_in_stop <- function(vec, cols, vec_name, arg = NULL){ ) } } + +#' @title Subvec not in +#' +#' @param vector A vector to subset +#' @param set A set-vector +#' +#' @return A subset of vector not in set +subvec_not_in <- function(vector, set){ + vector[!(vector %in% set)] +} diff --git a/R/theme_reach.R b/R/theme_reach.R index c64caf2..ee87ca4 100644 --- a/R/theme_reach.R +++ b/R/theme_reach.R @@ -6,6 +6,7 @@ #' #' @return The base REACH theme #' +#' @export theme_reach <- function(family = "Leelawadee") { rlang::check_installed("ggplot2", reason = "Package \"ggplot2\" needed for `theme_reach_*()` to work. Please install it.") @@ -47,7 +48,7 @@ theme_reach_borders <- function(family = "Leelawadee") { -#' @title Some reach more minimal theme for ggplot +#' @title Some reach more minimal theme for a ggplot histogram #' #' @param family The font family. Default to "Leelawadee" #' @@ -67,7 +68,7 @@ theme_reach_hist <- function(family = "Leelawadee") { } -#' @title Some reach more minimal theme for ggplot +#' @title Some reach more minimal theme for a ggplot flipped histogram #' #' @param family The font family. Default to "Leelawadee" #' diff --git a/docs/404.html b/docs/404.html index bee377d..97a5642 100644 --- a/docs/404.html +++ b/docs/404.html @@ -31,7 +31,7 @@ visualizeR - 0.1.0 + 0.1.1.9000 diff --git a/docs/LICENSE.html b/docs/LICENSE.html index ce86074..929cfb8 100644 --- a/docs/LICENSE.html +++ b/docs/LICENSE.html @@ -10,7 +10,7 @@ visualizeR - 0.1.0 + 0.1.1.9000 diff --git a/docs/authors.html b/docs/authors.html index 07a6ef4..75d6950 100644 --- a/docs/authors.html +++ b/docs/authors.html @@ -10,7 +10,7 @@ visualizeR - 0.1.0 + 0.1.1.9000 diff --git a/docs/index.html b/docs/index.html index a9a7aac..0700d06 100644 --- a/docs/index.html +++ b/docs/index.html @@ -33,7 +33,7 @@ visualizeR - 0.1.0 + 0.1.1.9000 diff --git a/docs/news/index.html b/docs/news/index.html index 737b162..6b17786 100644 --- a/docs/news/index.html +++ b/docs/news/index.html @@ -10,7 +10,7 @@ visualizeR - 0.1.0 + 0.1.1.9000 @@ -44,12 +44,21 @@ Source: NEWS.md + +visualizeR 0.1.1.9000 +Added two horizontal bar functions: hbar(), hbar_percent() (#3) +Added some internals to check for missing columns and bad arguments (#3) +Modified some theme_reach() documentation +Add bbox_buffer() function to produce a buffered bbox, e.g. for use with tmap + + visualizeR 0.1.0 -Added a NEWS.md file to track changes to the package. +Added a NEWS.md file to track changes to the package Initiate repo - +
NEWS.md
hbar()
hbar_percent()
theme_reach()
bbox_buffer()
tmap