Skip to content

Commit

Permalink
Recognise key_range_auto() result in guide_axis_nested() (#32)
Browse files Browse the repository at this point in the history
* example `GuideNone` from having compatible aesthetics

* recognise actualised `key_range_auto()`

* simplify empty labels guide

* add test

* add news bullet
  • Loading branch information
teunbrand authored Dec 3, 2024
1 parent 5707283 commit fd4c78c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# legendry (development version)

* Fixed bug where `guide_axis_nested(key = key_range_auto(...))` produced
duplicated labels (#31)

# legendry 0.1.0

First release.
Expand Down
4 changes: 3 additions & 1 deletion R/compose-.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,10 @@ loop_guides <- function(guides, params, method, ...) {

compatible_aes <- function(guides, available_aes, call = caller_env()) {

available <- lapply(guides, `[[`, name = "available_aes")
valid <- !is_each(guides, inherits, what = "GuideNone")
available <- lapply(guides[valid], `[[`, name = "available_aes")
common <- Reduce(any_intersect, available)

if (length(common) < 1) {
cli::cli_abort(
"The guides to combine have no shared {.field available aesthetics}.",
Expand Down
8 changes: 3 additions & 5 deletions R/guide_axis_nested.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,9 @@ guide_axis_nested <- function(
)
pad_discrete <- pad_discrete %||% switch(type, fence = 0.5, 0.4)

if (identical(key, "range_auto")) {
labels <- new_guide(
available_aes = c("any", "x", "y", "r", "theta"),
super = guide_none()
)
if (identical(key, "range_auto") ||
inherits(key, "key_range_auto_function")) {
labels <- guide_none()
} else {
labels <- primitive_labels(angle = angle)
}
Expand Down
4 changes: 3 additions & 1 deletion R/key-range.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,15 @@ key_range_auto <- function(sep = "[^[:alnum:]]+", reverse = FALSE, ...) {
force(reverse)
dots <- label_args(...)
call <- current_call()
function(scale, aesthetic = NULL) {
fun <- function(scale, aesthetic = NULL) {
range_from_label(
scale = scale, aesthetic = aesthetic,
sep = sep, reverse = reverse, extra_args = dots,
call = call
)
}
class(fun) <- union("key_range_auto_function", class(fun))
fun
}

#' @rdname key_range
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-guide_axis_nested.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ test_that("guide_axis_nested logic works", {

})

test_that("guide_axis_nested recognised `key_range_auto()`", {

guide <- guide_axis_nested(key = "range_auto")
expect_s3_class(guide$params$guides[[3]], "GuideNone")

guide <- guide_axis_nested(key = key_range_auto(sep = "foobar"))
expect_s3_class(guide$params$guides[[3]], "GuideNone")

guide <- guide_axis_nested(key = key_range_manual(1, 2))
expect_s3_class(guide$params$guides[[3]], "PrimitiveLabels")

})

# Visual test -------------------------------------------------------------

test_that("guide_axis_nested looks good as axis", {

base <- ggplot(mpg, aes(interaction(cyl, drv), hwy)) +
Expand Down

0 comments on commit fd4c78c

Please sign in to comment.