Skip to content

Commit

Permalink
Generation of non-intercept data in hline/vline should respect coord_…
Browse files Browse the repository at this point in the history
…flip(), closes #1519
  • Loading branch information
cpsievert committed Jul 13, 2019
1 parent 2a439bc commit 237f89d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
12 changes: 8 additions & 4 deletions R/layers2traces.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Expand All @@ -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"))
}
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-ggplot-hline.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
14 changes: 14 additions & 0 deletions tests/testthat/test-ggplot-vline.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})

0 comments on commit 237f89d

Please sign in to comment.