Skip to content

Make sure boxplot check doesn't polute other stats/geoms #3800

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

Merged
merged 2 commits into from
Feb 6, 2020
Merged
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
4 changes: 3 additions & 1 deletion R/stat-boxplot.r
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ StatBoxplot <- ggproto("StatBoxplot", Stat,
},

setup_params = function(data, params) {
params$flipped_aes <- has_flipped_aes(data, params, main_is_orthogonal = TRUE, group_has_equal = TRUE)
params$flipped_aes <- has_flipped_aes(data, params, main_is_orthogonal = TRUE,
group_has_equal = TRUE,
main_is_optional = TRUE)
data <- flip_data(data, params$flipped_aes)

has_x <- !(is.null(data$x) && is.null(params$x))
Expand Down
24 changes: 17 additions & 7 deletions R/utilities.r
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,10 @@ switch_orientation <- function(aesthetics) {
#' will be the discrete-like one. Examples of `TRUE` is [stat_density()] and
#' [stat_bin()], while examples of `FALSE` is [stat_ydensity()] and
#' [stat_boxplot()]
#' - `main_is_optional`: This argument controls the rare case of layers were the
#' main direction is an optional aesthetic. This is only seen in
#' [stat_boxplot()] where `x` is set to `0` if not given. If `TRUE` there will
#' be a check for whether all `x` or all `y` are equal to `0`
#'
#' @param data The layer data
#' @param params The parameters of the `Stat`/`Geom`. Only the `orientation`
Expand All @@ -491,6 +495,8 @@ switch_orientation <- function(aesthetics) {
#' will only be flipped if `params$orientation == "y"`
#' @param main_is_continuous If there is a discrete and continuous axis, does
#' the continuous one correspond to the main orientation?
#' @param main_is_optional Is the main axis aesthetic optional and, if not
#' given, set to `0`
#' @param flip Logical. Is the layer flipped.
#'
#' @return `has_flipped_aes()` returns `TRUE` if it detects a layer in the other
Expand All @@ -507,7 +513,8 @@ switch_orientation <- function(aesthetics) {
#'
has_flipped_aes <- function(data, params = list(), main_is_orthogonal = NA,
range_is_orthogonal = NA, group_has_equal = FALSE,
ambiguous = FALSE, main_is_continuous = FALSE) {
ambiguous = FALSE, main_is_continuous = FALSE,
main_is_optional = FALSE) {
# Is orientation already encoded in data?
if (!is.null(data$flipped_aes)) {
not_na <- which(!is.na(data$flipped_aes))
Expand Down Expand Up @@ -591,12 +598,15 @@ has_flipped_aes <- function(data, params = list(), main_is_orthogonal = NA,
if (xor(y_is_int, x_is_int)) {
return(y_is_int != main_is_continuous)
}
# Is one of the axes a single value
if (all(x == 1)) {
return(main_is_continuous)
}
if (all(y == 1)) {
return(!main_is_continuous)

if (main_is_optional) {
# Is one of the axes all 0
if (all(x == 0)) {
return(main_is_continuous)
}
if (all(y == 0)) {
return(!main_is_continuous)
}
}

y_diff <- diff(sort(y))
Expand Down
10 changes: 9 additions & 1 deletion man/bidirection.Rd

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