8000 Bug fix: allow empty `Vlines` and `Hlines` as `ggplot2` does by dereckmezquita · Pull Request #2252 · plotly/plotly.R · GitHub
[go: up one dir, main page]

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 9 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
9 changes: 7 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,10 @@ 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")
# fix for #1947; applied to Hline as well
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
34 changes: 34 additions & 0 deletions tests/testthat/test-ggplot-hline.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,37 @@ 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))
})

# fix for issue #1974 pull request #2252
test_that("geom_vline/geom_hline does not throw an error with ggplotly when no lines are found", {
x <- seq(0, 10, by = 1)
# random walk
y <- cumsum(rnorm(length(x), mean = 0, sd = 1))
df <- data.frame(x, y)
df$value_of_interest <- "not_voi"
df[round(df$x, 0) == 2, ]$value_of_interest <- "voi" # value of interest

# horizontal line no error
p3 <- ggplot2::ggplot(df) +
ggplot2::geom_line(ggplot2::aes(x = x, y = y)) +
ggplot2::geom_hline(
ggplot2::aes(yintercept = x),
data = df[df$value_of_interest == "voi", ],
colour = "pink"
)

expect_error(plotly::ggplotly(p3), NA) # lines are found no error is thrown

# horizontal line not set; error
p4 <- ggplot2::ggplot(df) +
ggplot2::geom_line(ggplot2::aes(x = x, y = y)) +
ggplot2::geom_hline(
ggplot2::aes(yintercept = x),
data = df[df$value_of_interest == "something_not_matched", ],
colour = "pink"
)

# error given without fix:
# "Error in fix.by(by.y, y) : 'by' must specify a uniquely valid column"
expect_error(plotly::ggplotly(p4), NA) # no lines are found no error is thrown
})
36 changes: 36 additions & 0 deletions tests/testthat/test-ggplot-vline.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,39 @@ 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))
})


# fix for issue #1974 pull request #2252
test_that("geom_vline/geom_hline does not throw an error with ggplotly when no lines are found", {
x <- seq(0, 10, by = 1)
# random walk
y <- cumsum(rnorm(length(x), mean = 0, sd = 1))
df <- data.frame(x, y)
df$point_of_interest <- "not_poi"
df[df$x == 2, ]$point_of_interest <- "poi" # point of interest

# Case 1: Vertical line by feeding data to it; this allows for programmatically setting many lines at different years
p1 <- ggplot2::ggplot(df) +
ggplot2::geom_line(ggplot2::aes(x = x, y = y)) +
ggplot2::geom_vline(
ggplot2::aes(xintercept = x),
data = df[df$point_of_interest == "poi", ],
colour = "yellow"
)

# Test that ggplotly does not throw an error for both cases
expect_error(plotly::ggplotly(p1), NA) # lines are found no error is thrown

# Case 2: No lines are found, ggplot2 accepts it and no error is thrown
p2 <- ggplot2::ggplot(df) +
ggplot2::geom_line(ggplot2::aes(x = x, y = y)) +
ggplot2::geom_vline(
ggplot2::aes(xintercept = x),
data = df[df$point_of_interest == "something_not_matched", ],
colour = "yellow"
)

# error given without fix:
# "Error in fix.by(by.y, y) : 'by' must specify a uniquely valid column"
expect_error(plotly::ggplotly(p2), NA) # no lines are found no error is thrown
})
0