Fix R CMD checks and update README

This commit is contained in:
gnoblet 2025-07-03 11:52:39 +02:00
parent 0aae59491e
commit 6b508150a7
33 changed files with 498 additions and 368 deletions

116
R/bar.R
View file

@ -2,13 +2,14 @@
#'
#' @inheritParams bar
#'
#' @param ... Additional arguments passed to `bar()`
#'
#' @export
hbar <- function(
...,
flip = TRUE,
add_text = FALSE,
theme_fun = theme_bar(flip = flip, add_text = add_text)
) {
...,
flip = TRUE,
add_text = FALSE,
theme_fun = theme_bar(flip = flip, add_text = add_text)) {
bar(flip = flip, add_text = add_text, theme_fun = theme_fun, ...)
}
@ -48,6 +49,8 @@ hbar <- function(
#' @param add_text_expand_limit Default to adding 10\% on top of the bar.
#' @param add_text_round Round the text label.
#' @param theme_fun Whatever theme function. For no custom theme, use theme_fun = NULL.
#' @param scale_fill_fun Scale fill function. Default to scale_fill_visualizer_discrete().
#' @param scale_color_fun Scale color function. Default to scale_color_visualizer_discrete().
#'
#' @inheritParams reorder_by
#'
@ -55,48 +58,47 @@ hbar <- function(
#'
#' @export
bar <- function(
df,
x,
y,
group = "",
facet = "",
order = "none",
x_rm_na = TRUE,
y_rm_na = TRUE,
group_rm_na = TRUE,
facet_rm_na = TRUE,
y_expand = 0.1,
add_color = color("cat_5_main_1"),
add_color_guide = TRUE,
flip = FALSE,
wrap = NULL,
position = "dodge",
alpha = 1,
x_title = NULL,
y_title = NULL,
group_title = NULL,
title = NULL,
subtitle = NULL,
caption = NULL,
width = 0.8,
add_text = FALSE,
add_text_size = 4.5,
add_text_color = color("dark_grey"),
add_text_font_face = "bold",
add_text_threshold_display = 0.05,
add_text_suffix = "%",
add_text_expand_limit = 1.2,
add_text_round = 1,
theme_fun = theme_bar(
flip = flip,
add_text = add_text,
axis_text_x_angle = 0,
axis_text_x_vjust = 0.5,
axis_text_x_hjust = 0.5
),
scale_fill_fun = scale_fill_visualizer_discrete(),
scale_color_fun = scale_color_visualizer_discrete()
) {
df,
x,
y,
group = "",
facet = "",
order = "none",
x_rm_na = TRUE,
y_rm_na = TRUE,
group_rm_na = TRUE,
facet_rm_na = TRUE,
y_expand = 0.1,
add_color = color("cat_5_main_1"),
add_color_guide = TRUE,
flip = FALSE,
wrap = NULL,
position = "dodge",
alpha = 1,
x_title = NULL,
y_title = NULL,
group_title = NULL,
title = NULL,
subtitle = NULL,
caption = NULL,
width = 0.8,
add_text = FALSE,
add_text_size = 4.5,
add_text_color = color("dark_grey"),
add_text_font_face = "bold",
add_text_threshold_display = 0.05,
add_text_suffix = "%",
add_text_expand_limit = 1.2,
add_text_round = 1,
theme_fun = theme_bar(
flip = flip,
add_text = add_text,
axis_text_x_angle = 0,
axis_text_x_vjust = 0.5,
axis_text_x_hjust = 0.5
),
scale_fill_fun = scale_fill_visualizer_discrete(),
scale_color_fun = scale_color_visualizer_discrete()) {
#------ Checks
# df is a data frame
@ -349,7 +351,7 @@ bar <- function(
vjust_flip <- -0.5
}
# function for interaction
# function for interactio
interaction_f <- function(group, facet, data) {
if (group == "" && facet == "") {
return(NULL)
@ -366,14 +368,7 @@ bar <- function(
# add text labels
if (add_text & position == "dodge") {
df <- dplyr::mutate(
df,
"y_threshold" := ifelse(
!!rlang::sym(y) >= add_text_threshold_display,
!!rlang::sym(y),
NA
)
)
df$y_threshold <- ifelse(df[[y]] >= add_text_threshold_display, df[[y]], NA)
# expand limits
g <- g +
@ -407,14 +402,7 @@ bar <- function(
position = ggplot2::position_dodge2(width = dodge_width)
)
} else if (add_text & position == "stack") {
df <- dplyr::mutate(
df,
"y_threshold" := ifelse(
!!rlang::sym(y) >= add_text_threshold_display,
!!rlang::sym(y),
NA
)
)
df$y_threshold <- ifelse(df[[y]] >= add_text_threshold_display, df[[y]], NA)
g <- g +
ggplot2::geom_text(

View file

@ -23,17 +23,3 @@
`%notallin%` <- function(a, b) {
!(all(a %in% b))
}
# infix for null replacement
#' @importFrom rlang `%||%`
#' If Null Replace Operator
#'
#' An alias for the `%||%` operator that returns `a` if it's not NULL, otherwise returns `b`.
#'
#' @param a First value to test
#' @param b Value to use if `a` is NULL
#'
#' @return `a` if not NULL, otherwise `b`
`%ifnullrep%` <- function(a, b) {
a %||% b
}

View file

@ -1,6 +1,7 @@
#' @rdname lollipop
#'
#' @inheritParams lollipop
#' @param ... Additional arguments passed to `lollipop()`
#'
#' @export
hlollipop <- function(
@ -12,6 +13,7 @@ hlollipop <- function(
#' Simple lollipop chart
#'
#' @description
#' `lollipop()` is a simple lollipop chart (dots connected to the baseline by a segment) with some customization allowed.
#' `hlollipop()` uses `lollipop()` with sane defaults for a horizontal lollipop chart.
#'
@ -41,14 +43,24 @@ hlollipop <- function(
#' @param line_color The color of the line connecting dots to the baseline.
#' @param dodge_width Width for position dodge when using groups (controls space between grouped lollipops).
#' @param theme_fun Whatever theme function. For no custom theme, use theme_fun = NULL.
#' @param scale_fill_fun Scale fill function.
#' @param scale_color_fun Scale color function.
#' @param scale_fill_fun Scale fill function. Default to scale_fill_visualizer_discrete().
#' @param scale_color_fun Scale color function. Default to scale_color_visualizer_discrete().
#'
#'
#' @inheritParams reorder_by
#'
#' @importFrom rlang `:=`
#'
#' @return A ggplot object
#' @export
#' @examples
#' \dontrun{
#' df <- data.frame(x = letters[1:5], y = c(10, 5, 7, 12, 8))
#' # Vertical lollipop
#' lollipop(df, "x", "y")
#' # Horizontal lollipop
#' hlollipop(df, "x", "y")
#' }
lollipop <- function(
df,
x,

View file

@ -22,36 +22,35 @@
#' @param subtitle Plot subtitle. Default to NULL.
#' @param caption Plot caption. Default to NULL.
#' @param theme_fun Whatever theme. Default to theme_point(). NULL if no theming needed.
#'
#' @inheritParams scale_color_visualizer_discrete
#' @param scale_fill_fun Scale fill function. Default to scale_fill_visualizer_discrete().
#' @param scale_color_fun Scale color function. Default to scale_color_visualizer_discrete().
#'
#' @export
point <- function(
df,
x,
y,
group = "",
facet = "",
facet_scales = "free",
x_rm_na = TRUE,
y_rm_na = TRUE,
group_rm_na = TRUE,
facet_rm_na = TRUE,
add_color = color("cat_5_main_1"),
add_color_guide = TRUE,
flip = TRUE,
alpha = 1,
size = 2,
x_title = NULL,
y_title = NULL,
group_title = NULL,
title = NULL,
subtitle = NULL,
caption = NULL,
theme_fun = theme_point(),
scale_fill_fun = scale_fill_visualizer_discrete(),
scale_color_fun = scale_color_visualizer_discrete()
) {
df,
x,
y,
group = "",
facet = "",
facet_scales = "free",
x_rm_na = TRUE,
y_rm_na = TRUE,
group_rm_na = TRUE,
facet_rm_na = TRUE,
add_color = color("cat_5_main_1"),
add_color_guide = TRUE,
flip = TRUE,
alpha = 1,
size = 2,
x_title = NULL,
y_title = NULL,
group_title = NULL,
title = NULL,
subtitle = NULL,
caption = NULL,
theme_fun = theme_point(),
scale_fill_fun = scale_fill_visualizer_discrete(),
scale_color_fun = scale_color_visualizer_discrete()) {
#------ Checks
# df is a data frame

View file

@ -5,6 +5,7 @@
#' @inheritParams palette_gen
#'
#' @param reverse_guide Boolean indicating whether the guide should be reversed.
#' @param title_position Position of the title. See [ggplot2::guide_legend()]'s title.position argument.
#' @param ... Additional arguments passed to [ggplot2::discrete_scale()] if discrete or [ggplot2::scale_fill_gradient()] if continuous.
#'
#' @export

View file

@ -2,16 +2,18 @@
#'
#' @return A custom theme object.
#'
#'
#' @rdname theme_default
#'
#' @inheritParams bar
#'
#' @export
theme_bar <- function(
flip = TRUE,
add_text = FALSE,
axis_text_x_angle = 0,
axis_text_x_vjust = 0.5,
axis_text_x_hjust = 0.5
) {
flip = TRUE,
add_text = FALSE,
axis_text_x_angle = 0,
axis_text_x_vjust = 0.5,
axis_text_x_hjust = 0.5) {
# If add_text is TRUE, flip is FALSE
if (!flip && !add_text) {
par_axis_text_font_face <- "plain"

View file

@ -1,11 +1,17 @@
#' ggplot2 theme wrapper with fonts and colors
#'
#' @param font_family The font family for all plot's texts. Default to "Segoe UI".
#' @param title_size The size of the title. Defaults to 12.
#' @param title_color Title color.
#' @param title_font_face Title font face. Default to "bold". Font face ("plain", "italic", "bold", "bold.italic").
#' @param title_hjust Title horizontal justification. Default to NULL. Use 0.5 to center the title.
#' @param title_font_family Title font family. Default to "Roboto Condensed".
#' @param title_font_family Title font family. Default to "Carlito".
#' @param title_position_to_plot TRUE or FALSE. Positioning to plot or to panel?
#' @param subtitle_font_family Subtitle font family. Default to "Carlito".
#' @param subtitle_size The size of the subtitle. Defaults to 10.
#' @param subtitle_color Subtitle color.
#' @param subtitle_font_face Subtitle font face. Default to "plain". Font face ("plain", "italic", "bold", "bold.italic").
#' @param subtitle_hjust Subtitle horizontal justification. Default to NULL. Use 0.5 to center the subtitle.
#' @param text_font_family Text font family. Default to "Carlito".
#' @param text_size The size of all text other than the title, subtitle and caption. Defaults to 10.
#' @param text_color Text color.
#' @param text_font_face Text font face. Default to "bold". Font face ("plain", "italic", "bold", "bold.italic").
@ -18,16 +24,21 @@
#' @param legend_title_size Legend title size.
#' @param legend_title_color Legend title color.
#' @param legend_title_font_face Legend title font face. Default to "plain". Font face ("plain", "italic", "bold", "bold.italic").
#' @param legend_title_font_family Legend title font family. Default to "Carlito".
#' @param legend_text_size Legend text size.
#' @param legend_text_color Legend text color.
#' @param legend_text_font_face Legend text font face. Default to "plain". Font face ("plain", "italic", "bold", "bold.italic").
#' @param legend_text_font_family Legend text font family. Default to "Carlito".
#'
#' @param legend_reverse Reverse the color in the guide? Default to TRUE.
#' @param title_size The size of the legend title. Defaults to 11.
#' @param title_color Legend title color.
#' @param title_font_face Legend title font face. Default to "plain". Font face ("plain", "italic", "bold", "bold.italic").
#' @param title_position_to_plot TRUE or FALSE. Positioning to plot or to panel?
#' @param facet_size Facet font size.
#' @param facet_color Facet font color.
#' @param facet_font_face Facet font face. Default to "plain". Font face ("plain", "italic", "bold", "bold.italic").
#' @param facet_font_family Facet font family. Default to "Carlito".
#' @param facet_bg_color Facet background color.
#' @param axis_x Boolean. Do you need x-axis?
#' @param axis_y Boolean. Do you need y-axis?
#' @param axis_text_font_family Axis text font family. Default to "Carlito".
#' @param axis_text_size Axis text size.
#' @param axis_text_color Axis text color.
#' @param axis_text_font_face Axis text font face. Default to "plain". Font face ("plain", "italic", "bold", "bold.italic").
@ -53,7 +64,11 @@
#' @param grid_minor_x_size Minor X line size.
#' @param grid_minor_y_size Minor Y line size.
#' @param grid_minor_color Minor grid lines color.
#' @param caption_font_family Caption font family. Default to "Carlito".
#' @param caption_font_face Caption font face. Default to "plain". Font face ("plain", "italic", "bold", "bold.italic").
#' @param caption_position_to_plot TRUE or FALSE. Positioning to plot or to panel?
#' @param caption_size The size of the caption. Defaults to 10.
#' @param caption_color Caption color.
#' @param ... Additional arguments passed to [ggplot2::theme()].
#'
#'
@ -61,76 +76,75 @@
#'
#' @export
theme_default <- function(
title_font_family = "Carlito",
title_size = 20,
title_color = color("dark_grey"),
title_font_face = "bold",
title_hjust = NULL,
title_position_to_plot = TRUE,
subtitle_font_family = "Carlito",
subtitle_size = 16,
subtitle_color = color("dark_grey"),
subtitle_font_face = "plain",
subtitle_hjust = NULL,
text_font_family = "Carlito",
text_size = 14,
text_color = color("dark_grey"),
text_font_face = "plain",
panel_background_color = "#FFFFFF",
panel_border = FALSE,
panel_border_color = color("dark_grey"),
legend_position = "top",
legend_direction = "horizontal",
legend_justification = "center",
legend_reverse = TRUE,
legend_title_size = 14,
legend_title_color = color("dark_grey"),
legend_title_font_face = "plain",
legend_title_font_family = "Carlito",
legend_text_size = 14,
legend_text_color = color("dark_grey"),
legend_text_font_face = "plain",
legend_text_font_family = "Carlito",
facet_size = 15,
facet_color = color("dark_grey"),
facet_font_face = "bold",
facet_font_family = "Carlito",
facet_bg_color = color("lighter_grey"),
axis_x = TRUE,
axis_y = TRUE,
axis_text_x = TRUE,
axis_line_x = FALSE,
axis_ticks_x = FALSE,
axis_text_y = TRUE,
axis_line_y = TRUE,
axis_ticks_y = TRUE,
axis_text_font_family = "Carlito",
axis_text_size = 14,
axis_text_color = color("dark_grey"),
axis_text_font_face = "plain",
axis_title_size = 15,
axis_title_color = color("dark_grey"),
axis_title_font_face = "plain",
axis_text_x_angle = 0,
axis_text_x_vjust = 0.5,
axis_text_x_hjust = 0.5,
grid_major_x = TRUE,
grid_major_y = FALSE,
grid_major_color = color("dark_grey"),
grid_major_x_size = 0.1,
grid_major_y_size = 0.1,
grid_minor_x = TRUE,
grid_minor_y = FALSE,
grid_minor_color = color("dark_grey"),
grid_minor_x_size = 0.05,
grid_minor_y_size = 0.05,
caption_font_family = "Carlito",
caption_font_face = "plain",
caption_position_to_plot = TRUE,
caption_size = 12,
caption_color = color("dark_grey"),
...
) {
title_font_family = "Carlito",
title_size = 20,
title_color = color("dark_grey"),
title_font_face = "bold",
title_hjust = NULL,
title_position_to_plot = TRUE,
subtitle_font_family = "Carlito",
subtitle_size = 16,
subtitle_color = color("dark_grey"),
subtitle_font_face = "plain",
subtitle_hjust = NULL,
text_font_family = "Carlito",
text_size = 14,
text_color = color("dark_grey"),
text_font_face = "plain",
panel_background_color = "#FFFFFF",
panel_border = FALSE,
panel_border_color = color("dark_grey"),
legend_position = "top",
legend_direction = "horizontal",
legend_justification = "center",
legend_reverse = TRUE,
legend_title_size = 14,
legend_title_color = color("dark_grey"),
legend_title_font_face = "plain",
legend_title_font_family = "Carlito",
legend_text_size = 14,
legend_text_color = color("dark_grey"),
legend_text_font_face = "plain",
legend_text_font_family = "Carlito",
facet_size = 15,
facet_color = color("dark_grey"),
facet_font_face = "bold",
facet_font_family = "Carlito",
facet_bg_color = color("lighter_grey"),
axis_x = TRUE,
axis_y = TRUE,
axis_text_x = TRUE,
axis_line_x = FALSE,
axis_ticks_x = FALSE,
axis_text_y = TRUE,
axis_line_y = TRUE,
axis_ticks_y = TRUE,
axis_text_font_family = "Carlito",
axis_text_size = 14,
axis_text_color = color("dark_grey"),
axis_text_font_face = "plain",
axis_title_size = 15,
axis_title_color = color("dark_grey"),
axis_title_font_face = "plain",
axis_text_x_angle = 0,
axis_text_x_vjust = 0.5,
axis_text_x_hjust = 0.5,
grid_major_x = TRUE,
grid_major_y = FALSE,
grid_major_color = color("dark_grey"),
grid_major_x_size = 0.1,
grid_major_y_size = 0.1,
grid_minor_x = TRUE,
grid_minor_y = FALSE,
grid_minor_color = color("dark_grey"),
grid_minor_x_size = 0.05,
grid_minor_y_size = 0.05,
caption_font_family = "Carlito",
caption_font_face = "plain",
caption_position_to_plot = TRUE,
caption_size = 12,
caption_color = color("dark_grey"),
...) {
# Basic simple theme
theme <- ggplot2::theme_minimal()

View file

@ -6,8 +6,5 @@
#'
#' @export
theme_dumbbell <- function() {
theme_default(
axis_line_x = TRUE,
grid_
)
theme_default()
}

View file

@ -1,10 +1,27 @@
#' Custom Theme for Lollipop Charts
#'
#' @return A custom theme object.
#' @description
#' A custom theme specifically designed for lollipop charts with appropriate grid lines and axis styling
#' based on whether the chart is flipped (horizontal) or not.
#'
#' @param flip Logical indicating whether the lollipop chart is flipped (horizontal). Default is TRUE.
#' @param axis_text_x_angle Angle for x-axis text labels. Default is 0.
#' @param axis_text_x_vjust Vertical justification for x-axis text labels. Default is 0.5.
#' @param axis_text_x_hjust Horizontal justification for x-axis text labels. Default is 0.5.
#'
#' @return A ggplot2 theme object
#'
#' @rdname theme_default
#'
#' @export
#'
#' @examples
#' \dontrun{
#' library(ggplot2)
#' df <- data.frame(x = letters[1:5], y = c(10, 5, 7, 12, 8))
#' ggplot(df, aes(x, y)) +
#' geom_point() +
#' theme_lollipop()
#' }
theme_lollipop <- function(
flip = TRUE,
axis_text_x_angle = 0,