From 4811f00b3feb4c77d193739423e31a762563819b Mon Sep 17 00:00:00 2001 From: gnoblet Date: Sat, 14 May 2022 10:20:14 -0400 Subject: [PATCH] Update documentation and check arg error in `hbar*()` functions --- DESCRIPTION | 6 +- NEWS.md | 10 +- R/bbox_buffer.R | 39 +++++++ R/hbar.R | 30 +++--- R/internals.R | 10 ++ R/theme_reach.R | 5 +- docs/404.html | 2 +- docs/LICENSE.html | 2 +- docs/authors.html | 2 +- docs/index.html | 2 +- docs/news/index.html | 15 ++- docs/pkgdown.yml | 2 +- docs/reference/abort_bad_argument.html | 90 +++++++++++++++++ docs/reference/buffer_bbox.html | 88 ++++++++++++++++ docs/reference/cols_agora.html | 2 +- docs/reference/cols_reach.html | 2 +- docs/reference/hbar.html | 118 ++++++++++++++++++++++ docs/reference/hbar_percent.html | 118 ++++++++++++++++++++++ docs/reference/if_not_in_stop.html | 92 +++++++++++++++++ docs/reference/if_vec_not_in_stop.html | 92 +++++++++++++++++ docs/reference/index.html | 41 +++++++- docs/reference/pal_agora.html | 2 +- docs/reference/pal_reach.html | 2 +- docs/reference/scale_color.html | 2 +- docs/reference/subvec_not_in.html | 88 ++++++++++++++++ docs/reference/theme_reach.html | 2 +- docs/reference/theme_reach_borders.html | 2 +- docs/reference/theme_reach_flip_hist.html | 6 +- docs/reference/theme_reach_hist.html | 6 +- docs/search.json | 2 +- docs/sitemap.xml | 21 ++++ man/abort_bad_argument.Rd | 21 ++++ man/buffer_bbox.Rd | 19 ++++ man/hbar.Rd | 49 +++++++++ man/hbar_percent.Rd | 49 +++++++++ man/if_not_in_stop.Rd | 23 +++++ man/if_vec_not_in_stop.Rd | 23 +++++ man/subvec_not_in.Rd | 19 ++++ man/theme_reach_flip_hist.Rd | 2 +- man/theme_reach_hist.Rd | 2 +- 40 files changed, 1057 insertions(+), 51 deletions(-) create mode 100644 R/bbox_buffer.R create mode 100644 docs/reference/abort_bad_argument.html create mode 100644 docs/reference/buffer_bbox.html create mode 100644 docs/reference/hbar.html create mode 100644 docs/reference/hbar_percent.html create mode 100644 docs/reference/if_not_in_stop.html create mode 100644 docs/reference/if_vec_not_in_stop.html create mode 100644 docs/reference/subvec_not_in.html create mode 100644 man/abort_bad_argument.Rd create mode 100644 man/buffer_bbox.Rd create mode 100644 man/hbar.Rd create mode 100644 man/hbar_percent.Rd create mode 100644 man/if_not_in_stop.Rd create mode 100644 man/if_vec_not_in_stop.Rd create mode 100644 man/subvec_not_in.Rd 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 + + + + + +
+
+
+ +
+

Abord bad argument

+
+ +
+

Usage

+
abort_bad_argument(arg, must, not = NULL)
+
+ +
+

Arguments

+
arg
+

An argument

+
must
+

What arg must be

+
not
+

Optional. What arg must not be.

+
+
+

Value

+

A stop statement

+
+ +
+ + +
+ + + +
+ + + + + + + diff --git a/docs/reference/buffer_bbox.html b/docs/reference/buffer_bbox.html new file mode 100644 index 0000000..e72c520 --- /dev/null +++ b/docs/reference/buffer_bbox.html @@ -0,0 +1,88 @@ + +Bbbox buffer — buffer_bbox • visualizeR + Skip to contents + + +
+
+
+ +
+

Bbbox buffer

+
+ +
+

Usage

+
buffer_bbox(sf_obj, buffer = 0)
+
+ +
+

Arguments

+
sf_obj
+

A `sf` object

+
buffer
+

A buffer, either one value or a vector of 4 values (left, bottom, right, top). Default to 0.

+
+
+

Value

+

A bbox with a buffer

+
+ +
+ + +
+ + + +
+ + + + + + + diff --git a/docs/reference/cols_agora.html b/docs/reference/cols_agora.html index a532207..8cd550c 100644 --- a/docs/reference/cols_agora.html +++ b/docs/reference/cols_agora.html @@ -10,7 +10,7 @@ visualizeR - 0.1.0 + 0.1.1.9000 + + + + + +
+
+
+ +
+

without any change to the x scale

+
+ +
+

Usage

+
hbar(
+  .tbl,
+  x,
+  y,
+  group = NULL,
+  initiative = "reach",
+  x_title = "",
+  y_title = "",
+  group_title = NULL,
+  font_family = "Leelawadee",
+  stack = FALSE,
+  ...
+)
+
+ +
+

Arguments

+
.tbl
+

Some data

+
x
+

Some numeric column on the x scale

+
y
+

Some column on the y scale

+
group
+

Some grouping categorical column, e.g. administrative areas

+
initiative
+

Either "reach" or "agora" or "impact" for the color palette

+
x_title
+

The x scale title. Default to empty string

+
y_title
+

The y scale title. Default to empty string

+
group_title
+

The group legend title. Defaut to NULL

+
font_family
+

The font family. Default to "Leelawadee"

+
stack
+

Should the chart be stacked? Default to "FALSE" (dodge)

+
...
+

Other arguments to be passed to "simplevis::gg_hbar" or "simplevis:gg_hbar_col"

+
+
+

Value

+

A horizontal bar chart

+
+ +
+ + +
+ + + +
+ + + + + + + diff --git a/docs/reference/hbar_percent.html b/docs/reference/hbar_percent.html new file mode 100644 index 0000000..4b951ae --- /dev/null +++ b/docs/reference/hbar_percent.html @@ -0,0 +1,118 @@ + +Simple horizontal bar chart — hbar_percent • visualizeR + Skip to contents + + +
+
+
+ +
+

with nice percentage x labels

+
+ +
+

Usage

+
hbar_percent(
+  .tbl,
+  x,
+  y,
+  group = NULL,
+  initiative = "reach",
+  x_title = "",
+  y_title = "",
+  group_title = NULL,
+  font_family = "Leelawadee",
+  stack = FALSE,
+  ...
+)
+
+ +
+

Arguments

+
.tbl
+

Some data

+
x
+

Some numeric column on the x scale

+
y
+

Some column on the y scale

+
group
+

Some grouping categorical column, e.g. administrative areas

+
initiative
+

Either "reach" or "agora" or "impact" for the color palette

+
x_title
+

The x scale title. Default to empty string

+
y_title
+

The y scale title. Default to empty string

+
group_title
+

The group legend title. Defaut to NULL

+
font_family
+

The font family. Default to "Leelawadee"

+
stack
+

Should the chart be stacked? Default to "FALSE" (dodge)

+
...
+

Other arguments to be passed to "simplevis::gg_hbar" or "simplevis:gg_hbar_col"

+
+
+

Value

+

A horizontal bar chart

+
+ +
+ + +
+ + + +
+ + + + + + + diff --git a/docs/reference/if_not_in_stop.html b/docs/reference/if_not_in_stop.html new file mode 100644 index 0000000..3045c9d --- /dev/null +++ b/docs/reference/if_not_in_stop.html @@ -0,0 +1,92 @@ + +Stop statement "If not in colnames" with colnames — if_not_in_stop • visualizeR + Skip to contents + + +
+
+
+ +
+

Stop statement "If not in colnames" with colnames

+
+ +
+

Usage

+
if_not_in_stop(.tbl, cols, df, arg = NULL)
+
+ +
+

Arguments

+
.tbl
+

A tibble

+
cols
+

A vector of column names (quoted)

+
df
+

Provide the tibble name as a character string

+
arg
+

Default to NULL.

+
+
+

Value

+

A stop statement

+
+ +
+ + +
+ + + +
+ + + + + + + diff --git a/docs/reference/if_vec_not_in_stop.html b/docs/reference/if_vec_not_in_stop.html new file mode 100644 index 0000000..c3d1e30 --- /dev/null +++ b/docs/reference/if_vec_not_in_stop.html @@ -0,0 +1,92 @@ + +Stop statement "If not in vector" — if_vec_not_in_stop • visualizeR + Skip to contents + + +
+
+
+ +
+

Stop statement "If not in vector"

+
+ +
+

Usage

+
if_vec_not_in_stop(vec, cols, vec_name, arg = NULL)
+
+ +
+

Arguments

+
vec
+

A vector of character strings

+
cols
+

A set of character strings

+
vec_name
+

Provide the vector name as a character string

+
arg
+

Default to NULL.

+
+
+

Value

+

A stop statement if some elements of vec are not in cols

+
+ +
+ + +
+ + + +
+ + + + + + + diff --git a/docs/reference/index.html b/docs/reference/index.html index e898b76..ca94559 100644 --- a/docs/reference/index.html +++ b/docs/reference/index.html @@ -10,7 +10,7 @@ visualizeR - 0.1.0 + 0.1.1.9000 + + + + + +
+
+
+ +
+

Subvec not in

+
+ +
+

Usage

+
subvec_not_in(vector, set)
+
+ +
+

Arguments

+
vector
+

A vector to subset

+
set
+

A set-vector

+
+
+

Value

+

A subset of vector not in set

+
+ +
+ + +
+ + + +
+ + + + + + + diff --git a/docs/reference/theme_reach.html b/docs/reference/theme_reach.html index 6c3bd12..dce4397 100644 --- a/docs/reference/theme_reach.html +++ b/docs/reference/theme_reach.html @@ -10,7 +10,7 @@ visualizeR - 0.1.0 + 0.1.1.9000