From 237f89d07ffcee64c934fdd6124f5c7e65ba2347 Mon Sep 17 00:00:00 2001 From: Carson Date: Fri, 12 Jul 2019 18:20:19 -0500 Subject: [PATCH] Generation of non-intercept data in hline/vline should respect coord_flip(), closes #1519 --- R/layers2traces.R | 12 ++++++++---- tests/testthat/test-ggplot-hline.R | 13 +++++++++++++ tests/testthat/test-ggplot-vline.R | 14 ++++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/R/layers2traces.R b/R/layers2traces.R index fba34f75c9..f35d8533e3 100644 --- a/R/layers2traces.R +++ b/R/layers2traces.R @@ -417,8 +417,10 @@ to_basic.GeomHline <- function(data, prestats_data, layout, params, p, ...) { data$group <- do.call(paste, data[!grepl("group", names(data)) & !vapply(data, anyNA, logical(1))] ) - lay <- tidyr::gather_(layout$layout, "variable", "x", c("x_min", "x_max")) - data <- merge(lay[c("PANEL", "x")], data, by = "PANEL") + x <- if (inherits(p$coordinates, "CoordFlip")) "y" else "x" + lay <- tidyr::gather_(layout$layout, "variable", x, paste0(x, c("_min", "_max"))) + data <- merge(lay[c("PANEL", x)], data, by = "PANEL") + data[["x"]] <- data[[x]] data[["y"]] <- data$yintercept prefix_class(data, c("GeomHline", "GeomPath")) } @@ -429,8 +431,10 @@ to_basic.GeomVline <- function(data, prestats_data, layout, params, p, ...) { data$group <- do.call(paste, data[!grepl("group", names(data)) & !vapply(data, anyNA, logical(1))] ) - lay <- tidyr::gather_(layout$layout, "variable", "y", c("y_min", "y_max")) - data <- merge(lay[c("PANEL", "y")], data, by = "PANEL") + y <- if (inherits(p$coordinates, "CoordFlip")) "x" else "y" + lay <- tidyr::gather_(layout$layout, "variable", y, paste0(y, c("_min", "_max"))) + data <- merge(lay[c("PANEL", y)], data, by = "PANEL") + data[["y"]] <- data[[y]] data[["x"]] <- data$xintercept prefix_class(data, c("GeomVline", "GeomPath")) } diff --git a/tests/testthat/test-ggplot-hline.R b/tests/testthat/test-ggplot-hline.R index b696c05d49..e227fe99db 100644 --- a/tests/testthat/test-ggplot-hline.R +++ b/tests/testthat/test-ggplot-hline.R @@ -75,3 +75,16 @@ test_that("hline/vline/abline split on linetype/colour/size", { unique(vapply(l$data, function(x) x$line$width, numeric(1))), 4 ) }) + + +test_that("hline works with coord_flip", { + + gg <- ggplot() + + geom_point(aes(6, 5)) + + geom_hline(yintercept = 5) + + coord_flip() + + l <- plotly_build(gg)$x + expect_equivalent(l$data[[2]]$x, c(5, 5)) + expect_equivalent(l$data[[2]]$y, c(5.95, 6.05)) +}) diff --git a/tests/testthat/test-ggplot-vline.R b/tests/testthat/test-ggplot-vline.R index f83fea235a..e215c993a2 100644 --- a/tests/testthat/test-ggplot-vline.R +++ b/tests/testthat/test-ggplot-vline.R @@ -33,3 +33,17 @@ test_that("vector xintercept results in multiple vertical lines", { expect_true(l$mode == "lines") expect_true(l$line$color == "rgba(0,0,255,1)") }) + + + +test_that("vline works with coord_flip", { + + gg <- ggplot() + + geom_point(aes(5, 6)) + + geom_vline(xintercept = 5) + + coord_flip() + + l <- plotly_build(gg)$x + expect_equivalent(l$data[[2]]$x, c(5.95, 6.05)) + expect_equivalent(l$data[[2]]$y, c(5, 5)) +})