Skip to content

Bug fix: allow empty Vlines and Hlines as ggplot2 does #2252

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 16 commits into from
Jun 3, 2023
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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Closed #2208: `ggplotly()` no longer errors given a `geom_area()` with 1 or less data points (error introduced by new behavior in ggplot2 v3.4.0). (#2209)
* Closed #2220: `ggplotly()` no longer errors on `stat_summary(geom = "crossbar")`. (#2222)
* Closed #2212: `ggplotly()` no longer removes legends when setting guide properties via `guides(aes = guide_xxx(...))`.
* Closed #1947: `ggplotly()` now correctly handles `geom_vline`/`geom_hline` with empty data. Previously, if `geom_vline`/`geom_hline` was passed an empty data frame, it would result in an error. The plot is drawn even if no lines are found; this is the same behavior as `ggplot2`.

# 4.10.1

Expand Down
8 changes: 6 additions & 2 deletions R/layers2traces.R
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,9 @@ to_basic.GeomHline <- function(data, prestats_data, layout, params, p, ...) {
data = layout$layout, cols = paste0(x, c("_min", "_max")), values_to = x, names_to = "variable"
)
lay <- as.data.frame(lay)
data <- merge(lay[c("PANEL", x)], data, by = "PANEL")
if (nrow(data) > 0) {
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 @@ -462,7 +464,9 @@ to_basic.GeomVline <- function(data, prestats_data, layout, params, p, ...) {
data = layout$layout, cols = paste0(y, c("_min", "_max")), values_to = y, names_to = "variable"
)
lay <- as.data.frame(lay)
data <- merge(lay[c("PANEL", y)], data, by = "PANEL")
if (nrow(data) > 0) {
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
5 changes: 5 additions & 0 deletions tests/testthat/test-ggplot-hline.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,8 @@ test_that("hline works with coord_flip", {
expect_equivalent(l$data[[2]]$x, c(5, 5))
expect_equivalent(l$data[[2]]$y, c(5.95, 6.05))
})

test_that("geom_vline/geom_hline does not throw an error with ggplotly when no lines are found", {
p3 <- ggplot(df) + geom_hline(aes(yintercept = x), data = data.frame(x = 1)[F, , drop = FALSE])
expect_error(plotly::ggplotly(p3), NA)
})
5 changes: 5 additions & 0 deletions tests/testthat/test-ggplot-vline.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@ test_that("vline works with coord_flip", {
expect_equivalent(l$data[[2]]$x, c(5.95, 6.05))
expect_equivalent(l$data[[2]]$y, c(5, 5))
})

test_that("geom_vline/geom_hline does not throw an error with ggplotly when no lines are found", {
p <- ggplot(df) + geom_vline(aes(xintercept = x), data = data.frame(x = 1)[F, , drop = FALSE])
expect_error(plotly::ggplotly(p), NA)
})