diff --git a/DESCRIPTION b/DESCRIPTION
index 101827c..14196d2 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -20,7 +20,7 @@ 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:
@@ -28,7 +28,8 @@ Imports:
scales,
cli,
rlang,
- dplyr
+ dplyr,
+ purrr
Suggests:
ggtext,
knitr,
@@ -36,7 +37,6 @@ Suggests:
tibble,
tidyr,
glue,
- purrr,
stringr,
testthat (>= 2.1.0),
plotly,
@@ -45,6 +45,7 @@ Suggests:
ragg (>= 1.2.6),
gapminder,
diffviewer,
- vdiffr
+ vdiffr,
+ withr
VignetteBuilder: knitr
Roxygen: list(markdown = TRUE)
diff --git a/NEWS.md b/NEWS.md
index dedc8cc..fb49b85 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -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.
diff --git a/R/use_afcharts.R b/R/use_afcharts.R
index 3fb6410..096bf94 100644
--- a/R/use_afcharts.R
+++ b/R/use_afcharts.R
@@ -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.
@@ -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)
+
+ }
+
+
+ }
}
diff --git a/man/use_afcharts.Rd b/man/use_afcharts.Rd
index ec7661b..80aaab5 100644
--- a/man/use_afcharts.Rd
+++ b/man/use_afcharts.Rd
@@ -4,13 +4,20 @@
\alias{use_afcharts}
\title{Use afcharts defaults.}
\usage{
-use_afcharts(default_colour = afcharts::af_colour_values["dark-blue"], ...)
+use_afcharts(
+ default_colour = afcharts::af_colour_values["dark-blue"],
+ ...,
+ reset = FALSE
+)
}
\arguments{
\item{default_colour}{Default colour/fill for geoms. Default value is 'blue'
from \code{af_colour_values}.}
\item{...}{Arguments passed to \code{theme_af()}.}
+
+\item{reset}{Logical. Turn off use_afcharts. This aims to reset the default
+chart setting to their status when \code{use_afcharts} was first called.}
}
\value{
NULL. Function is used for side effects of setting ggplot2 plot
diff --git a/tests/testthat/_snaps/chart-output/use-afcharts-1.svg b/tests/testthat/_snaps/chart-output/use-afcharts-1.svg
new file mode 100644
index 0000000..f7c0146
--- /dev/null
+++ b/tests/testthat/_snaps/chart-output/use-afcharts-1.svg
@@ -0,0 +1,68 @@
+
+
diff --git a/tests/testthat/_snaps/chart-output/use-afcharts-2.svg b/tests/testthat/_snaps/chart-output/use-afcharts-2.svg
new file mode 100644
index 0000000..d69cfd1
--- /dev/null
+++ b/tests/testthat/_snaps/chart-output/use-afcharts-2.svg
@@ -0,0 +1,60 @@
+
+
diff --git a/tests/testthat/_snaps/chart-output/use-afcharts-3.svg b/tests/testthat/_snaps/chart-output/use-afcharts-3.svg
new file mode 100644
index 0000000..c3aa37f
--- /dev/null
+++ b/tests/testthat/_snaps/chart-output/use-afcharts-3.svg
@@ -0,0 +1,79 @@
+
+
diff --git a/tests/testthat/_snaps/chart-output/use-afcharts-4.svg b/tests/testthat/_snaps/chart-output/use-afcharts-4.svg
new file mode 100644
index 0000000..a78f550
--- /dev/null
+++ b/tests/testthat/_snaps/chart-output/use-afcharts-4.svg
@@ -0,0 +1,68 @@
+
+
diff --git a/tests/testthat/test-chart-output.R b/tests/testthat/test-chart-output.R
index 35db7e5..55e7cef 100644
--- a/tests/testthat/test-chart-output.R
+++ b/tests/testthat/test-chart-output.R
@@ -77,16 +77,68 @@ test_that("scale_colour_continuous_af works", {
})
-# Test commented out until there is an way to turn off use_afcharts
-
-# test_that("use_afcharts works", {
-#
-# use_afcharts()
-#
-# d <- subset(mpg, manufacturer == "ford")
-#
-# plot <- ggplot2::ggplot(d, ggplot2::aes(x = model, fill = class, colour = class)) +
-# ggplot2::geom_bar()
-#
-# expect_match_plot("use_afcharts", plot)
-# })
+# Test use_afcharts
+
+test_that("use_afcharts works", {
+
+ # Set default theme, geom and colour scale
+
+ # Set theme
+
+ old_theme <- ggplot2::theme_set(ggplot2::theme_dark())
+
+ withr::defer(ggplot2::theme_set(old_theme))
+
+
+ # Set geom
+
+ old_geom <- ggplot2::update_geom_defaults(
+ geom = "bar",
+ new = list(fill = "red")
+ )
+
+ withr::defer(
+ ggplot2::update_geom_defaults(
+ geom = "bar",
+ new = old_geom
+ )
+ )
+
+
+ # Set scale
+
+ old_options <- options(ggplot2.discrete.fill = ggplot2::scale_fill_viridis_d)
+ withr::defer(options(old_options))
+
+
+ # Turn on use_afcharts - should ignore the above defaults
+
+ use_afcharts()
+
+ d <- subset(ggplot2::mpg, manufacturer == "ford")
+
+ plot1 <- ggplot2::ggplot(d, ggplot2::aes(x = model, fill = class)) +
+ ggplot2::geom_bar()
+
+ plot2 <- ggplot2::ggplot(d, ggplot2::aes(x = model)) +
+ ggplot2::geom_bar()
+
+ expect_match_plot("use_afcharts_1", plot1)
+ expect_match_plot("use_afcharts_2", plot2)
+
+
+ # Turn use_afcharts off and check default plot settings revert to what they
+ # were prior to using use_afcharts
+
+ use_afcharts(reset = TRUE)
+
+ plot3 <- ggplot2::ggplot(d, ggplot2::aes(x = model, fill = class)) +
+ ggplot2::geom_bar()
+
+ plot4 <- ggplot2::ggplot(d, ggplot2::aes(x = model)) +
+ ggplot2::geom_bar()
+
+ expect_match_plot("use_afcharts_3", plot3)
+ expect_match_plot("use_afcharts_4", plot4)
+
+})