Skip to content

Commit f136000

Browse files
cpsievertclaude
andcommitted
Fix #2455, #2460: element_blank panel.border no longer creates empty shapes
The make_panel_border() function now checks if panel.border is NULL or element_blank before processing. This prevents creating useless invisible shapes when themes like theme_grey() or theme_classic() have blank borders. Themes with visible borders (like theme_bw()) continue to work correctly. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 7c09349 commit f136000

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

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]

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)