Skip to content

Commit e076d9b

Browse files
authored
Better handle position aesthetics in params (#3666)
1 parent 010bffc commit e076d9b

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

R/geom-linerange.r

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ GeomLinerange <- ggproto("GeomLinerange", Geom,
9999

100100
setup_params = function(data, params) {
101101
params$flipped_aes <- has_flipped_aes(data, params, range_is_orthogonal = TRUE)
102-
if (!(params$flipped_aes || all(c("x", "ymin", "ymax") %in% names(data)))) {
102+
# if flipped_aes == TRUE then y, xmin, xmax is present
103+
if (!(params$flipped_aes || all(c("x", "ymin", "ymax") %in% c(names(data), names(params))))) {
103104
stop("Either, `x`, `ymin`, and `ymax` or `y`, `xmin`, and `xmax` must be supplied", call. = FALSE)
104105
}
105106
params

R/utilities.r

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -512,20 +512,27 @@ has_flipped_aes <- function(data, params = list(), main_is_orthogonal = NA,
512512
return(params$orientation == "y")
513513
}
514514

515+
x <- data$x %||% params$x
516+
y <- data$y %||% params$y
517+
xmin <- data$xmin %||% params$xmin
518+
ymin <- data$ymin %||% params$ymin
519+
xmax <- data$xmax %||% params$xmax
520+
ymax <- data$ymax %||% params$ymax
521+
515522
# Does a single x or y aesthetic corespond to a specific orientation
516-
if (!is.na(main_is_orthogonal) && sum(c("x", "y") %in% names(data)) + sum(c("x", "y") %in% names(params)) == 1) {
517-
return(("x" %in% names(data) || "x" %in% names(params)) == main_is_orthogonal)
523+
if (!is.na(main_is_orthogonal) && xor(is.null(x), is.null(y))) {
524+
return(is.null(y) == main_is_orthogonal)
518525
}
519526

520-
has_x <- !is.null(data$x)
521-
has_y <- !is.null(data$y)
527+
has_x <- !is.null(x)
528+
has_y <- !is.null(y)
522529

523530
# Does a provided range indicate an orientation
524531
if (!is.na(range_is_orthogonal)) {
525-
if (any(c("ymin", "ymax") %in% names(data))) {
532+
if (!is.null(ymin) || !is.null(ymax)) {
526533
return(!range_is_orthogonal)
527534
}
528-
if (any(c("xmin", "xmax") %in% names(data))) {
535+
if (!is.null(xmin) || !is.null(xmax)) {
529536
return(range_is_orthogonal)
530537
}
531538
}
@@ -536,21 +543,23 @@ has_flipped_aes <- function(data, params = list(), main_is_orthogonal = NA,
536543
}
537544

538545
# Is there a single actual discrete position
539-
y_is_int <- is.integer(data$y)
540-
x_is_int <- is.integer(data$x)
546+
y_is_int <- is.integer(y)
547+
x_is_int <- is.integer(x)
541548
if (xor(y_is_int, x_is_int)) {
542549
return(y_is_int != main_is_continuous)
543550
}
544551

545552
# Does each group have a single x or y value
546553
if (group_has_equal) {
547554
if (has_x) {
555+
if (length(x) == 1) return(FALSE)
548556
x_groups <- vapply(split(data$x, data$group), function(x) length(unique(x)), integer(1))
549557
if (all(x_groups == 1)) {
550558
return(FALSE)
551559
}
552560
}
553561
if (has_y) {
562+
if (length(y) == 1) return(TRUE)
554563
y_groups <- vapply(split(data$y, data$group), function(x) length(unique(x)), integer(1))
555564
if (all(y_groups == 1)) {
556565
return(TRUE)
@@ -568,21 +577,21 @@ has_flipped_aes <- function(data, params = list(), main_is_orthogonal = NA,
568577
return(FALSE)
569578
}
570579
# Is there a single discrete-like position
571-
y_is_int <- if (has_y) isTRUE(all.equal(data$y, round(data$y))) else FALSE
572-
x_is_int <- if (has_x) isTRUE(all.equal(data$x, round(data$x))) else FALSE
580+
y_is_int <- if (has_y) isTRUE(all.equal(y, round(y))) else FALSE
581+
x_is_int <- if (has_x) isTRUE(all.equal(x, round(x))) else FALSE
573582
if (xor(y_is_int, x_is_int)) {
574583
return(y_is_int != main_is_continuous)
575584
}
576585
# Is one of the axes a single value
577-
if (all(data$x == 1)) {
586+
if (all(x == 1)) {
578587
return(main_is_continuous)
579588
}
580-
if (all(data$y == 1)) {
589+
if (all(y == 1)) {
581590
return(!main_is_continuous)
582591
}
583592
# If both are discrete like, which have most 0 or 1-spaced values
584-
y_diff <- diff(sort(data$y))
585-
x_diff <- diff(sort(data$x))
593+
y_diff <- diff(sort(y))
594+
x_diff <- diff(sort(x))
586595

587596
if (y_is_int && x_is_int) {
588597
return((sum(x_diff <= 1) < sum(y_diff <= 1)) != main_is_continuous)

0 commit comments

Comments
 (0)