Skip to content

Add missing theme sub args #6492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
39 changes: 27 additions & 12 deletions R/theme-sub.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,58 +54,73 @@ subtheme <- function(elements, prefix = "", suffix = "", call = caller_env()) {

#' @export
#' @describeIn subtheme Theme specification for all axes.
theme_sub_axis <- function(title, text, ticks, ticks.length, line) {
theme_sub_axis <- function(title, text, ticks, ticks.length, line, minor.ticks.length) {
subtheme(find_args(), "axis.")
}

#' @export
#' @describeIn subtheme Theme specification for both x axes.
theme_sub_axis_x <- function(title, text, ticks, ticks.length, line) {
theme_sub_axis_x <- function(title, text, ticks, ticks.length, line, minor.ticks.length) {
subtheme(find_args(), "axis.", ".x")
}

#' @export
#' @describeIn subtheme Theme specification for both y axes.
theme_sub_axis_y <- function(title, text, ticks, ticks.length, line) {
theme_sub_axis_y <- function(title, text, ticks, ticks.length, line, minor.ticks.length) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not minor.ticks argument for this and above?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no theme(axis.minor.ticks), they all descent from the major ticks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but then why do you have them below?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should've been more specific: they descent from position-resolved major ticks. To illustrate: there is no minor.ticks.y because minor.ticks.y.left and minor.ticks.y.right descent from ticks.y.left and ticks.y.right respectively.

subtheme(find_args(), "axis.", ".y")
}

#' @export
#' @describeIn subtheme Theme specification for the bottom x axis.
theme_sub_axis_bottom <- function(title, text, ticks, ticks.length, line) {
theme_sub_axis_bottom <- function(title, text, ticks, ticks.length, line, minor.ticks, minor.ticks.length) {
subtheme(find_args(), "axis.", ".x.bottom")
}

#' @export
#' @describeIn subtheme Theme specification for the top x axis.
theme_sub_axis_top <- function(title, text, ticks, ticks.length, line) {
theme_sub_axis_top <- function(title, text, ticks, ticks.length, line, minor.ticks, minor.ticks.length) {
subtheme(find_args(), "axis.", ".x.top")
}

#' @export
#' @describeIn subtheme Theme specification for the left y axis.
theme_sub_axis_left <- function(title, text, ticks, ticks.length, line) {
theme_sub_axis_left <- function(title, text, ticks, ticks.length, line, minor.ticks, minor.ticks.length) {
subtheme(find_args(), "axis.", ".y.left")
}

#' @export
#' @describeIn subtheme Theme specification for the right y axis.
theme_sub_axis_right <- function(title, text, ticks, ticks.length, line) {
theme_sub_axis_right <- function(title, text, ticks, ticks.length, line, minor.ticks, minor.ticks.length) {
subtheme(find_args(), "axis.", ".y.right")
}

#' @export
#' @describeIn subtheme Theme specification for the legend.
theme_sub_legend <- function(background, margin, spacing, spacing.x, spacing.y,
key, key.size, key.height, key.width, text, title,
position, direction, justification, box, box.just,
box.margin, box.background, box.spacing) {
theme_sub_legend <- function(
# Text stuff
text, text.position, title, title.position,
# Drawn elements
background, frame, ticks, ticks.length, axis.line,
# Spacings
spacing, spacing.x, spacing.y, margin,
# Seys
key, key.size, key.height, key.width, key.spacing, key.spacing.x,
key.spacing.y, key.justification,
# Settings
byrow, position, direction, location, position.inside,
# Justification
justification, justification.top, justification.bottom, justification.left,
justification.right, justification.inside,
# Box
box, box.just, box.margin, box.background, box.spacing
) {
subtheme(find_args(), "legend.")
}

#' @export
#' @describeIn subtheme Theme specification for the panels.
theme_sub_panel <- function(background, border, spacing, spacing.x, spacing.y,
theme_sub_panel <- function(background, border,
widths, heights, spacing, spacing.x, spacing.y,
grid, grid.major, grid.minor, grid.major.x,
grid.major.y, grid.minor.x, grid.minor.y, ontop) {
subtheme(find_args(), "panel.")
Expand Down
74 changes: 63 additions & 11 deletions man/subtheme.Rd

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

19 changes: 19 additions & 0 deletions tests/testthat/_snaps/theme.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,22 @@
The `options('ggplot2.discrete.colour')` setting is incompatible with the `palette.colour.discrete` theme setting.
i You can set `options(ggplot2.discrete.colour = NULL)`.

# theme elements are covered in `theme_sub_*()` functions

Code
extra_elements
Output
[1] "..." "line"
[3] "rect" "text"
[5] "title" "point"
[7] "polygon" "geom"
[9] "spacing" "margins"
[11] "aspect.ratio" "axis.text.theta"
[13] "axis.text.r" "axis.ticks.theta"
[15] "axis.ticks.r" "axis.minor.ticks.theta"
[17] "axis.minor.ticks.r" "axis.ticks.length.theta"
[19] "axis.ticks.length.r" "axis.minor.ticks.length.theta"
[21] "axis.minor.ticks.length.r" "axis.line.theta"
[23] "axis.line.r" "complete"
[25] "validate"

22 changes: 22 additions & 0 deletions tests/testthat/test-theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,28 @@ test_that("geom elements are inherited correctly", {
expect_equal(p$colour, "red")
})

test_that("theme elements are covered in `theme_sub_*()` functions", {
# We use a snapshot test here to trigger when a new theme element is added
# or removed.
# A failure of this test should be taken as a prompt to see if the new
# theme element should be included in one of the `theme_sub_*` functions.

fmls <- paste0("axis.", fn_fmls_names(theme_sub_axis))
fmls <- c(fmls, paste0("axis.", fn_fmls_names(theme_sub_axis_x), ".x"))
fmls <- c(fmls, paste0("axis.", fn_fmls_names(theme_sub_axis_y), ".y"))
fmls <- c(fmls, paste0("axis.", fn_fmls_names(theme_sub_axis_top), ".x.top"))
fmls <- c(fmls, paste0("axis.", fn_fmls_names(theme_sub_axis_bottom), ".x.bottom"))
fmls <- c(fmls, paste0("axis.", fn_fmls_names(theme_sub_axis_left), ".y.left"))
fmls <- c(fmls, paste0("axis.", fn_fmls_names(theme_sub_axis_right), ".y.right"))
fmls <- c(fmls, paste0("legend.", fn_fmls_names(theme_sub_legend)))
fmls <- c(fmls, paste0("plot.", fn_fmls_names(theme_sub_plot)))
fmls <- c(fmls, paste0("panel.", fn_fmls_names(theme_sub_panel)))
fmls <- c(fmls, paste0("strip.", fn_fmls_names(theme_sub_strip)))

extra_elements <- setdiff(fn_fmls_names(theme), fmls)
expect_snapshot(extra_elements)
})

# Visual tests ------------------------------------------------------------

test_that("element_polygon() can render a grob", {
Expand Down