Skip to content

Commit 1720f9f

Browse files
committed
Have subplot() reanchor image axis references, closes #1332
1 parent 3ed3dd4 commit 1720f9f

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

R/subplots.R

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
subplot <- function(..., nrows = 1, widths = NULL, heights = NULL, margin = 0.02,
6464
shareX = FALSE, shareY = FALSE, titleX = shareX,
6565
titleY = shareY, which_layout = "merge") {
66-
66+
6767

6868
plots <- dots2plots(...)
6969

@@ -215,16 +215,25 @@ subplot <- function(..., nrows = 1, widths = NULL, heights = NULL, margin = 0.02
215215
axisMap <- setNames(sub("axis", "", axisMap), sub("axis", "", names(axisMap)))
216216
newAnchors <- names(axisMap)[match(oldAnchors, axisMap)]
217217
traces[[i]] <- Map(function(tr, a) { tr[[key]] <- a; tr }, traces[[i]], newAnchors)
218-
# also map annotation[i].xref/annotation[i].yref
218+
# also map annotation and image xaxis/yaxis references
219+
# TODO: do this for shapes as well?
219220
ref <- list(xaxis = "xref", yaxis = "yref")[[key]]
220221
if (is.null(ref)) next
221-
if (is.null(annotations[[i]])) next
222-
annotations[[i]] <- Map(function(ann, a) {
223-
if (!identical(ann[[ref]], "paper")) ann[[ref]] <- a
224-
ann
225-
}, annotations[[i]], newAnchors)
222+
if (length(annotations[[i]])) {
223+
annotations[[i]] <- Map(function(x, y) {
224+
if (!identical(x[[ref]], "paper")) x[[ref]] <- y
225+
x
226+
}, annotations[[i]], newAnchors)
227+
}
228+
if (length(images[[i]])) {
229+
images[[i]] <- Map(function(x, y) {
230+
if (!identical(x[[ref]], "paper")) x[[ref]] <- y
231+
x
232+
}, images[[i]], newAnchors)
233+
}
226234
}
227235

236+
228237
# rescale domains according to the tabular layout
229238
xDom <- as.numeric(domainInfo[i, c("xstart", "xend")])
230239
yDom <- as.numeric(domainInfo[i, c("yend", "ystart")])

tests/testthat/test-plotly-subplot.R

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,39 @@ test_that("images accumulate and paper coordinates are repositioned", {
199199
expect_true(imgs[[2]]$sizey == 0.5)
200200
})
201201

202+
test_that("images axis references are remapped", {
203+
204+
r <- as.raster(matrix(hcl(0, 80, seq(50, 80, 10)), nrow = 4, ncol = 5))
205+
206+
# embed the raster as an image
207+
p <- plot_ly(x = 1, y = 1) %>%
208+
layout(
209+
images = list(list(
210+
source = raster2uri(r),
211+
sizing = "fill",
212+
xref = "x",
213+
yref = "y",
214+
x = 0,
215+
y = 0,
216+
sizex = 1,
217+
sizey = 1,
218+
xanchor = "left",
219+
yanchor = "bottom"
220+
))
221+
)
222+
223+
s <- subplot(p, p, nrows = 1, margin = 0.02)
224+
imgs <- plotly_build(s)$x$layout$images
225+
expect_true(imgs[[1]]$x == 0)
226+
expect_true(imgs[[1]]$y == 0)
227+
expect_true(imgs[[1]]$xref == "x")
228+
expect_true(imgs[[1]]$yref == "y")
229+
expect_true(imgs[[2]]$x == 0)
230+
expect_true(imgs[[2]]$y == 0)
231+
expect_true(imgs[[2]]$xref == "x2")
232+
expect_true(imgs[[2]]$yref == "y2")
233+
})
234+
202235

203236

204237
test_that("geo+cartesian behaves", {

0 commit comments

Comments
 (0)