Skip to content

Commit 4caa068

Browse files
committed
subplot() now bumps annotation xref/yref correctly, fixes #1181
1 parent fcb859f commit 4caa068

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

R/subplots.R

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,11 @@ subplot <- function(..., nrows = 1, widths = NULL, heights = NULL, margin = 0.02
214214
axisMap <- setNames(sub("axis", "", axisMap), sub("axis", "", names(axisMap)))
215215
newAnchors <- names(axisMap)[match(oldAnchors, axisMap)]
216216
traces[[i]] <- Map(function(tr, a) { tr[[key]] <- a; tr }, traces[[i]], newAnchors)
217+
annAnchor <- list(xaxis = "xref", yaxis = "yref")[[key]]
218+
if (is.null(annAnchor)) next
219+
annotations[[i]] <- Map(function(ann, a) { ann[[annAnchor]] <- a; ann}, annotations[[i]], newAnchors)
217220
}
221+
218222
# rescale domains according to the tabular layout
219223
xDom <- as.numeric(domainInfo[i, c("xstart", "xend")])
220224
yDom <- as.numeric(domainInfo[i, c("yend", "ystart")])
@@ -391,14 +395,16 @@ reposition <- function(obj, domains) {
391395
o <- obj[[i]]
392396
# TODO: this implementation currently assumes xref/yref == "paper"
393397
# should we support references to axis objects as well?
394-
for (j in c("x", "x0", "x1")) {
398+
xs <- if (identical(o$xref, "paper")) c("x", "x0", "x1")
399+
for (j in xs) {
395400
if (is.numeric(o[[j]])) {
396401
obj[[i]][[j]] <- scales::rescale(
397402
o[[j]], as.numeric(domains[c("xstart", "xend")]), from = c(0, 1)
398403
)
399404
}
400405
}
401-
for (j in c("y", "y0", "y1")) {
406+
ys <- if (identical(o$xref, "paper")) c("y", "y0", "y1")
407+
for (j in ys) {
402408
if (is.numeric(o[[j]])) {
403409
obj[[i]][[j]] <- scales::rescale(
404410
o[[j]], as.numeric(domains[c("yend", "ystart")]), from = c(0, 1)

tests/testthat/test-plotly-subplot.R

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,38 @@ test_that("subplot accepts a list of plots", {
135135
})
136136

137137
# Ignore for now https://github.com/ggobi/ggally/issues/264
138-
#test_that("ggplotly understands ggmatrix", {
139-
# L <- save_outputs(GGally::ggpairs(iris), "plotly-subplot-ggmatrix")
140-
#})
138+
test_that("ggplotly understands ggmatrix", {
139+
L <- save_outputs(GGally::ggpairs(iris), "plotly-subplot-ggmatrix")
140+
})
141+
142+
test_that("annotation xref/yref are bumped correctly", {
143+
144+
p1 <- plot_ly(mtcars) %>%
145+
add_annotations(text = ~cyl, x = ~wt, y = ~mpg)
146+
p2 <- plot_ly(mtcars) %>%
147+
add_annotations(text = ~am, x = ~wt, y = ~mpg)
148+
s <- subplot(p1, p2)
149+
ann <- plotly_build(s)$x$layout$annotations
150+
151+
txt <- sapply(ann, "[[", "text")
152+
xref <- sapply(ann, "[[", "xref")
153+
yref <- sapply(ann, "[[", "yref")
154+
155+
expect_length(ann, 64)
156+
expect_equal(txt, c(mtcars$cyl, mtcars$am))
157+
expect_equal(xref, rep(c("x", "x2"), each = 32))
158+
expect_equal(yref, rep(c("y", "y2"), each = 32))
159+
160+
s2 <- subplot(p1, p2, shareY = TRUE)
161+
ann2 <- plotly_build(s2)$x$layout$annotations
162+
163+
xref2 <- sapply(ann2, "[[", "xref")
164+
yref2 <- sapply(ann2, "[[", "yref")
165+
expect_equal(xref2, rep(c("x", "x2"), each = 32))
166+
expect_equal(yref2, rep(c("y", "y"), each = 32))
167+
})
168+
169+
141170

142171
test_that("geo+cartesian behaves", {
143172
# specify some map projection/options

0 commit comments

Comments
 (0)