Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bcf2a21

Browse filesBrowse files
Bug fix: allow empty Vlines and Hlines as ggplot2 does (#2252)
Co-authored-by: Carson Sievert <cpsievert1@gmail.com>
1 parent 3f1030b commit bcf2a21
Copy full SHA for bcf2a21

File tree

Expand file treeCollapse file tree

4 files changed

+17
-2
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+17
-2
lines changed

‎NEWS.md

Copy file name to clipboardExpand all lines: NEWS.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* 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)
2020
* Closed #2220: `ggplotly()` no longer errors on `stat_summary(geom = "crossbar")`. (#2222)
2121
* Closed #2212: `ggplotly()` no longer removes legends when setting guide properties via `guides(aes = guide_xxx(...))`.
22+
* 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`.
2223

2324
# 4.10.1
2425

‎R/layers2traces.R

Copy file name to clipboardExpand all lines: R/layers2traces.R
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,9 @@ to_basic.GeomHline <- function(data, prestats_data, layout, params, p, ...) {
448448
data = layout$layout, cols = paste0(x, c("_min", "_max")), values_to = x, names_to = "variable"
449449
)
450450
lay <- as.data.frame(lay)
451-
data <- merge(lay[c("PANEL", x)], data, by = "PANEL")
451+
if (nrow(data) > 0) {
452+
data <- merge(lay[c("PANEL", x)], data, by = "PANEL")
453+
}
452454
data[["x"]] <- data[[x]]
453455
data[["y"]] <- data$yintercept
454456
prefix_class(data, c("GeomHline", "GeomPath"))
@@ -465,7 +467,9 @@ to_basic.GeomVline <- function(data, prestats_data, layout, params, p, ...) {
465467
data = layout$layout, cols = paste0(y, c("_min", "_max")), values_to = y, names_to = "variable"
466468
)
467469
lay <- as.data.frame(lay)
468-
data <- merge(lay[c("PANEL", y)], data, by = "PANEL")
470+
if (nrow(data) > 0) {
471+
data <- merge(lay[c("PANEL", y)], data, by = "PANEL")
472+
}
469473
data[["y"]] <- data[[y]]
470474
data[["x"]] <- data$xintercept
471475
prefix_class(data, c("GeomVline", "GeomPath"))

‎tests/testthat/test-ggplot-hline.R

Copy file name to clipboardExpand all lines: tests/testthat/test-ggplot-hline.R
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,8 @@ test_that("hline works with coord_flip", {
9797
expect_equivalent(l$data[[2]]$x, c(5, 5))
9898
expect_equivalent(l$data[[2]]$y, c(5.95, 6.05))
9999
})
100+
101+
test_that("geom_vline/geom_hline does not throw an error with ggplotly when no lines are found", {
102+
p3 <- ggplot(df) + geom_hline(aes(yintercept = x), data = data.frame(x = 1)[F, , drop = FALSE])
103+
expect_error(plotly::ggplotly(p3), NA)
104+
})

‎tests/testthat/test-ggplot-vline.R

Copy file name to clipboardExpand all lines: tests/testthat/test-ggplot-vline.R
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,8 @@ test_that("vline works with coord_flip", {
4646
expect_equivalent(l$data[[2]]$x, c(5.95, 6.05))
4747
expect_equivalent(l$data[[2]]$y, c(5, 5))
4848
})
49+
50+
test_that("geom_vline/geom_hline does not throw an error with ggplotly when no lines are found", {
51+
p <- ggplot(df) + geom_vline(aes(xintercept = x), data = data.frame(x = 1)[F, , drop = FALSE])
52+
expect_error(plotly::ggplotly(p), NA)
53+
})

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.