Skip to content

Commit 4f09792

Browse files
Update use_afcharts
1 parent 6a2cead commit 4f09792

10 files changed

Lines changed: 179 additions & 52 deletions

File tree

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ Imports:
2929
cli,
3030
rlang,
3131
dplyr,
32-
purrr
32+
purrr,
33+
stats
3334
Suggests:
3435
ggtext,
3536
knitr,

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ export(scale_fill_discrete_af)
99
export(theme_af)
1010
export(use_afcharts)
1111
importFrom(rlang,.data)
12+
importFrom(stats,setNames)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

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

7+
- `theme_af` now uses theme options set by earlier calls of `use_afcharts` by default. #51
8+
79
- 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.
810

911
- Added `af_dark_blue`, `af_orange` and `af_grey` to give easier access to the hex codes of these colours.

R/afcharts-package.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33

44
## usethis namespace: start
55
#' @importFrom rlang .data
6+
#' @importFrom stats setNames
67
## usethis namespace: end
78
NULL

R/theme_af.R

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
#' @param axis_text,axis_title 'x', 'y', 'xy' or 'none' to determine whether axis text and/or axis titles should be displayed.
1515
#' Text defaults to 'xy', as does title. Note that axis text refers to the 'labels' under the tick marks.
1616
#'
17-
#' @param legend_title Set to 'none' to suppress legend titles.
17+
#' @param legend_title Set to 'none' to suppress legend titles. Defaults to
18+
#' 'show'.
1819
#'
1920
#' @returns ggplot2 plot theme
2021
#'
@@ -29,19 +30,18 @@
2930
#' @export
3031

3132

32-
theme_af <- function(base_size = getOption("afcharts.base_size", 14),
33-
base_line_size = NULL,
34-
base_rect_size = NULL,
35-
grid = getOption("afcharts.grid", "y"),
36-
axis = getOption("afcharts.axis", "x"),
37-
ticks = getOption("afcharts.ticks", "xy"),
38-
legend = getOption("afcharts.legend", "right"),
39-
axis_text = getOption("afcharts.axis_text", "xy"),
40-
axis_title = getOption("afcharts.axis_title", "xy"),
41-
legend_title = getOption("afcharts.axis_title", "show")) {
42-
43-
if (is.null(base_line_size)) base_line_size <- getOption("afcharts.base_line_size", base_size / 24)
44-
if (is.null(base_rect_size)) base_rect_size <- getOption("afcharts.base_rect_size", base_size / 24)
33+
theme_af <- function(
34+
base_size = getOption("afcharts.base_size", 14),
35+
base_line_size = getOption("afcharts.base_line_size", base_size / 24),
36+
base_rect_size = getOption("afcharts.base_rect_size", base_size / 24),
37+
grid = getOption("afcharts.grid", "y"),
38+
axis = getOption("afcharts.axis", "x"),
39+
ticks = getOption("afcharts.ticks", "xy"),
40+
legend = getOption("afcharts.legend", "right"),
41+
axis_text = getOption("afcharts.axis_text", "xy"),
42+
axis_title = getOption("afcharts.axis_title", "xy"),
43+
legend_title = getOption("afcharts.legend_title", "show")
44+
) {
4545

4646
grid <- match.arg(grid, c("y", "x", "xy", "none"))
4747
axis <- match.arg(axis, c("x", "y", "xy", "none"))

R/use_afcharts.R

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,14 @@ use_afcharts <- function(default_colour =
4343

4444
# Set options for all theme_af arguments provided in ...
4545

46-
theme_args <- names(formals(theme_af))
47-
for (arg in intersect(names(dots), theme_args)) {
48-
options(setNames(list(dots[[arg]]), paste0("afcharts.", arg)))
49-
}
46+
theme_args <- intersect(names(dots), names(formals(theme_af)))
47+
options(
48+
setNames(
49+
dots[theme_args],
50+
paste0("afcharts.", theme_args, recycle0 = TRUE)
51+
)
52+
)
53+
5054

5155
# Use afcharts theme ----
5256

@@ -81,14 +85,6 @@ use_afcharts <- function(default_colour =
8185
# Get default base sizes used in theme
8286
default <- formals(theme_af)
8387

84-
# Update default values with those passed to use_afcharts
85-
new_values <- c(...)
86-
for (i in seq_along(new_values)) {
87-
default <- replace(default,
88-
which(names(default) == names(new_values)[i]),
89-
new_values[i])
90-
}
91-
9288
# Evaluate base_size values for use in geom defaults
9389
base_size <- eval(default$base_size)
9490
base_line_size <- eval(default$base_line_size)
@@ -208,9 +204,12 @@ use_afcharts <- function(default_colour =
208204
}
209205

210206
theme_args <- names(formals(theme_af))
211-
for (arg in theme_args) {
212-
options(setNames(list(NULL), paste0("afcharts.", arg)))
213-
}
207+
options(
208+
setNames(
209+
vector("list", length = length(theme_args)),
210+
paste0("afcharts.", theme_args)
211+
)
212+
)
214213

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

man/theme_af.Rd

Lines changed: 12 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 55 additions & 0 deletions
Loading
Lines changed: 56 additions & 0 deletions
Loading

tests/testthat/test-chart-output.R

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -273,25 +273,36 @@ test_that("use_afcharts works", {
273273

274274
})
275275

276-
# Test theme_af()
277-
278-
test_that("theme_af() inherits arguments from use_afcharts() including defaults", {
276+
test_that("theme_af() inherits arguments from use_afcharts()", {
279277

280278
use_afcharts(reset = TRUE)
281-
use_afcharts(base_size = 20,
282-
base_rect_size = 10,
283-
grid = "x",
284-
axis = "xy",
285-
ticks = "none",
286-
legend = "left")
279+
280+
use_afcharts(
281+
base_size = 20,
282+
base_line_size = 15,
283+
base_rect_size = 10,
284+
grid = "x",
285+
axis = "xy",
286+
ticks = "none",
287+
legend = "left",
288+
axis_text = "x",
289+
axis_title = "y",
290+
legend_title = "none"
291+
)
287292

288293
d <- subset(ggplot2::mpg, manufacturer == "ford")
289294

290-
plot5 <- ggplot2::ggplot(d, ggplot2::aes(x = model, fill = class)) +
295+
plot1 <- ggplot2::ggplot(d, ggplot2::aes(x = model, fill = class)) +
291296
ggplot2::geom_bar() +
292297
theme_af()
293298

294-
expect_match_plot("use_afcharts_5", plot5)
299+
expect_match_plot("use_afcharts_theme_af1", plot1)
300+
301+
plot2 <- ggplot2::ggplot(d, ggplot2::aes(x = model, fill = class)) +
302+
ggplot2::geom_bar() +
303+
theme_af(base_size = 5, legend_title = "show")
304+
305+
expect_match_plot("use_afcharts_theme_af2", plot2)
295306

296307
use_afcharts(reset = TRUE)
297308
})

0 commit comments

Comments
 (0)