Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ URL:
BugReports: https://github.com/best-practice-and-impact/afcharts/issues
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.3.2
RoxygenNote: 7.3.3
Depends:
R (>= 3.5)
Imports:
ggplot2,
scales,
cli,
rlang,
dplyr
dplyr,
purrr
Suggests:
ggtext,
knitr,
rmarkdown,
tibble,
tidyr,
glue,
purrr,
stringr,
testthat (>= 2.1.0),
plotly,
Expand All @@ -45,6 +45,7 @@ Suggests:
ragg (>= 1.2.6),
gapminder,
diffviewer,
vdiffr
vdiffr,
withr
VignetteBuilder: knitr
Roxygen: list(markdown = TRUE)
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# afcharts (development version)

- `use_afcharts` gains a `reset` argument to turn off analysis function styling of charts.

- Set the `main` colour palettes to `categorical`. The Scale_*_discrete_af functions now use the 6 colour cateogrical palette by default, rather than the 4 colour.

- Added `af_dark_blue`, `af_orange` and `af_grey` to give easier access to the hex codes of these colours.
Expand Down
247 changes: 166 additions & 81 deletions R/use_afcharts.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#' @param default_colour Default colour/fill for geoms. Default value is 'blue'
#' from `af_colour_values`.
#' @param ... Arguments passed to `theme_af()`.
#' @param reset Logical. Turn off use_afcharts. This aims to reset the default
#' chart setting to their status when `use_afcharts` was first called.
#'
#' @returns NULL. Function is used for side effects of setting ggplot2 plot
#' theme, colour palette and geom aesthetic defaults.
Expand All @@ -26,98 +28,181 @@
#' @export


use_afcharts <- function(
default_colour = afcharts::af_colour_values["dark-blue"],
...) {
use_afcharts <- function(default_colour =
afcharts::af_colour_values["dark-blue"],
...,
reset = FALSE) {

# Use afcharts theme ----
if (!rlang::is_bool(reset)) {
cli::cli_abort("{.arg reset} must be {.code TRUE} or {.code FALSE}")
}

ggplot2::theme_set(theme_af(...))
if (isFALSE(reset)) {

cli::cli_alert_info("Default ggplot2 theme set to `theme_af`.")
# Use afcharts theme ----

old_theme <- ggplot2::theme_set(theme_af(...))

# Use use_afcharts colour palette ----
if (!isTRUE(getOption("afcharts.use_afcharts"))) {
options("afcharts.old.theme" = old_theme)
}

cli::cli_alert_info("Default ggplot2 theme set to `theme_af`.")


# Use use_afcharts colour palette ----

old_scales <- options(
ggplot2.continuous.fill = scale_fill_continuous_af,
ggplot2.continuous.colour = scale_colour_continuous_af,
ggplot2.discrete.fill = scale_fill_discrete_af,
ggplot2.discrete.colour = scale_colour_discrete_af
)

options(ggplot2.continuous.fill = scale_fill_continuous_af,
ggplot2.continuous.colour = scale_colour_continuous_af,
ggplot2.discrete.fill = scale_fill_discrete_af,
ggplot2.discrete.colour = scale_colour_discrete_af)
if (!isTRUE(getOption("afcharts.use_afcharts"))) {
options("afcharts.old.scales" = old_scales)
}

cli::cli_alert_info("Default colours set.")

cli::cli_alert_info("Default colour palettes set.")

# Set default geom characteristics ----

# Get default base sizes used in theme
default <- formals(theme_af)
# Set default geom characteristics ----

# Update default values with those passed to use_afcharts
new_values <- c(...)
for (i in seq_along(new_values)) {
default <- replace(default,
which(names(default) == names(new_values)[i]),
new_values[i])
}
# Get default base sizes used in theme
default <- formals(theme_af)

# Update default values with those passed to use_afcharts
new_values <- c(...)
for (i in seq_along(new_values)) {
default <- replace(default,
which(names(default) == names(new_values)[i]),
new_values[i])
}

# Evaluate base_size values for use in geom defaults
base_size <- eval(default$base_size)
base_line_size <- eval(default$base_line_size)

# Lines
old_line <- ggplot2::update_geom_defaults(
geom = "line",
new = list(colour = default_colour,
linewidth = base_line_size)
)

old_hline <- ggplot2::update_geom_defaults(
geom = "hline",
new = list(colour = default_colour,
linewidth = base_line_size)
)

old_vline <- ggplot2::update_geom_defaults(
geom = "vline",
new = list(colour = default_colour,
linewidth = base_line_size)
)

# Col
old_col <- ggplot2::update_geom_defaults(
geom = "col",
new = list(fill = default_colour)
)

# Bar
old_bar <- ggplot2::update_geom_defaults(
geom = "bar",
new = list(fill = default_colour)
)

# Evaluate base_size values for use in geom defaults
base_size <- eval(default$base_size)
base_line_size <- eval(default$base_line_size)

# Lines
ggplot2::update_geom_defaults(
geom = "line",
new = list(colour = default_colour,
linewidth = base_line_size)
)

ggplot2::update_geom_defaults(
geom = "hline",
new = list(colour = default_colour,
linewidth = base_line_size)
)

ggplot2::update_geom_defaults(
geom = "vline",
new = list(colour = default_colour,
linewidth = base_line_size)
)

# Col
ggplot2::update_geom_defaults(
geom = "col",
new = list(fill = default_colour)
)

# Bar
ggplot2::update_geom_defaults(
geom = "bar",
new = list(fill = default_colour)
)

# Text
ggplot2::update_geom_defaults(
geom = "text",
new = list(colour = "black",
size = base_size / ggplot2::.pt)
)

ggplot2::update_geom_defaults(
geom = "label",
new = list(colour = "black",
size = base_size / ggplot2::.pt)
)

# Point
ggplot2::update_geom_defaults(
geom = "point",
new = list(colour = default_colour,
fill = default_colour,
size = base_size / 8)
)

cli::cli_alert_info("Default geom aesthetics set.")

invisible(NULL)
# Text
old_text <- ggplot2::update_geom_defaults(
geom = "text",
new = list(colour = "black",
size = base_size / ggplot2::.pt)
)

old_label <- ggplot2::update_geom_defaults(
geom = "label",
new = list(colour = "black",
size = base_size / ggplot2::.pt)
)

# Point
old_point <- ggplot2::update_geom_defaults(
geom = "point",
new = list(colour = default_colour,
fill = default_colour,
size = base_size / 8)
)


if (!isTRUE(getOption("afcharts.use_afcharts"))) {
options(
afcharts.old.geoms = list(
line = old_line,
hline = old_hline,
vline = old_vline,
col = old_col,
bar = old_bar,
text = old_text,
label = old_label,
point = old_point
)
)
}

cli::cli_alert_info("Default geom aesthetics set.")

# Record that using use_af ----

options("afcharts.use_afcharts" = TRUE)

invisible(NULL)

} else {

if (isTRUE(getOption("afcharts.use_afcharts"))) {

# Reset theme
old_theme <- getOption("afcharts.old.theme")

if (!is.null(old_theme)) {
ggplot2::theme_set(old_theme)
cli::cli_alert_info("Reverting theme.")
options("afcharts.old.theme" = NULL)
}

# Reset scales
old_scales <- getOption("afcharts.old.scales")

if (!is.null(old_scales)) {
options(old_scales)
cli::cli_alert_info("Reverting colour palettes.")
options("afcharts.old.scales" = NULL)
}

# Reset geoms

old_geoms <- getOption("afcharts.old.geoms")

if (!is.null(old_geoms)) {
purrr::walk2(
names(old_geoms), old_geoms,
\(geom, default) {
ggplot2::update_geom_defaults(geom, default)
}

)
cli::cli_alert_info("Reverting geom aesthetics.")
options("afcharts.old.geoms" = NULL)
}

# Turn off use_afcharts
options("afcharts.use_afcharts" = NULL)

}


}
}
9 changes: 8 additions & 1 deletion man/use_afcharts.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading