Skip to content

Commit 3292abd

Browse files
committed
Fix #2455, #2460: element_blank panel.border no longer creates empty shapes
1 parent c11110e commit 3292abd

7 files changed

Lines changed: 28 additions & 74 deletions

File tree

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
* `plotly_build()` now works with `ggmatrix` objects (e.g., from `GGally::ggpairs()`). (#2447)
1010
* Closed #2415: `ggplotly()` now shows variables named 'group' in tooltips when mapped to aesthetics like `colour`.
11+
* Closed #2455, #2460: `ggplotly()` no longer creates empty shapes when `panel.border` is `element_blank()` (ggplot2 4.0.0 compatibility).
1112

1213
# plotly 4.11.0
1314

R/ggplotly.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1389,7 +1389,12 @@ make_strip_rect <- function(xdom, ydom, theme, side = "top") {
13891389

13901390
# theme(panel.border) -> plotly.js rect shape
13911391
make_panel_border <- function(xdom, ydom, theme) {
1392-
rekt <- rect2shape(theme[["panel.border"]])
1392+
border <- theme[["panel.border"]]
1393+
# Don't draw anything if panel.border is blank or NULL
1394+
if (is.null(border) || is_blank(border)) {
1395+
return(list())
1396+
}
1397+
rekt <- rect2shape(border)
13931398
rekt$x0 <- xdom[1]
13941399
rekt$x1 <- xdom[2]
13951400
rekt$y0 <- ydom[1]

inst/examples/shiny/event_data/tests/testthat/_snaps/shinytest2/001.json

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -328,24 +328,7 @@
328328
"hoverformat": ".2f"
329329
},
330330
"shapes": [
331-
{
332-
"type": "rect",
333-
"fillcolor": null,
334-
"line": {
335-
"color": null,
336-
"width": 0,
337-
"linetype": [
338331

339-
]
340-
},
341-
"yref": "paper",
342-
"xref": "paper",
343-
"layer": "below",
344-
"x0": 0,
345-
"x1": 1,
346-
"y0": 0,
347-
"y1": 1
348-
}
349332
],
350333
"showlegend": false,
351334
"legend": {

inst/examples/shiny/event_data/tests/testthat/_snaps/shinytest2/002.json

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"plotly_afterplot-A": "\"plot\"",
1919
"plotly_click-A": "[{\"curveNumber\":0,\"pointNumber\":7,\"x\":24.4,\"y\":3.19,\"customdata\":\"Merc 240D\"}]",
2020
"plotly_hover-A": null,
21-
"plotly_relayout-A": "{\"width\":962,\"height\":400}"
21+
"plotly_relayout-A": "{\"width\":947,\"height\":400}"
2222
},
2323
"output": {
2424
"brushed": "[1] \"Brush extents appear here (double-click to clear)\"",
@@ -331,24 +331,7 @@
331331
"hoverformat": ".2f"
332332
},
333333
"shapes": [
334-
{
335-
"type": "rect",
336-
"fillcolor": null,
337-
"line": {
338-
"color": null,
339-
"width": 0,
340-
"linetype": [
341334

342-
]
343-
},
344-
"yref": "paper",
345-
"xref": "paper",
346-
"layer": "below",
347-
"x0": 0,
348-
"x1": 1,
349-
"y0": 0,
350-
"y1": 1
351-
}
352335
],
353336
"showlegend": false,
354337
"legend": {

inst/examples/shiny/event_data/tests/testthat/_snaps/shinytest2/003.json

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"plotly_brushing-A": "{\"x\":[23.95978500551268,25.98332414553473],\"y\":[3.0020072289156627,3.5073743975903615]}",
2121
"plotly_click-A": "[{\"curveNumber\":0,\"pointNumber\":7,\"x\":24.4,\"y\":3.19,\"customdata\":\"Merc 240D\"}]",
2222
"plotly_hover-A": null,
23-
"plotly_relayout-A": "{\"width\":962,\"height\":400}",
23+
"plotly_relayout-A": "{\"width\":947,\"height\":400}",
2424
"plotly_selected-A": "[{\"curveNumber\":0,\"pointNumber\":7,\"x\":24.4,\"y\":3.19,\"customdata\":\"Merc 240D\"}]",
2525
"plotly_selecting-A": "[{\"curveNumber\":0,\"pointNumber\":7,\"x\":24.4,\"y\":3.19,\"customdata\":\"Merc 240D\"}]"
2626
},
@@ -335,24 +335,7 @@
335335
"hoverformat": ".2f"
336336
},
337337
"shapes": [
338-
{
339-
"type": "rect",
340-
"fillcolor": null,
341-
"line": {
342-
"color": null,
343-
"width": 0,
344-
"linetype": [
345338

346-
]
347-
},
348-
"yref": "paper",
349-
"xref": "paper",
350-
"layer": "below",
351-
"x0": 0,
352-
"x1": 1,
353-
"y0": 0,
354-
"y1": 1
355-
}
356339
],
357340
"showlegend": false,
358341
"legend": {

inst/examples/shiny/event_data/tests/testthat/_snaps/shinytest2/004.json

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"plotly_click-A": null,
2222
"plotly_deselect-A": "\"plot\"",
2323
"plotly_hover-A": null,
24-
"plotly_relayout-A": "{\"width\":962,\"height\":400}",
24+
"plotly_relayout-A": "{\"width\":947,\"height\":400}",
2525
"plotly_selected-A": null,
2626
"plotly_selecting-A": null
2727
},
@@ -336,24 +336,7 @@
336336
"hoverformat": ".2f"
337337
},
338338
"shapes": [
339-
{
340-
"type": "rect",
341-
"fillcolor": null,
342-
"line": {
343-
"color": null,
344-
"width": 0,
345-
"linetype": [
346339

347-
]
348-
},
349-
"yref": "paper",
350-
"xref": "paper",
351-
"layer": "below",
352-
"x0": 0,
353-
"x1": 1,
354-
"y0": 0,
355-
"y1": 1
356-
}
357340
],
358341
"showlegend": false,
359342
"legend": {

tests/testthat/test-ggplot-theme.R

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,28 @@ test_that("marker default shape is a circle", {
6565
test_that("plot panel border is translated correctly", {
6666
ggpenguin <- penguin.base + theme_grey() # has no panel.border
6767
info <- expect_doppelganger_built(ggpenguin, "theme-panel-border-1")
68-
68+
6969
red <- ggplot(palmerpenguins::penguins) +
7070
theme_grey() +
7171
geom_point(aes(bill_length_mm, bill_depth_mm)) +
7272
theme(panel.border = element_rect(colour = "red", fill = NA))
73-
73+
7474
info <- expect_doppelganger_built(red, "theme-panel-border-2")
7575
expect_true(info$layout$shapes[[1]]$line$color == toRGB("red"))
7676
})
77+
78+
test_that("element_blank panel.border does not create empty shapes (#2455, #2460)", {
79+
# theme_grey() has element_blank() panel.border - should NOT create a shape
80+
p_grey <- ggplot(mtcars, aes(wt, mpg)) + geom_point() + theme_grey()
81+
L_grey <- plotly_build(ggplotly(p_grey))$x
82+
# No panel border shapes should be created for element_blank
83+
grey_shapes <- Filter(function(s) identical(s$xref, "paper") && identical(s$yref, "paper"), L_grey$layout$shapes)
84+
expect_equal(length(grey_shapes), 0)
85+
86+
# theme_bw() has visible panel.border - SHOULD create a shape
87+
p_bw <- ggplot(mtcars, aes(wt, mpg)) + geom_point() + theme_bw()
88+
L_bw <- plotly_build(ggplotly(p_bw))$x
89+
bw_shapes <- Filter(function(s) identical(s$xref, "paper") && identical(s$yref, "paper"), L_bw$layout$shapes)
90+
expect_true(length(bw_shapes) > 0)
91+
expect_false(is.na(bw_shapes[[1]]$line$color))
92+
})

0 commit comments

Comments
 (0)