Skip to content

Commit

Permalink
Various fixes to orientation sniffing (#3553)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasp85 authored Oct 9, 2019
1 parent a2ab197 commit 9b667b9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion R/position-stack.r
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ PositionStack <- ggproto("PositionStack", Position,
vars = c("x", "xmin", "xmax", "y"),
name = "position_stack"
)
flip_data(data, params$flip_data)
flip_data(data, params$flipped_aes)
},

compute_panel = function(data, params, scales) {
Expand Down
14 changes: 11 additions & 3 deletions R/utilities.r
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,10 @@ has_flipped_aes <- function(data, params = list(), main_is_orthogonal = NA,
ambiguous = FALSE, main_is_continuous = FALSE) {
# Is orientation already encoded in data?
if (!is.null(data$flipped_aes)) {
return(data$flipped_aes[[1]])
not_na <- which(!is.na(data$flipped_aes))
if (length(not_na) != 0) {
return(data$flipped_aes[[not_na[1L]]])
}
}

# Is orientation requested in the params
Expand Down Expand Up @@ -583,12 +586,17 @@ has_flipped_aes <- function(data, params = list(), main_is_orthogonal = NA,
# If both are discrete like, which have most 0 or 1-spaced values
y_diff <- diff(sort(data$y))
x_diff <- diff(sort(data$x))

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

y_diff <- y_diff[y_diff != 0]
x_diff <- x_diff[x_diff != 0]

# If none are discrete is either regularly spaced
y_is_regular <- if (has_y) all((y_diff / min(y_diff)) %% 1 < .Machine$double.eps) else FALSE
x_is_regular <- if (has_x) all((x_diff / min(x_diff)) %% 1 < .Machine$double.eps) else FALSE
y_is_regular <- if (has_y && length(y_diff) != 0) all((y_diff / min(y_diff)) %% 1 < .Machine$double.eps) else FALSE
x_is_regular <- if (has_x && length(x_diff) != 0) all((x_diff / min(x_diff)) %% 1 < .Machine$double.eps) else FALSE
if (xor(y_is_regular, x_is_regular)) {
return(y_is_regular != main_is_continuous)
}
Expand Down

0 comments on commit 9b667b9

Please sign in to comment.