This commit is contained in:
gnoblet 2025-01-19 20:06:31 +01:00
parent a9b8b5f708
commit 5beec7fb90
22 changed files with 782 additions and 171 deletions

View file

@ -10,3 +10,4 @@
^docs$
^pkgdown$
^data-raw$
^test-example.R

View file

@ -1 +0,0 @@
source("renv/activate.R")

View file

@ -31,7 +31,9 @@ Imports:
ggalluvial,
viridisLite,
waffle,
stringr
stringr,
checkmate,
data.table
Suggests:
knitr,
roxygen2,

152
R/bar.R
View file

@ -1,11 +1,25 @@
#' @rdname bar
#'
#' @inheritParams bar
#'
#' @export
hbar <- function(...) bar(flip = TRUE, theme_fun = theme_bar(flip = TRUE, add_text = FALSE), ...)
#' Simple bar chart
#'
#' [bar()] is a simple bar chart with some customization allowed, in particular the `theme_fun` argument for theming. [hbar()] uses [bar()] with sane defaults for a horizontal bar chart.
#'
#' @param df A data frame.
#' @param x A quoted numeric column.
#' @param y A quoted character column or coercible as a character column.
#' @param group Some quoted grouping categorical column, e.g. administrative areas or population groups.
#' @param facet Some quoted grouping categorical column, e.g. administrative areas or population groups.
#' @param order Should bars be ordered? "none" if no, "y" if yes based on y, "grouped" if yes based on y and group.
#' @param x_rm_na Remove NAs in x?
#' @param y_rm_na Remove NAs in y?
#' @param group_rm_na Remove NAs in group?
#' @param add_color Add a color to bars (if no grouping).
#' @param flip TRUE or FALSE. Default to TRUE or horizontal bar plot.
#' @param flip TRUE or FALSE (default). Default to TRUE or horizontal bar plot.
#' @param wrap Should x-labels be wrapped? Number of characters.
#' @param position Should the chart be stacked? Default to "dodge". Can take "dodge" and "stack".
#' @param alpha Fill transparency.
@ -25,11 +39,8 @@
#' @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_impact Use the package custom scales for fill and color.
#'
#' @inheritParams scale_color_impact_discrete
#'
#' @importFrom rlang `%||%`
#' @inheritParams reorder
#'
#' @export
bar <- function(
@ -37,8 +48,14 @@ bar <- function(
x,
y,
group = "",
add_color = color("dark_grey"),
flip = TRUE,
facet = "",
order = "none",
x_rm_na = TRUE,
y_rm_na = TRUE,
group_rm_na = TRUE,
facet_rm_na = TRUE,
add_color = color("cat_5_main_1"),
flip = FALSE,
wrap = NULL,
position = "dodge",
alpha = 1,
@ -48,15 +65,62 @@ bar <- function(
title = NULL,
subtitle = NULL,
caption = NULL,
width = 0.5,
add_text = TRUE,
add_text_size = 5,
width = 0.7,
add_text = FALSE,
add_text_size = 4,
add_text_color = color("dark_grey"),
add_text_font_face = "plain",
add_text_threshold_display = 0.05,
add_text_suffix = "%",
add_text_expand_limit = 1.2,
add_text_round = 1){
add_text_round = 1,
theme_fun = theme_bar(
flip = FALSE,
add_text = FALSE,
axis_text_x_angle = 45,
axis_text_x_vjust = 1,
axis_text_x_hjust = 1
),
scale_fill_fun = scale_fill_impact_discrete,
scale_color_fun = scale_color_impact_discrete
){
#------ Checks
# df is a data frame
checkmate::assert_data_frame(df)
# x and y and group are character
checkmate::assert_character(x, len = 1)
checkmate::assert_character(y, len = 1)
checkmate::assert_character(group, len = 1)
# x and y are columns in df
checkmate::assert_choice(x, colnames(df))
checkmate::assert_choice(y, colnames(df))
if (group != "") checkmate::assert_choice(group, colnames(df))
# x_rm_na, y_rm_na and group_rm_na are logical scalar
checkmate::assert_logical(x_rm_na, len = 1)
checkmate::assert_logical(y_rm_na, len = 1)
checkmate::assert_logical(group_rm_na, len = 1)
# flip is a logical scalar
checkmate::assert_logical(flip, len = 1)
# add_text_threshold_display is a numeric scalar
checkmate::assert_numeric(add_text_threshold_display, len = 1)
# add_text_suffix is a character scalar
checkmate::assert_character(add_text_suffix, len = 1)
# add_text_expand_limit is a numeric scalar
checkmate::assert_numeric(add_text_expand_limit, len = 1)
# add_text_round is a numeric scalar
checkmate::assert_numeric(add_text_round, len = 1)
# Check if numeric and character
if (class(df[[y]]) %notin% c("integer", "numeric")) rlang::abort(paste0(y, " must be numeric."))
@ -65,6 +129,24 @@ if (!any(class(df[[x]]) %in% c("character", "factor"))) rlang::abort(paste0(x, "
# Check if position is stack or dodge
if (position %notin% c("stack", "dodge")) rlang::abort("Position should be either 'stack' or 'dodge'.")
#----- Data wrangling
# want to use df as a data.table
if (!checkmate::test_data_table(df)) {
rlang::warn("Converting df to data.table.")
data.table::setDT(df)
}
# Remove NAs using data.table
if (x_rm_na) df[, (x) := na.omit(get(x))]
if (y_rm_na) df[, (y) := na.omit(get(y))]
if (group != "" && group_rm_na) df[, (group) := na.omit(get(group))]
# Reorder
dir_order = ifelse(flip, 1, -1)
df <- reorder(df, x, y, group, order, dir_order)
# Prepare aes
if(group != "") {
g <- ggplot2::ggplot(
@ -99,9 +181,31 @@ g <- g + ggplot2::labs(
fill = group_title
)
# Width
width <- width
dodge_width <- width
#Facets
if (facet != "") {
g <- g + ggforce::facet_row(facet, scales = "free_x", space = "free")
}
# Guides for legend
# g <- g + ggplot2::guides(
# fill = ggplot2::guide_legend(
# title.position = "left",
# title.hjust = 0,
# label.hjust = 0.5,
# #label.position = "bottom",
# direction = "horizontal"),
# color = ggplot2::guide_legend(
# title.position = "left",
# title.hjust = 0,
# label.hjust = 0.5,
# #label.position = "bottom",
# direction = "horizontal")
# )
# Should the graph use position_fill?
if(group != "") {
@ -156,9 +260,7 @@ if(group != "") {
}
}
# Expand scale
g <- g + ggplot2::scale_y_continuous(expand = c(0, 0))
# Wrap labels on the x scale?
if (!is.null(wrap)) {
g <- g + ggplot2::scale_x_discrete(labels = scales::label_wrap(wrap))
}
@ -195,8 +297,7 @@ if (add_text & position == "dodge") {
position = ggplot2::position_dodge2(width = dodge_width)
)
} else if (add_text & position == "stack") {
} else if (add_text & position == "stack") {
df <- dplyr::mutate(df, "y_threshold" := ifelse(!!rlang::sym(y) >= add_text_threshold_display, !!rlang::sym(y), NA ))
@ -211,11 +312,24 @@ if (add_text & position == "dodge") {
position = ggplot2::position_stack(vjust = 0.5)
)
}
}
# Remove trailing 0
! no applicable method for 'round_any' applied to an object of class "character"
# y scale tweaks
g <- g +
ggplot2::scale_y_continuous(
# start at 0
expand = c(0, 0),
# remove trailing 0 and choose accuracy of y labels
labels = scales::label_number(
accuracy = 0.1,
drop0trailing = TRUE,
big.mark = "",
decimal.mark = "."),
)
# Add theme fun
if (!is.null(theme_fun)) g <- g + theme_fun
return(g)
}

104
R/reorder.R Normal file
View file

@ -0,0 +1,104 @@
#' Reorder a Data Frame Factoring Column x
#'
#' @param df A data frame to be reordered.
#' @param x A character scalar specifying the column to be reordered.
#' @param y A character scalar specifying the column to order by if ordering by values.
#' @param group A character scalar specifying the grouping column (optional).
#' @param order A character scalar specifying the order type (one of "none", "y", "grouped"). See details.
#' @param dir_order A logical scalar specifying whether to flip the order.
#'
#' @details Ordering takes the following possible values:
#'
#' * "none": No reordering.
#' * "y": Order by values of y.
#' * "grouped_y": Order by values of y and group.
#' * "x": Order alphabetically by x.
#' * "grouped_x": Order alphabetically by x and group.
#'
#'
#' @return The reordered data frame.
#'
#' @examples
#' # Example usage
#' df <- data.frame(col1 = c("b", "a", "c"), col2 = c(10, 25, 3))
#' reorder(df, "col1", "col2")
#' @export
reorder <- function(df, x, y, group = "", order = "y", dir_order = 1){
#------ Checks
# df is a data frame
checkmate::assert_data_frame(df)
# df is data.table, if not convert
if (!checkmate::test_data_table(df)) {
rlang::warn("Converting df to data.table.")
data.table::setDT(df)
}
# x and y are character scalar and in df
checkmate::assert_character(x, len = 1)
checkmate::assert_character(y, len = 1)
checkmate::assert_subset(x, colnames(df))
checkmate::assert_subset(y, colnames(df))
# group is character scalar and in df if not empty
checkmate::assert_character(group, len = 1)
if (group != "") checkmate::assert_subset(group, colnames(df))
# order is a character scalar in c("none", "y", "grouped")
checkmate::assert_choice(order, c("none", "y", "grouped_y", "x", "grouped_x"))
# dir_order is 1 or -1 (numeric scalar)
checkmate::assert_subset(dir_order, c(1, -1))
#------ Reorder
# droplevels first
if (is.factor(df[[x]])) {
df[, (x) := droplevels(get(x))]
}
# reording options
if (order == "y") {
data.table::setorderv(df, y, order = dir_order)
df[, (x) := forcats::fct_inorder(get(x))]
} else if (order == "grouped" && group == "") {
rlang::warn("Group is empty. Ordering by y only.")
data.table::setorderv(df, y, order = dir_order)
df[, (x) := forcats::fct_inorder(get(x))]
} else if (order == "grouped_y" && group != "") {
data.table::setorderv(df, c(group, y), order = dir_order)
df[, (x) := forcats::fct_inorder(get(x))]
} else if (order == "x") {
data.table::setorderv(df, x, order = dir_order)
df[, (x) := forcats::fct_inorder(get(x))]
} else if (order == "grouped_x" && group != "") {
data.table::setorderv(df, c(group, x), order = dir_order)
df[, (x) := forcats::fct_inorder(get(x))]
} else if (order == "grouped_x" && group == "") {
rlang::warn("Group is empty. Ordering by x only.")
data.table::setorderv(df, x, order = dir_order)
df[, (x) := forcats::fct_inorder(get(x))]
}
return(df)
}

View file

@ -9,7 +9,7 @@
#' @param ... Additional arguments passed to [ggplot2::discrete_scale()] if discrete or [ggplot2::scale_fill_gradient()] if continuous.
#'
#' @export
scale_visualizer_discrete <- function(palette = "cat_5_main", direction = 1, reverse_guide = TRUE, ...) {
scale_visualizer_discrete <- function(palette = "cat_5_main", direction = 1, reverse_guide = TRUE, title_position = NULL, ...) {
s <- scale_color_visualizer_discrete(palette, direction, reverse_guide, ...) +
scale_fill_visualizer_discrete(palette, direction, reverse_guide, ...)
@ -21,7 +21,7 @@ scale_visualizer_discrete <- function(palette = "cat_5_main", direction = 1, rev
#' @rdname scale_visualizer_dicscrete
#'
#' @export
scale_visualizer_continuous <- function(palette = "seq_5_main", direction = 1, reverse_guide = TRUE, ...) {
scale_visualizer_continuous <- function(palette = "seq_5_main", direction = 1, reverse_guide = TRUE, title_position = NULL, ...) {
s <- scale_color_visualizer_continuous(palette, direction, reverse_guide, ...) +
scale_fill_visualizer_continuous(palette, direction, reverse_guide, ...)
@ -40,14 +40,14 @@ scale_visualizer_continuous <- function(palette = "seq_5_main", direction = 1, r
#' @param ... Additional arguments passed to [ggplot2::discrete_scale()] if discrete or [ggplot2::scale_fill_gradient()] if continuous.
#'
#' @export
scale_color_visualizer_discrete <- function(palette = "cat_5_main", direction = 1, reverse_guide = TRUE, ...) {
scale_color_visualizer_discrete <- function(palette = "cat_5_main", direction = 1, reverse_guide = TRUE, title_position = NULL, ...) {
if (!(is.null(palette))) {
ggplot2::discrete_scale(
"color",
palette = palette_gen(palette, "categorical", direction),
guide = ggplot2::guide_legend(
title.position = "top",
title.position = title_position,
draw.ulim = TRUE,
draw.llim = TRUE,
# ticks.colour = "#F1F3F5",
@ -56,11 +56,10 @@ scale_color_visualizer_discrete <- function(palette = "cat_5_main", direction =
...
)
} else {
ggplot2::scale_colour_viridis_d(
direction = direction,
guide = ggplot2::guide_legend(
title.position = "top",
title.position = title_position,
draw.ulim = TRUE,
draw.llim = TRUE,
# ticks.colour = "#F1F3F5",
@ -68,23 +67,20 @@ scale_color_visualizer_discrete <- function(palette = "cat_5_main", direction =
),
...
)
}
}
#' @rdname scale_color_visualizer_discrete
#'
#' @export
scale_fill_visualizer_discrete <- function(palette = "cat_5_main", direction = 1, reverse_guide = TRUE, ...) {
scale_fill_visualizer_discrete <- function(palette = "cat_5_main", direction = 1, reverse_guide = TRUE, title_position = NULL, ...) {
if (!(is.null(palette))) {
ggplot2::discrete_scale(
"fill",
palette = palette_gen(palette, "categorical", direction),
guide = ggplot2::guide_legend(
title.position = "top",
title.position = title_position,
draw.ulim = TRUE,
draw.llim = TRUE,
# ticks.colour = "#F1F3F5",
@ -93,11 +89,10 @@ scale_fill_visualizer_discrete <- function(palette = "cat_5_main", direction = 1
...
)
} else {
ggplot2::scale_fill_viridis_d(
direction = direction,
guide = ggplot2::guide_legend(
title.position = "top",
title.position = title_position,
draw.ulim = TRUE,
draw.llim = TRUE,
# ticks.colour = "#F1F3F5",
@ -105,24 +100,21 @@ scale_fill_visualizer_discrete <- function(palette = "cat_5_main", direction = 1
),
...
)
}
}
#' @rdname scale_color_visualizer_discrete
#'
#' @export
scale_fill_visualizer_continuous <- function(palette = "seq_5_main", direction = 1, reverse_guide = TRUE, ...) {
scale_fill_visualizer_continuous <- function(palette = "seq_5_main", direction = 1, reverse_guide = TRUE, title_position = NULL, ...) {
if (!(is.null(palette))) {
pal <- palette_gen(palette, "continuous", direction)
ggplot2::scale_fill_gradientn(
colors = pal(256),
guide = ggplot2::guide_colorbar(
title.position = "top",
title.position = title_position,
draw.ulim = TRUE,
draw.llim = TRUE,
# ticks.colour = "#F1F3F5",
@ -130,37 +122,32 @@ scale_fill_visualizer_continuous <- function(palette = "seq_5_main", direction =
),
...
)
} else {
ggplot2::scale_fill_viridis_c(
option = "magma",
guide = ggplot2::guide_colorbar(
title.position = "top",
title.position = title_position,
draw.ulim = TRUE,
draw.llim = TRUE,
# ticks.colour = "#F1F3F5",
reverse = reverse_guide
),
...)
}
}
#' @rdname scale_color_visualizer_discrete
#'
#' @export
scale_color_visualizer_continuous <- function(palette = "seq_5_main", direction = 1, reverse_guide = TRUE, ...) {
scale_color_visualizer_continuous <- function(palette = "seq_5_main", direction = 1, reverse_guide = TRUE, title_position = NULL, ...) {
if (!(is.null(palette))) {
pal <- palette_gen(palette, "continuous", direction)
ggplot2::scale_fill_gradientn(
colors = pal(256),
guide = ggplot2::guide_colorbar(
title.position = "top",
title.position = title_position,
draw.ulim = TRUE,
draw.llim = TRUE,
# ticks.colour = "#F1F3F5",
@ -168,20 +155,16 @@ scale_color_visualizer_continuous <- function(palette = "seq_5_main", direction
),
...
)
} else {
ggplot2::scale_colour_viridis_c(
option = "magma",
guide = ggplot2::guide_colorbar(
title.position = "top",
title.position = title_position,
draw.ulim = TRUE,
draw.llim = TRUE,
# ticks.colour = "#F1F3F5",
reverse = reverse_guide
),
....)
}
}

View file

@ -1,22 +0,0 @@
dat <- data.frame(
x = c(15, 34, 59, 21, 33, 66),
y = c("Admin A", "Admin B", "Admin C", "Admin C", "Admin B", "Admin A"),
group = c("Displaced", "Non displaced", "Non displaced", "Displaced", "Displaced", "Non displaced")
)
dat |>
bar(
x = "y",
y = "x",
group = "group",
flip = F,
add_text = F,
title = "In Admin A and C, Non-Displaced Persons Face Greater WASH Challenges Than Their Displaced Counterparts",
subtitle = "% of households not accessing WASH services by admin 1 and status",
caption = "Source: FAO 2022. No message is a real one. Fake data are used in this example. As a cautiom, no decision should be made based on this plot.",
) +
theme_visualizer_bar() +
scale_color_visualizer_discrete() +
scale_fill_visualizer_discrete()

147
R/theme.R
View file

@ -1,22 +1,3 @@
#' ggplot2 theme for bar charts with sane defaults
#'
#' @rdname theme_visualizer
#' @inheritParams theme_visualizer
#'
#' @export
theme_visualizer_bar <- function(...) {
theme_visualizer_default(
grid_major_y = TRUE
, axis_line_y = FALSE
, axis_ticks_y = FALSE
, grid_major_x = FALSE
, ...
)
}
#' ggplot2 theme wrapper with fonts and colors
#'
#' @param font_family The font family for all plot's texts. Default to "Segoe UI".
@ -79,42 +60,51 @@ theme_visualizer_bar <- function(...) {
#' @description Give some reach colors and fonts to a ggplot.
#'
#' @export
theme_visualizer_default <- function(
font_family = "Carlito",
title_size = 14,
theme_default <- function(
title_font_family = "Carlito",
title_size = 16,
title_color = color("dark_grey"),
title_font_face = "bold",
title_hjust = NULL,
title_position_to_plot = TRUE,
title_font_family = "Carlito",
subtitle_size = 13,
subtitle_font_face = "plain",
subtitle_font_family = "Carlito",
text_size = 12,
subtitle_size = 15,
subtitle_color = color("dark_grey"),
subtitle_font_face = "plain",
subtitle_hjust = NULL,
text_font_family = "Carlito",
text_size = 13,
text_color = color("dark_grey"),
text_font_face = "plain",
panel_background_color = "#FFFFFF",
panel_border = FALSE,
panel_border_color = color("dark_grey"),
legend_position = "bottom",
legend_position = "top",
legend_direction = "horizontal",
legend_justification = "center",
legend_reverse = TRUE,
legend_title_size = 12,
legend_title_size = 13,
legend_title_color = color("dark_grey"),
legend_title_font_face = "plain",
legend_text_size = 12,
legend_text_size = 13,
legend_text_color = color("dark_grey"),
legend_text_font_face = "plain",
facet_title_size = 13,
facet_title_color = color("dark_grey"),
facet_title_font_face = "bold",
facet_title_font_family = "Carlito",
facet_title_position = "bottom",
facet_background_color = color("light_grey"),
axis_x = TRUE,
axis_y = TRUE,
axis_text_x = TRUE,
axis_line_x = TRUE,
axis_ticks_x = TRUE,
axis_line_x = FALSE,
axis_ticks_x = FALSE,
axis_text_y = TRUE,
axis_line_y = TRUE,
axis_ticks_y = TRUE,
axis_text_size = 12,
axis_text_font_family = "Carlito",
axis_text_size = 13,
axis_text_color = color("dark_grey"),
axis_text_font_face = "plain",
axis_title_size = 15,
@ -128,29 +118,24 @@ theme_visualizer_default <- function(
grid_major_color = color("dark_grey"),
grid_major_x_size = 0.1,
grid_major_y_size = 0.1,
grid_minor_x = FALSE,
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_text_size = 10,
caption_text_color = color("dark_grey"),
caption_size = 11,
caption_color = color("dark_grey"),
...) {
# Basic simple theme
# theme <- ggplot2::theme_bw()
theme <- ggplot2::theme_minimal()
theme <- ggplot2::theme(
# Title - design
title = ggtext::element_textbox_simple(
family = title_font_family,
color = title_color,
size = title_size,
face = title_font_face
),
# Text - design
theme <- theme + ggplot2::theme(
# # Text - design
text = ggplot2::element_text(
family = font_family,
family = text_font_family,
color = text_color,
size = text_size,
face = text_font_face
@ -159,58 +144,58 @@ theme_visualizer_default <- function(
legend.position = legend_position,
# Defaut legend to vertical direction
legend.direction = legend_direction,
# Default legend to left justified
legend.justification = legend_justification,
# set panel background color
panel.background = ggplot2::element_rect(
fill = panel_background_color
),
# Remove background for legend key
legend.key = ggplot2::element_blank(),
# Text sizes
axis.text = ggplot2::element_text(
size = axis_text_size,
family = font_family,
family = axis_text_font_family,
face = axis_text_font_face,
color = axis_text_color
),
axis.title = ggplot2::element_text(
size = axis_title_size,
family = font_family,
family = axis_text_font_family,
face = axis_title_font_face,
color = axis_title_color
),
# Wrap title
# # Wrap title
plot.title = ggtext::element_textbox_simple(
hjust = title_hjust,
width = grid::unit(0.8, "npc"),
family = title_font_family,
color = title_color,
size = title_size,
face = title_font_face,
width = grid::unit(0.9, "npc"),
margin = ggplot2::margin(b = 5)
),
plot.subtitle = ggtext::element_textbox_simple(
hjust = title_hjust,
family = subtitle_font_family,
color = text_color,
color = subtitle_color,
size = subtitle_size,
face = subtitle_font_face,
margin = ggplot2::margin(t = 5, b = 5)
),
plot.caption = ggtext::element_textbox_simple(
size = caption_text_size,
family = font_family,
color = caption_text_color
),
legend.title = ggplot2::element_text(
size = legend_title_size,
face = legend_title_font_face,
family = font_family,
color = legend_title_color
),
legend.text = ggplot2::element_text(
size = legend_text_size,
face = legend_text_font_face,
family = font_family,
color = legend_text_color
size = caption_size,
face = caption_font_face,
family = caption_font_family,
color = caption_color,
margin = ggplot2::margin(t = 5)
),
# legend.title = ggplot2::element_text(
# size = legend_title_size,
# face = legend_title_font_face,
# family = font_family,
# color = legend_title_color
# #, vjust = 0.5
# ),
# legend.text = ggplot2::element_text(
# size = legend_text_size,
# face = legend_text_font_face,
# family = font_family,
# color = legend_text_color
# # #, hjust = 0.5
# # ),
axis.text.x = ggplot2::element_text(
angle = axis_text_x_angle,
vjust = axis_text_x_vjust,
@ -377,6 +362,20 @@ theme_visualizer_default <- function(
)
}
# Add facet title text size
theme <- theme + ggplot2::theme(
strip.text = ggplot2::element_text(
size = facet_title_size,
family = facet_title_font_family,
face = facet_title_font_face,
color = facet_title_color
),
strip.background = ggplot2::element_rect(
fill = facet_background_color,
linewidth = 0
)
)
# Other parameters
theme <- theme + ggplot2::theme(...)

63
R/theme_bar.R Normal file
View file

@ -0,0 +1,63 @@
#' Custom Theme for Bar Charts
#'
#' @return A custom theme object.
#'
#' @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) {
# If add_text is TRUE, flip is FALSE
if (!flip && !add_text){
par_axis_line_y <- FALSE
par_axis_ticks_y <- FALSE
par_axis_line_x <- TRUE
par_axis_ticks_x <- TRUE
par_grid_major_y <- TRUE
par_grid_major_x <- FALSE
par_grid_minor_y <- TRUE
par_grid_minor_x <- FALSE
} else if (flip && !add_text){
par_axis_line_y <- TRUE
par_axis_ticks_y <- TRUE
par_axis_line_x <- FALSE
par_axis_ticks_x <- FALSE
par_grid_major_y <- FALSE
par_grid_major_x <- TRUE
par_grid_minor_y <- FALSE
par_grid_minor_x <- TRUE
} else if (!flip && add_text){
par_axis_line_y <- FALSE
par_axis_ticks_y <- FALSE
par_axis_line_x <- TRUE
par_axis_ticks_x <- TRUE
par_grid_major_y <- FALSE
par_grid_major_x <- FALSE
par_grid_minor_y <- FALSE
par_grid_minor_x <- FALSE
} else if (flip && add_text){
par_axis_line_y <- TRUE
par_axis_ticks_y <- TRUE
par_axis_line_x <- FALSE
par_axis_ticks_x <- FALSE
par_grid_major_y <- FALSE
par_grid_major_x <- FALSE
par_grid_minor_y <- FALSE
par_grid_minor_x <- FALSE
}
# Theme
t <- theme_default(
grid_major_y = par_grid_major_y
, axis_line_y = par_axis_line_y
, axis_ticks_y = par_axis_ticks_y
, axis_ticks_x = par_axis_ticks_x
, axis_line_x = par_axis_line_x
, grid_major_x = par_grid_major_x
, grid_minor_y = par_grid_minor_y
, grid_minor_x = par_grid_minor_x
, axis_text_x_angle = axis_text_x_angle
, axis_text_x_vjust = axis_text_x_vjust
, axis_text_x_hjust = axis_text_x_hjust
)
return(t)
}

49
R/theme_visualizer_bar.R Normal file
View file

@ -0,0 +1,49 @@
#' Dynamic Theme for ggplot2
#'
#' A dynamic theme that adjusts axis text styles based on whether the plot is flipped.
#'
#' This function dynamically applies different axis text styles depending on
#' the coordinate system of the plot. If the plot is flipped (e.g., using
#' `coord_flip()`), the x-axis and y-axis text styles are adjusted accordingly.
#'
#' @return A ggproto object that applies a dynamic theme to a ggplot2 plot.
#' @examples
#' library(ggplot2)
#'
#' # Example with a regular plot
#' p <- ggplot(mpg, aes(displ, hwy)) +
#' geom_col()
#'
#' # Add the dynamic theme
#' p + theme_visualizer_bar()
#'
#' # Add the dynamic theme with a flipped coordinate system
#' p + theme_visualizer_bar() + coord_flip()
#'
#' @export
theme_visualizer_bar <- function() {
out <- theme_grey()
class(out) <- c("ThemeVisualizerBar", class(out))
#structure(list(), class = c("ThemeVisualizerBar", "theme", "gg"))
return(out)
}
ggplot_add.theme_visualizer_bar <- function(object, p, object_name) {
# Check if the plot is flipped
is_flipped <- inherits(p$coordinates, "CoordFlip")
if (!is_flipped) {
object <- object +
theme_minimal()
} else {
object <- object +
theme(
panel.grid.major = ggplot2::element_line(color = "blue")
)
}
return(object)
}

View file

@ -27,7 +27,8 @@ bar(
add_text_font_face = "plain",
add_text_threshold_display = 0.05,
add_text_suffix = "\%",
add_text_expand_limit = 1.1
add_text_expand_limit = 1.2,
add_text_round = 1
)
}
\arguments{
@ -76,6 +77,12 @@ bar(
\item{add_text_suffix}{If percent is FALSE, should we add a suffix to the text label?}
\item{add_text_expand_limit}{Default to adding 10% on top of the bar.}
\item{add_text_round}{Round the text label.}
\item{theme_fun}{Whatever theme function. For no custom theme, use theme_fun = NULL.}
\item{scale_impact}{Use the package custom scales for fill and color.}
}
\description{
Simple bar chart

View file

@ -8,9 +8,9 @@
\usage{
palette_gen(palette, type, direction = 1, ...)
palette_gen_categorical(palette = "branding_reach", direction = 1)
palette_gen_categorical(palette = "cat_5_main", direction = 1)
palette_gen_sequential(palette = "seq_7_artichoke", direction = 1, ...)
palette_gen_sequential(palette = "seq_5_main", direction = 1, ...)
}
\arguments{
\item{palette}{Palette name from [palette()].}

View file

@ -5,7 +5,7 @@
\alias{scale_fill_visualizer_discrete}
\alias{scale_fill_visualizer_continuous}
\alias{scale_color_visualizer_continuous}
\title{Scale constructors}
\title{Scale constructors for fill and colors}
\usage{
scale_color_visualizer_discrete(
palette = "cat_5_main",
@ -45,5 +45,5 @@ scale_color_visualizer_continuous(
\item{...}{Additional arguments passed to [ggplot2::discrete_scale()] if discrete or [ggplot2::scale_fill_gradient()] if continuous.}
}
\description{
Scale constructors
This function is based on [palette()]. If palette is NULL, the used palette will be magma from gpplot2's viridis scale constructors.
}

View file

@ -0,0 +1,25 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/scale.R
\name{scale_visualizer_discrete}
\alias{scale_visualizer_discrete}
\title{One scale for all}
\usage{
scale_visualizer_discrete(
palette = "cat_5_main",
direction = 1,
reverse_guide = TRUE,
...
)
}
\arguments{
\item{palette}{Palette name from [palette()].}
\item{direction}{1 or -1; should the order of colors be reversed?}
\item{reverse_guide}{Boolean indicating whether the guide should be reversed.}
\item{...}{Additional arguments passed to [ggplot2::discrete_scale()] if discrete or [ggplot2::scale_fill_gradient()] if continuous.}
}
\description{
This function is based on [palette()]. If palette is NULL, the used palette will be magma from gpplot2's viridis scale constructors.
}

14
man/theme_custom.Rd Normal file
View file

@ -0,0 +1,14 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/theme_bar.R
\name{theme_custom}
\alias{theme_custom}
\title{Custom Theme}
\usage{
theme_custom()
}
\value{
A custom theme object.
}
\description{
Create a custom theme for ggplot2.
}

16
man/theme_visualizer.Rd Normal file
View file

@ -0,0 +1,16 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/theme_visualizer_bar.R
\docType{data}
\name{ThemeVisualizerBar}
\alias{ThemeVisualizerBar}
\title{ggplot2 theme for bar charts with sane defaults}
\format{
An object of class \code{ThemeVisualizerBar} (inherits from \code{ggproto}, \code{gg}) of length 1.
}
\usage{
ThemeVisualizerBar
}
\description{
ggplot2 theme for bar charts with sane defaults
}
\keyword{datasets}

View file

@ -0,0 +1,33 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/theme_visualizer_bar.R
\name{theme_visualizer_bar}
\alias{theme_visualizer_bar}
\title{Dynamic Theme for ggplot2}
\usage{
theme_visualizer_bar()
}
\value{
A ggproto object that applies a dynamic theme to a ggplot2 plot.
}
\description{
A dynamic theme that adjusts axis text styles based on whether the plot is flipped.
}
\details{
This function dynamically applies different axis text styles depending on
the coordinate system of the plot. If the plot is flipped (e.g., using
`coord_flip()`), the x-axis and y-axis text styles are adjusted accordingly.
}
\examples{
library(ggplot2)
# Example with a regular plot
p <- ggplot(mpg, aes(displ, hwy)) +
geom_col()
# Add the dynamic theme
p + theme_visualizer_bar()
# Add the dynamic theme with a flipped coordinate system
p + theme_visualizer_bar() + coord_flip()
}

View file

@ -1,28 +1,31 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/theme.R
\name{theme_reach}
\alias{theme_reach}
\title{ggplot2 theme wrapper with REACH fonts and colors}
\name{theme_visualizer_default}
\alias{theme_visualizer_default}
\title{ggplot2 theme wrapper with fonts and colors}
\usage{
theme_reach(
theme_visualizer_default(
font_family = "Carlito",
title_size = 16,
title_size = 14,
title_color = color("dark_grey"),
title_font_face = "bold",
title_hjust = NULL,
title_position_to_plot = TRUE,
title_font_family = "Carlito",
text_size = 14,
subtitle_size = 13,
subtitle_font_face = "plain",
subtitle_font_family = "Carlito",
text_size = 12,
text_color = color("dark_grey"),
text_font_face = "plain",
panel_background_color = "#FFFFFF",
panel_border = FALSE,
panel_border_color = color("dark_grey"),
legend_position = "bottom",
legend_position = "top",
legend_direction = "horizontal",
legend_justification = "left",
legend_justification = "center",
legend_reverse = TRUE,
legend_title_size = 14,
legend_title_size = 12,
legend_title_color = color("dark_grey"),
legend_title_font_face = "plain",
legend_text_size = 12,
@ -36,7 +39,7 @@ theme_reach(
axis_text_y = TRUE,
axis_line_y = TRUE,
axis_ticks_y = TRUE,
axis_text_size = 14,
axis_text_size = 12,
axis_text_color = color("dark_grey"),
axis_text_font_face = "plain",
axis_title_size = 15,
@ -47,15 +50,17 @@ theme_reach(
axis_text_x_hjust = 0.5,
grid_major_x = TRUE,
grid_major_y = FALSE,
grid_major_color = color("light_grey"),
grid_major_x_size = 0.01,
grid_major_y_size = 0.01,
grid_major_color = color("dark_grey"),
grid_major_x_size = 0.1,
grid_major_y_size = 0.1,
grid_minor_x = FALSE,
grid_minor_y = FALSE,
grid_minor_color = color("light_grey"),
grid_minor_x_size = 0.005,
grid_minor_y_size = 0.005,
grid_minor_color = color("dark_grey"),
grid_minor_x_size = 0.05,
grid_minor_y_size = 0.05,
caption_position_to_plot = TRUE,
caption_text_size = 10,
caption_text_color = color("dark_grey"),
...
)
}
@ -163,6 +168,8 @@ theme_reach(
\item{caption_position_to_plot}{TRUE or FALSE. Positioning to plot or to panel?}
\item{...}{Additional arguments passed to [ggplot2::theme()].}
\item{p}{A ggplot2 object.}
}
\description{
Give some reach colors and fonts to a ggplot.

97
plot.svg Normal file
View file

@ -0,0 +1,97 @@
<?xml version='1.0' encoding='UTF-8' ?>
<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' class='svglite' width='717.08pt' height='586.70pt' viewBox='0 0 717.08 586.70'>
<defs>
<style type='text/css'><![CDATA[
.svglite line, .svglite polyline, .svglite polygon, .svglite path, .svglite rect, .svglite circle {
fill: none;
stroke: #000000;
stroke-linecap: round;
stroke-linejoin: round;
stroke-miterlimit: 10.00;
}
.svglite text {
white-space: pre;
}
]]></style>
</defs>
<rect width='100%' height='100%' style='stroke: none; fill: none;'/>
<defs>
<clipPath id='cpMC4wMHw3MTcuMDh8MC4wMHw1ODYuNzA='>
<rect x='0.00' y='0.00' width='717.08' height='586.70' />
</clipPath>
</defs>
<g clip-path='url(#cpMC4wMHw3MTcuMDh8MC4wMHw1ODYuNzA=)'>
</g>
<defs>
<clipPath id='cpNjIuOTV8NzExLjYwfDYxLjExfDU2OC40OQ=='>
<rect x='62.95' y='61.11' width='648.65' height='507.37' />
</clipPath>
</defs>
<g clip-path='url(#cpNjIuOTV8NzExLjYwfDYxLjExfDU2OC40OQ==)'>
<polyline points='62.95,568.49 62.95,61.11 ' style='stroke-width: 0.21; stroke: #464647; stroke-linecap: butt;' />
<polyline points='301.22,568.49 301.22,61.11 ' style='stroke-width: 0.21; stroke: #464647; stroke-linecap: butt;' />
<polyline points='539.49,568.49 539.49,61.11 ' style='stroke-width: 0.21; stroke: #464647; stroke-linecap: butt;' />
<rect x='62.95' y='544.63' width='32.17' height='17.58' style='stroke-width: 1.07; stroke: #F8766D; stroke-linecap: butt; stroke-linejoin: miter; fill: #F8766D;' />
<rect x='62.95' y='519.51' width='39.87' height='17.58' style='stroke-width: 1.07; stroke: #F8766D; stroke-linecap: butt; stroke-linejoin: miter; fill: #F8766D;' />
<rect x='62.95' y='494.39' width='41.28' height='17.58' style='stroke-width: 1.07; stroke: #F8766D; stroke-linecap: butt; stroke-linejoin: miter; fill: #F8766D;' />
<rect x='62.95' y='469.27' width='82.73' height='17.58' style='stroke-width: 1.07; stroke: #F8766D; stroke-linecap: butt; stroke-linejoin: miter; fill: #F8766D;' />
<rect x='62.95' y='444.16' width='56.42' height='17.58' style='stroke-width: 1.07; stroke: #7CAE00; stroke-linecap: butt; stroke-linejoin: miter; fill: #7CAE00;' />
<rect x='62.95' y='419.04' width='95.80' height='17.58' style='stroke-width: 1.07; stroke: #7CAE00; stroke-linecap: butt; stroke-linejoin: miter; fill: #7CAE00;' />
<rect x='62.95' y='393.92' width='151.11' height='17.58' style='stroke-width: 1.07; stroke: #7CAE00; stroke-linecap: butt; stroke-linejoin: miter; fill: #7CAE00;' />
<rect x='62.95' y='368.80' width='31.42' height='17.58' style='stroke-width: 1.07; stroke: #00BFC4; stroke-linecap: butt; stroke-linejoin: miter; fill: #00BFC4;' />
<rect x='62.95' y='343.69' width='36.04' height='17.58' style='stroke-width: 1.07; stroke: #00BFC4; stroke-linecap: butt; stroke-linejoin: miter; fill: #00BFC4;' />
<rect x='62.95' y='318.57' width='36.73' height='17.58' style='stroke-width: 1.07; stroke: #00BFC4; stroke-linecap: butt; stroke-linejoin: miter; fill: #00BFC4;' />
<rect x='62.95' y='293.45' width='43.07' height='17.58' style='stroke-width: 1.07; stroke: #00BFC4; stroke-linecap: butt; stroke-linejoin: miter; fill: #00BFC4;' />
<rect x='62.95' y='268.33' width='47.02' height='17.58' style='stroke-width: 1.07; stroke: #00BFC4; stroke-linecap: butt; stroke-linejoin: miter; fill: #00BFC4;' />
<rect x='62.95' y='243.22' width='60.66' height='17.58' style='stroke-width: 1.07; stroke: #00BFC4; stroke-linecap: butt; stroke-linejoin: miter; fill: #00BFC4;' />
<rect x='62.95' y='218.10' width='72.68' height='17.58' style='stroke-width: 1.07; stroke: #00BFC4; stroke-linecap: butt; stroke-linejoin: miter; fill: #00BFC4;' />
<rect x='62.95' y='192.98' width='88.09' height='17.58' style='stroke-width: 1.07; stroke: #00BFC4; stroke-linecap: butt; stroke-linejoin: miter; fill: #00BFC4;' />
<rect x='62.95' y='167.86' width='113.25' height='17.58' style='stroke-width: 1.07; stroke: #00BFC4; stroke-linecap: butt; stroke-linejoin: miter; fill: #00BFC4;' />
<rect x='62.95' y='142.75' width='589.32' height='17.58' style='stroke-width: 1.07; stroke: #00BFC4; stroke-linecap: butt; stroke-linejoin: miter; fill: #00BFC4;' />
<rect x='62.95' y='117.63' width='648.65' height='17.58' style='stroke-width: 1.07; stroke: #00BFC4; stroke-linecap: butt; stroke-linejoin: miter; fill: #00BFC4;' />
<rect x='62.95' y='92.51' width='38.37' height='17.58' style='stroke-width: 1.07; stroke: #C77CFF; stroke-linecap: butt; stroke-linejoin: miter; fill: #C77CFF;' />
<rect x='62.95' y='67.39' width='68.38' height='17.58' style='stroke-width: 1.07; stroke: #C77CFF; stroke-linecap: butt; stroke-linejoin: miter; fill: #C77CFF;' />
</g>
<g clip-path='url(#cpMC4wMHw3MTcuMDh8MC4wMHw1ODYuNzA=)'>
<polyline points='62.95,568.49 62.95,61.11 ' style='stroke-width: 1.07; stroke: #464647; stroke-linecap: butt;' />
<text x='58.02' y='556.49' text-anchor='end' style='font-size: 9.60px;fill: #4D4D4D; font-family: "Carlito";' textLength='16.22px' lengthAdjust='spacingAndGlyphs'>DRC</text>
<text x='58.02' y='531.38' text-anchor='end' style='font-size: 9.60px;fill: #4D4D4D; font-family: "Carlito";' textLength='21.79px' lengthAdjust='spacingAndGlyphs'>Egypt</text>
<text x='58.02' y='506.26' text-anchor='end' style='font-size: 9.60px;fill: #4D4D4D; font-family: "Carlito";' textLength='32.03px' lengthAdjust='spacingAndGlyphs'>Ethiopia</text>
<text x='58.02' y='481.14' text-anchor='end' style='font-size: 9.60px;fill: #4D4D4D; font-family: "Carlito";' textLength='27.82px' lengthAdjust='spacingAndGlyphs'>Nigeria</text>
<text x='58.02' y='456.02' text-anchor='end' style='font-size: 9.60px;fill: #4D4D4D; font-family: "Carlito";' textLength='28.44px' lengthAdjust='spacingAndGlyphs'>Mexico</text>
<text x='58.02' y='430.91' text-anchor='end' style='font-size: 9.60px;fill: #4D4D4D; font-family: "Carlito";' textLength='21.35px' lengthAdjust='spacingAndGlyphs'>Brazil</text>
<text x='58.02' y='405.79' text-anchor='end' style='font-size: 9.60px;fill: #4D4D4D; font-family: "Carlito";' textLength='52.54px' lengthAdjust='spacingAndGlyphs'>United States</text>
<text x='58.02' y='380.67' text-anchor='end' style='font-size: 9.60px;fill: #4D4D4D; font-family: "Carlito";' textLength='33.39px' lengthAdjust='spacingAndGlyphs'>Thailand</text>
<text x='58.02' y='355.55' text-anchor='end' style='font-size: 9.60px;fill: #4D4D4D; font-family: "Carlito";' textLength='26.53px' lengthAdjust='spacingAndGlyphs'>Turkey</text>
<text x='58.02' y='330.44' text-anchor='end' style='font-size: 9.60px;fill: #4D4D4D; font-family: "Carlito";' textLength='15.39px' lengthAdjust='spacingAndGlyphs'>Iran</text>
<text x='58.02' y='305.32' text-anchor='end' style='font-size: 9.60px;fill: #4D4D4D; font-family: "Carlito";' textLength='32.93px' lengthAdjust='spacingAndGlyphs'>Vietnam</text>
<text x='58.02' y='280.20' text-anchor='end' style='font-size: 9.60px;fill: #4D4D4D; font-family: "Carlito";' textLength='42.45px' lengthAdjust='spacingAndGlyphs'>Philippines</text>
<text x='58.02' y='255.08' text-anchor='end' style='font-size: 9.60px;fill: #4D4D4D; font-family: "Carlito";' textLength='22.33px' lengthAdjust='spacingAndGlyphs'>Japan</text>
<text x='58.02' y='229.97' text-anchor='end' style='font-size: 9.60px;fill: #4D4D4D; font-family: "Carlito";' textLength='44.77px' lengthAdjust='spacingAndGlyphs'>Bangladesh</text>
<text x='58.02' y='204.85' text-anchor='end' style='font-size: 9.60px;fill: #4D4D4D; font-family: "Carlito";' textLength='32.71px' lengthAdjust='spacingAndGlyphs'>Pakistan</text>
<text x='58.02' y='179.73' text-anchor='end' style='font-size: 9.60px;fill: #4D4D4D; font-family: "Carlito";' textLength='37.92px' lengthAdjust='spacingAndGlyphs'>Indonesia</text>
<text x='58.02' y='154.61' text-anchor='end' style='font-size: 9.60px;fill: #4D4D4D; font-family: "Carlito";' textLength='19.30px' lengthAdjust='spacingAndGlyphs'>India</text>
<text x='58.02' y='129.50' text-anchor='end' style='font-size: 9.60px;fill: #4D4D4D; font-family: "Carlito";' textLength='21.99px' lengthAdjust='spacingAndGlyphs'>China</text>
<text x='58.02' y='104.38' text-anchor='end' style='font-size: 9.60px;fill: #4D4D4D; font-family: "Carlito";' textLength='35.80px' lengthAdjust='spacingAndGlyphs'>Germany</text>
<text x='58.02' y='79.26' text-anchor='end' style='font-size: 9.60px;fill: #4D4D4D; font-family: "Carlito";' textLength='24.54px' lengthAdjust='spacingAndGlyphs'>Russia</text>
<polyline points='62.95,568.49 711.60,568.49 ' style='stroke-width: 1.07; stroke: #464647; stroke-linecap: butt;' />
<text x='62.95' y='579.57' text-anchor='middle' style='font-size: 9.60px;fill: #4D4D4D; font-family: "Carlito";' textLength='4.86px' lengthAdjust='spacingAndGlyphs'>0</text>
<text x='301.22' y='579.57' text-anchor='middle' style='font-size: 9.60px;fill: #4D4D4D; font-family: "Carlito";' textLength='14.58px' lengthAdjust='spacingAndGlyphs'>500</text>
<text x='539.49' y='579.57' text-anchor='middle' style='font-size: 9.60px;fill: #4D4D4D; font-family: "Carlito";' textLength='19.44px' lengthAdjust='spacingAndGlyphs'>1000</text>
<text x='262.72' y='39.89' style='font-size: 12.00px;fill: #464647; font-family: "Carlito";' textLength='33.53px' lengthAdjust='spacingAndGlyphs'>Region</text>
<rect x='302.44' y='28.10' width='15.86' height='15.86' style='stroke-width: 1.07; stroke: #F8766D; stroke-linecap: butt; stroke-linejoin: miter; fill: #F8766D;' />
<rect x='353.35' y='28.10' width='15.86' height='15.86' style='stroke-width: 1.07; stroke: #7CAE00; stroke-linecap: butt; stroke-linejoin: miter; fill: #7CAE00;' />
<rect x='417.52' y='28.10' width='15.86' height='15.86' style='stroke-width: 1.07; stroke: #00BFC4; stroke-linecap: butt; stroke-linejoin: miter; fill: #00BFC4;' />
<rect x='461.85' y='28.10' width='15.86' height='15.86' style='stroke-width: 1.07; stroke: #C77CFF; stroke-linecap: butt; stroke-linejoin: miter; fill: #C77CFF;' />
<text x='324.49' y='39.11' style='font-size: 9.60px;fill: #464647; font-family: "Carlito";' textLength='22.67px' lengthAdjust='spacingAndGlyphs'>Africa</text>
<text x='375.40' y='39.11' style='font-size: 9.60px;fill: #464647; font-family: "Carlito";' textLength='35.93px' lengthAdjust='spacingAndGlyphs'>Americas</text>
<text x='439.57' y='39.11' style='font-size: 9.60px;fill: #464647; font-family: "Carlito";' textLength='16.10px' lengthAdjust='spacingAndGlyphs'>Asia</text>
<text x='483.90' y='39.11' style='font-size: 9.60px;fill: #464647; font-family: "Carlito";' textLength='27.93px' lengthAdjust='spacingAndGlyphs'>Europe</text>
<text x='5.48' y='14.56' style='font-size: 14.00px; font-weight: bold;fill: #464647; font-family: "Carlito";' textLength='63.66px' lengthAdjust='spacingAndGlyphs'>Population</text>
<text x='72.30' y='14.56' style='font-size: 14.00px; font-weight: bold;fill: #464647; font-family: "Carlito";' textLength='11.95px' lengthAdjust='spacingAndGlyphs'>of</text>
<text x='87.42' y='14.56' style='font-size: 14.00px; font-weight: bold;fill: #464647; font-family: "Carlito";' textLength='37.74px' lengthAdjust='spacingAndGlyphs'>Global</text>
<text x='128.31' y='14.56' style='font-size: 14.00px; font-weight: bold;fill: #464647; font-family: "Carlito";' textLength='45.62px' lengthAdjust='spacingAndGlyphs'>Regions</text>
<text x='177.09' y='14.56' style='font-size: 14.00px; font-weight: bold;fill: #464647; font-family: "Carlito";' textLength='10.94px' lengthAdjust='spacingAndGlyphs'>in</text>
<text x='191.20' y='14.56' style='font-size: 14.00px; font-weight: bold;fill: #464647; font-family: "Carlito";' textLength='41.00px' lengthAdjust='spacingAndGlyphs'>Million</text>
</g>
</svg>

After

Width:  |  Height:  |  Size: 11 KiB

97
test-example.R Normal file
View file

@ -0,0 +1,97 @@
# dat <- data.frame(
# x = c(15, 34, 59, 21, 33, 66),
# y = c("Admin A", "Admin B", "Admin C", "Admin C", "Admin B", "Admin A"),
# group = c("Displaced", "Non displaced", "Non displaced", "Displaced", "Displaced", "Non displaced")
# )
library(visualizeR)
# dat |>
# bar(
# x = "y",
# y = "x",
# #group = "group",
# group_title = "Displacement Status",
# flip = T,
# add_text = T,
# title = "In Admin A and C, Non-Displaced Persons Face Greater WASH Challenges Than Their Displaced Counterparts",
# subtitle = "% of households not accessing WASH services by admin 1 and displacement status",
# caption = "Source: FAO 2022. No message is a real one. Fake data are used in this example. As a cautiom, no decision should be made based on this plot.",
# ) +
# theme_bar(flip = T, add_text = T) +
# scale_color_visualizer_discrete() +
# scale_fill_visualizer_discrete()
library(rio)
dat <- import("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/11_SevCatOneNumNestedOneObsPerGroup.csv")
library(dplyr)
library(ggplot2)
library(data.table)
# dat as a data.table if it4s not
if (!checkmate::test_data_table(dat)) {
rlang::warn("Converting dat to data.table.")
data.table::setDT(dat)
}
# in all character columns, tranform empty string to NA
vars_chr <- colnames(dat)[sapply(dat, is.character)]
dat[, (vars_chr) := lapply(.SD, function(x) fifelse(x == "", NA_character_, x)), .SDcols = vars_chr]
# in value, if -1 replace with NA
dat[, value := fifelse(value == -1, NA_real_, value)]
# remove lines where value is NA (in place)
dat <- dat[!is.na(value), ]
dat
# arrange(value) |>
# group_by(region) |>
# mutate(key = forcats::fct_reorder(key, value)) |>
df = dat |> arrange(value) |> tail(20) |> mutate(
value = value/1000000,
key = ifelse(key == "Democratic Republic of the Congo", "DRC", key))
bar(
df,
x = "key",
y = "value",
group = "region",
group_title = "Region",
facet = "region",
order = "grouped_y",
title = "Population of Global Regions in Million"
) + scale_fill_visualizer_discrete(title_position = "top") + scale_color_visualizer_discrete()
hbar(
df,
x = "key",
y = "value",
group = "region",
group_title = "Region",
facet = "region",
order = "none",
x_rm_na = T,
y_rm_na = T,
group_rm_na = T,
title = "Population of Global Regions (in Million)"
) + scale_fill_visualizer_discrete(title_position = "left") + scale_color_visualizer_discrete()
ggplot2::ggsave(
"plot.svg",
gg
)
# ggplot2::theme(
# #legend.direction = "horizontal",
# legend.position = "top"
# )
#
#theme_bar(flip = F, axis_text_x_angle = 45) +
#scale_color_visualizer_discrete() +
#scale_fill_visualizer_discrete()

22
test.R Normal file
View file

@ -0,0 +1,22 @@
library(visualizeR)
library(ggplot2)
# Example usagea
# Sample data
data <- data.frame(
category = c("A", "B", "C", "D"),
value = c(3, 7, 9, 5)
)
library(visualizeR)
library(ggplot2)
# Regular bar plot
p1 <-
bar(
df = data,
x = "category",
y = "value",
flip = F
) +
theme_bar(flip = F)
p1

View file

@ -1,4 +1,5 @@
Version: 1.0
ProjectId: e1665596-bf01-400a-b4a1-2f46436c0b23
RestoreWorkspace: Default
SaveWorkspace: Default