Skip to content

Commit 909db3b

Browse files
authored
Merge pull request #1494 from ropensci/sizemode-pixel
reposition [x-y]anchor when [x-y]sizemode='pixel'
2 parents 572d065 + 341305c commit 909db3b

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

R/subplots.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ reposition <- function(obj, domains) {
416416
o <- obj[[i]]
417417
xs <- if (identical(o$xref, "paper")) {
418418
if (is.numeric(o$sizex)) obj[[i]]$sizex <- o$sizex * abs(diff(xdom))
419-
c("x", "x0", "x1")
419+
if (identical(o$xsizemode, "pixel")) "xanchor" else c("x", "x0", "x1")
420420
}
421421
for (j in xs) {
422422
if (is.numeric(o[[j]])) {
@@ -425,7 +425,7 @@ reposition <- function(obj, domains) {
425425
}
426426
ys <- if (identical(o$yref, "paper")) {
427427
if (is.numeric(o$sizey)) obj[[i]]$sizey <- o$sizey * abs(diff(ydom))
428-
c("y", "y0", "y1")
428+
if (identical(o$ysizemode, "pixel")) "yanchor" else c("y", "y0", "y1")
429429
}
430430
for (j in ys) {
431431
if (is.numeric(o[[j]])) {

tests/figs/subplot/plotly-subplot-ggmatrix.svg

Lines changed: 1 addition & 1 deletion
Loading
Lines changed: 1 addition & 0 deletions
Loading

tests/testthat/test-plotly-subplot.R

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,56 @@ test_that("shape paper repositioning", {
208208
expect_equal(y1, rep(0.75, 2))
209209
expect_equal(xref, rep("paper", 2))
210210
expect_equal(yref, rep("paper", 2))
211+
212+
# now with a fixed height/width
213+
p1 <- plot_ly() %>%
214+
layout(
215+
shapes = list(
216+
type = "rect",
217+
x0 = 0.25,
218+
x1 = 0.75,
219+
xref = "paper",
220+
y0 = 0,
221+
y1 = 30,
222+
yanchor = 0.5,
223+
ysizemode = "pixel",
224+
yref = "paper",
225+
fillcolor = "red"
226+
)
227+
)
228+
p2 <- plot_ly() %>%
229+
layout(
230+
shapes = list(
231+
type = "rect",
232+
y0 = 0.25,
233+
y1 = 0.75,
234+
yref = "paper",
235+
x0 = 0,
236+
x1 = 30,
237+
xanchor = 0.5,
238+
xsizemode = "pixel",
239+
xref = "paper",
240+
line = list(color = "blue")
241+
)
242+
)
243+
244+
s <- subplot(p1, p2)
245+
shapes <- expect_doppelganger_built(s, "subplot-reposition-shape-fixed")$layout$shapes
246+
expect_length(shapes, 2)
247+
248+
xanchor <- lapply(shapes, "[[", "xanchor")[[2]]
249+
yanchor <- lapply(shapes, "[[", "yanchor")[[1]]
250+
x0 <- sapply(shapes, "[[", "x0")
251+
x1 <- sapply(shapes, "[[", "x1")
252+
y0 <- sapply(shapes, "[[", "y0")
253+
y1 <- sapply(shapes, "[[", "y1")
254+
255+
expect_equal(xanchor, 0.76)
256+
expect_equal(yanchor, 0.5)
257+
expect_equal(x0, c(0.12, 0))
258+
expect_equal(x1, c(0.36, 30))
259+
expect_equal(y0, c(0, 0.25))
260+
expect_equal(y1, c(30, 0.75))
211261
})
212262

213263

0 commit comments

Comments
 (0)