Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 14 additions & 2 deletions R/layers2traces.R
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,11 @@ to_basic.GeomErrorbar <- function(data, prestats_data, layout, params, p, ...) {
# width for ggplot2 means size of the entire bar, on the data scale
# (plotly.js wants half, in pixels)
data <- merge(data, layout$layout, by = "PANEL", sort = FALSE)
data$width <- (data[["xmax"]] - data[["x"]]) /(data[["x_max"]] - data[["x_min"]])
data$width <- if (params[["flipped_aes"]]) {
(data[["ymax"]] - data[["y"]]) /(data[["y_max"]] - data[["y_min"]])
} else {
(data[["xmax"]] - data[["x"]]) /(data[["x_max"]] - data[["x_min"]])
}
data$fill <- NULL
prefix_class(data, "GeomErrorbar")
}
Expand Down Expand Up @@ -873,7 +877,11 @@ geom2trace.GeomTile <- function(data, params, p) {

#' @export
geom2trace.GeomErrorbar <- function(data, params, p) {
make_error(data, params, "y")
if (params[["flipped_aes"]]) {
make_error(data, params, "x")
} else {
make_error(data, params, "y")
}
}

#' @export
Expand Down Expand Up @@ -951,6 +959,10 @@ hover_on <- function(data) {

# make trace with errorbars
make_error <- function(data, params, xy = "x") {
# if xy is NULL: set xy to mean of xy_min and xy_max
if (is.null(data[[xy]])) {
data[[xy]] <- (data[[paste0(xy, "min")]] + data[[paste0(xy, "max")]]) / 2
}
color <- aes2plotly(data, params, "colour")
e <- list(
x = data[["x"]],
Expand Down
26 changes: 26 additions & 0 deletions tests/testthat/test-geom-errorbar-flipped-aes.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
context("Errorbar")

test_that("geom_errobar is rendered with flipped aes", {

df <- dplyr::group_by(iris, Species)
df <- dplyr::summarise_if(df, is.numeric, list(m = mean, q1 = ~ quantile(.x, .25), q3 = ~ quantile(.x, .75)))
gp <- ggplot(df, aes(y = Species, xmin = Sepal.Width_q1, xmax = Sepal.Width_q3)) +
geom_errorbar()

L <- plotly_build(gp)

# Tests
# xmin and xmax equal to ggplot
expect_equivalent(L[["x"]][["data"]][[1]][["x"]] + L[["x"]][["data"]][[1]][["error_x"]][["array"]],
ggplot_build(gp)$data[[1]]$xmax)

expect_equivalent(L[["x"]][["data"]][[1]][["x"]] - L[["x"]][["data"]][[1]][["error_x"]][["arrayminus"]],
ggplot_build(gp)$data[[1]]$xmin)
# xmin and xmax equal to data
expect_equivalent(L[["x"]][["data"]][[1]][["x"]] + L[["x"]][["data"]][[1]][["error_x"]][["array"]],
df$Sepal.Width_q3)

expect_equivalent(L[["x"]][["data"]][[1]][["x"]] - L[["x"]][["data"]][[1]][["error_x"]][["arrayminus"]],
df$Sepal.Width_q1)

})
35 changes: 35 additions & 0 deletions tests/testthat/test-geom-errorbar-issue-1751.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
context("Errorbar")

test_that("geom_errobar is rendered when y aes is not set", {

# Example from issue #1751
d <- data.frame(auc=c(0.268707482993197,0.571428571428571),
sup=c(0.407680628614317,0.648343533190079),
inf=c(0.129734337372078,0.494513609667063),
Names = c("Firmicutes","Spirochaetes"))

# Plot with y aes set
p <- ggplot(d, aes(Names)) +
geom_errorbar(aes(y = auc, ymin = inf, ymax = sup))

L <- plotly_build(p)

# Plot with y aes not set
p1 <- ggplot(d, aes(Names)) +
geom_errorbar(aes(ymin = inf, ymax = sup))

L1 <- plotly_build(p1)

# Tests
## array and arrayminus of L and L1 are equivalent
expect_equivalent(L[["x"]][["data"]][[1]][["error_y"]][["array"]],
L1[["x"]][["data"]][[1]][["error_y"]][["array"]])

expect_equivalent(L[["x"]][["data"]][[1]][["error_y"]][["arrayminus"]],
L1[["x"]][["data"]][[1]][["error_y"]][["arrayminus"]])

## array equals difference between sup and auc, array equals difference between auc and inf
expect_equivalent(L1[["x"]][["data"]][[1]]$error_y$array, d$sup - d$auc)
expect_equivalent(L1[["x"]][["data"]][[1]]$error_y$arrayminus, d$auc - d$inf)

})