From 5ec0f029d2067c0ca7d26a1c92358f44ed39e57f Mon Sep 17 00:00:00 2001 From: Carson Sievert Date: Mon, 7 Jan 2019 16:18:19 -0600 Subject: [PATCH 1/5] fix xref/yref bumping logic in subplot() when number of traces is greater than number of annotations/images -- also, add support for shapes --- R/subplots.R | 29 ++-- tests/testthat/test-plotly-subplot.R | 226 ++++++++++++++++++++++----- 2 files changed, 205 insertions(+), 50 deletions(-) diff --git a/R/subplots.R b/R/subplots.R index 40c8106119..81e32fa3d5 100644 --- a/R/subplots.R +++ b/R/subplots.R @@ -207,30 +207,31 @@ subplot <- function(..., nrows = 1, widths = NULL, heights = NULL, margin = 0.02 map <- xMap[xMap %in% sub("x", "xaxis", yAxes[[i]][[j]]$anchor %||% "x")] yAxes[[i]][[j]]$anchor <- sub("axis", "", names(map)) } - # map trace xaxis/yaxis/geo attributes + for (key in c("geo", "subplot", "xaxis", "yaxis")) { + # bump trace axis references oldAnchors <- unlist(lapply(traces[[i]], "[[", key)) if (!length(oldAnchors)) next axisMap <- if (key == "yaxis") yMap else xMap axisMap <- setNames(sub("axis", "", axisMap), sub("axis", "", names(axisMap))) newAnchors <- names(axisMap)[match(oldAnchors, axisMap)] traces[[i]] <- Map(function(tr, a) { tr[[key]] <- a; tr }, traces[[i]], newAnchors) - # also map annotation and image xaxis/yaxis references - # TODO: do this for shapes as well? + + # bump annotation, image, shape xref/yref + # (none of these layout components have geo/subplot support) ref <- list(xaxis = "xref", yaxis = "yref")[[key]] if (is.null(ref)) next - if (length(annotations[[i]])) { - annotations[[i]] <- Map(function(x, y) { - if (!identical(x[[ref]], "paper")) x[[ref]] <- y - x - }, annotations[[i]], newAnchors) - } - if (length(images[[i]])) { - images[[i]] <- Map(function(x, y) { - if (!identical(x[[ref]], "paper")) x[[ref]] <- y - x - }, images[[i]], newAnchors) + bump_axis_ref <- function(obj, ref_default = sub("ref", "", ref)) { + # TODO: throw error/warning if ref_default doesn't match axisMap? + obj[[ref]] <- obj[[ref]] %||% ref_default + if (identical(obj[[ref]], "paper")) return(obj) + refIdx <- match(obj[[ref]], axisMap) + if (!is.na(refIdx)) obj[[ref]] <- names(axisMap)[refIdx] + obj } + annotations[[i]] <- lapply(annotations[[i]], bump_axis_ref) + shapes[[i]] <- lapply(shapes[[i]], bump_axis_ref) + images[[i]] <- lapply(images[[i]], bump_axis_ref, "paper") } diff --git a/tests/testthat/test-plotly-subplot.R b/tests/testthat/test-plotly-subplot.R index 6784ce8f44..01e39d3d1b 100644 --- a/tests/testthat/test-plotly-subplot.R +++ b/tests/testthat/test-plotly-subplot.R @@ -134,40 +134,83 @@ test_that("subplot accepts a list of plots", { expect_true(l$layout[[sub("y", "yaxis", xaxes[[1]]$anchor)]]$domain[1] == 0) }) -# Ignore for now https://github.com/ggobi/ggally/issues/264 test_that("ggplotly understands ggmatrix", { skip_if_not_installed("GGally") L <- expect_doppelganger_built(GGally::ggpairs(iris), "plotly-subplot-ggmatrix") }) -test_that("annotation xref/yref are bumped correctly", { - - p1 <- plot_ly(mtcars) %>% - add_annotations(text = ~cyl, x = ~wt, y = ~mpg) +test_that("annotation paper repositioning", { + p1 <- plot_ly() %>% + add_annotations(text = "foo", x = 0.5, y = 0.5, xref = "paper", yref = "paper") p2 <- plot_ly(mtcars) %>% - add_annotations(text = ~am, x = ~wt, y = ~mpg) - s <- subplot(p1, p2) + add_annotations(text = "bar", x = 0.5, y = 0.5, xref = "paper", yref = "paper") + s <- subplot(p1, p2, margin = 0) ann <- plotly_build(s)$x$layout$annotations + expect_length(ann, 2) - txt <- sapply(ann, "[[", "text") + text <- sapply(ann, "[[", "text") + x <- sapply(ann, "[[", "x") + y <- sapply(ann, "[[", "y") xref <- sapply(ann, "[[", "xref") yref <- sapply(ann, "[[", "yref") - expect_length(ann, 64) - expect_equal(txt, c(mtcars$cyl, mtcars$am)) - expect_equal(xref, rep(c("x", "x2"), each = 32)) - expect_equal(yref, rep(c("y", "y2"), each = 32)) + expect_equal(x, c(0.25, 0.75)) + expect_equal(y, c(0.5, 0.5)) + expect_equal(xref, rep("paper", 2)) + expect_equal(yref, rep("paper", 2)) +}) + +test_that("shape paper repositioning", { - s2 <- subplot(p1, p2, shareY = TRUE) - ann2 <- plotly_build(s2)$x$layout$annotations + p1 <- plot_ly(mtcars) %>% + layout( + shapes = ~list( + type = "rect", + x0 = 0.25, + x1 = 0.75, + y0 = 0.25, + y1 = 0.75, + xref = "paper", + yref = "paper", + fillcolor = "red" + ) + ) + p2 <- plot_ly(mtcars) %>% + layout( + shapes = ~list( + type = "line", + type = "rect", + x0 = 0.25, + x1 = 0.75, + y0 = 0.25, + y1 = 0.75, + xref = "paper", + yref = "paper", + line = list(color = "blue") + ) + ) - xref2 <- sapply(ann2, "[[", "xref") - yref2 <- sapply(ann2, "[[", "yref") - expect_equal(xref2, rep(c("x", "x2"), each = 32)) - expect_equal(yref2, rep(c("y", "y"), each = 32)) + s <- subplot(p1, p2) + shapes <- plotly_build(s)$x$layout$shapes + expect_length(shapes, 2) + + x0 <- sapply(shapes, "[[", "x0") + x1 <- sapply(shapes, "[[", "x1") + y0 <- sapply(shapes, "[[", "y0") + y1 <- sapply(shapes, "[[", "y1") + xref <- sapply(shapes, "[[", "xref") + yref <- sapply(shapes, "[[", "yref") + + expect_equal(x0, c(0.12, 0.64)) + expect_equal(x1, c(0.36, 0.88)) + expect_equal(y0, rep(0.25, 2)) + expect_equal(y1, rep(0.75, 2)) + expect_equal(xref, rep("paper", 2)) + expect_equal(yref, rep("paper", 2)) }) -test_that("images accumulate and paper coordinates are repositioned", { + +test_that("image paper repositioning", { skip_if_not_installed("png") r <- as.raster(matrix(hcl(0, 80, seq(50, 80, 10)), nrow = 4, ncol = 5)) @@ -191,17 +234,124 @@ test_that("images accumulate and paper coordinates are repositioned", { s <- subplot(p, p, nrows = 1, margin = 0.02) imgs <- plotly_build(s)$x$layout$images - expect_true(imgs[[1]]$x == 0) - expect_true(imgs[[1]]$y == 0) - expect_true(imgs[[1]]$sizex == 0.24) - expect_true(imgs[[1]]$sizey == 0.5) - expect_true(imgs[[2]]$x == 0.52) - expect_true(imgs[[2]]$y == 0) - expect_true(imgs[[2]]$sizex == 0.24) - expect_true(imgs[[2]]$sizey == 0.5) + + expect_length(imgs, 2) + + x <- sapply(imgs, "[[", "x") + y <- sapply(imgs, "[[", "y") + sizex <- sapply(imgs, "[[", "sizex") + sizey <- sapply(imgs, "[[", "sizey") + + expect_equal(x, c(0, 0.52)) + expect_equal(y, c(0, 0)) + expect_equal(sizex, rep(0.24, 2)) + expect_equal(sizey, rep(0.5, 2)) +}) + +test_that("annotation xref/yref bumping", { + + p1 <- plot_ly(mtcars) %>% + add_annotations(text = ~cyl, x = ~wt, y = ~mpg) + p2 <- plot_ly(mtcars) %>% + add_annotations(text = ~am, x = ~wt, y = ~mpg) + s <- subplot(p1, p2) + ann <- plotly_build(s)$x$layout$annotations + + txt <- sapply(ann, "[[", "text") + xref <- sapply(ann, "[[", "xref") + yref <- sapply(ann, "[[", "yref") + + expect_length(ann, 64) + expect_equal(txt, c(mtcars$cyl, mtcars$am)) + expect_equal(xref, rep(c("x", "x2"), each = 32)) + expect_equal(yref, rep(c("y", "y2"), each = 32)) + + s2 <- subplot(p1, p2, shareY = TRUE) + ann2 <- plotly_build(s2)$x$layout$annotations + + xref2 <- sapply(ann2, "[[", "xref") + yref2 <- sapply(ann2, "[[", "yref") + expect_equal(xref2, rep(c("x", "x2"), each = 32)) + expect_equal(yref2, rep(c("y", "y"), each = 32)) + + # now, with more traces than annotations + # https://github.com/ropensci/plotly/issues/1444 + p1 <- plot_ly() %>% + add_markers(x = 1, y = 1) %>% + add_markers(x = 2, y = 2) %>% + add_annotations(text = "foo", x = 1.5, y = 1.5) + p2 <- plot_ly() %>% + add_markers(x = 1, y = 1) %>% + add_markers(x = 2, y = 2) %>% + add_annotations(text = "bar", x = 1.5, y = 1.5) + s <- subplot(p1, p2) + ann <- plotly_build(s)$x$layout$annotations + + txt <- sapply(ann, "[[", "text") + xref <- sapply(ann, "[[", "xref") + yref <- sapply(ann, "[[", "yref") + + expect_length(ann, 2) + expect_equal(txt, c("foo", "bar")) + expect_equal(xref, c("x", "x2")) + expect_equal(yref, c("y", "y2")) + + s2 <- subplot(p1, p2, shareY = TRUE) + ann2 <- plotly_build(s2)$x$layout$annotations + + xref2 <- sapply(ann2, "[[", "xref") + yref2 <- sapply(ann2, "[[", "yref") + expect_equal(xref2, c("x", "x2")) + expect_equal(yref2, c("y", "y")) +}) + +test_that("shape xref/yref bumping", { + + p1 <- plot_ly(mtcars) %>% + layout( + shapes = ~list( + type = "rect", + x0 = min(cyl), + x1 = max(cyl), + y0 = min(am), + y1 = max(am), + fillcolor = "red" + ) + ) + p2 <- plot_ly(mtcars) %>% + layout( + shapes = ~list( + type = "line", + x0 = min(cyl), + x1 = max(cyl), + y0 = min(am), + y1 = max(am), + line = list(color = "blue") + ) + ) + + s <- subplot(p1, p2) + shapes <- plotly_build(s)$x$layout$shapes + expect_length(shapes, 2) + + types <- sapply(shapes, "[[", "type") + expect_equal(types, c("rect", "line")) + + xref <- sapply(shapes, "[[", "xref") + yref <- sapply(shapes, "[[", "yref") + expect_equal(xref, c("x", "x2")) + expect_equal(yref, c("y", "y2")) + + s2 <- subplot(p1, p2, shareY = TRUE) + shapes2 <- plotly_build(s2)$x$layout$shapes + + xref2 <- sapply(shapes2, "[[", "xref") + yref2 <- sapply(shapes2, "[[", "yref") + expect_equal(xref2, c("x", "x2")) + expect_equal(yref2, c("y", "y")) }) -test_that("images axis references are remapped", { +test_that("image xref/yref bumping", { skip_if_not_installed("png") r <- as.raster(matrix(hcl(0, 80, seq(50, 80, 10)), nrow = 4, ncol = 5)) @@ -225,14 +375,18 @@ test_that("images axis references are remapped", { s <- subplot(p, p, nrows = 1, margin = 0.02) imgs <- plotly_build(s)$x$layout$images - expect_true(imgs[[1]]$x == 0) - expect_true(imgs[[1]]$y == 0) - expect_true(imgs[[1]]$xref == "x") - expect_true(imgs[[1]]$yref == "y") - expect_true(imgs[[2]]$x == 0) - expect_true(imgs[[2]]$y == 0) - expect_true(imgs[[2]]$xref == "x2") - expect_true(imgs[[2]]$yref == "y2") + + expect_length(imgs, 2) + + x <- sapply(imgs, "[[", "x") + y <- sapply(imgs, "[[", "y") + xref <- sapply(imgs, "[[", "xref") + yref <- sapply(imgs, "[[", "yref") + + expect_equal(x, c(0, 0)) + expect_equal(y, c(0, 0)) + expect_equal(xref, c("x", "x2")) + expect_equal(yref, c("y", "y2")) }) From dd5d77fc69e49ef9c3e3f9ad26ebf39f0c231810 Mon Sep 17 00:00:00 2001 From: Carson Sievert Date: Mon, 7 Jan 2019 17:36:42 -0600 Subject: [PATCH 2/5] update news --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 0ce47d2d8c..e8659f77de 100644 --- a/NEWS.md +++ b/NEWS.md @@ -15,7 +15,7 @@ ## BUG FIXES -* `subplot()` now bumps annotation `xref`/`yref` anchors correctly (#1181). +* `subplot()` now automatically bumps non-paper `xref`/`yref` references sensibly for annotations, images, and shapes (#1181). * `subplot()` now accumulates images, repositions paper coordinates, and reanchors axis references (#1332). * `event_data("plotly_selected")` is no longer too eager to clear. That is, it is no longer set to `NULL` when clicking on a plot *after* triggering the "plotly_selected" event (#1121) (#1122). * The colorscale generated via the `color` argument in `plot_ly()` now uses an evenly spaced grid of values instead of quantiles (#1308). From 3b158f1b5a6c9a2cc7354a518b61b96a10db98e2 Mon Sep 17 00:00:00 2001 From: Carson Sievert Date: Mon, 7 Jan 2019 17:50:54 -0600 Subject: [PATCH 3/5] better news bullet --- NEWS.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index e8659f77de..b93ad02989 100644 --- a/NEWS.md +++ b/NEWS.md @@ -15,8 +15,9 @@ ## BUG FIXES -* `subplot()` now automatically bumps non-paper `xref`/`yref` references sensibly for annotations, images, and shapes (#1181). -* `subplot()` now accumulates images, repositions paper coordinates, and reanchors axis references (#1332). +* `subplot()` now works much better with annotations, images, and shapes: + - When `xref`/`yref` references an x/y axis these references are bumped accordingly (#1181). + - When `xref`/`yref` references paper coordinates, these coordinates are updated accordingly (#1332). * `event_data("plotly_selected")` is no longer too eager to clear. That is, it is no longer set to `NULL` when clicking on a plot *after* triggering the "plotly_selected" event (#1121) (#1122). * The colorscale generated via the `color` argument in `plot_ly()` now uses an evenly spaced grid of values instead of quantiles (#1308). * When using **shinytest** to test a **shiny** that contains **plotly** graph, false positive differences are no longer reported (rstudio/shinytest#174). From d10b12a7825ec04a667bd6976db3bd2aa8bb22ef Mon Sep 17 00:00:00 2001 From: Carson Sievert Date: Tue, 8 Jan 2019 11:31:43 -0600 Subject: [PATCH 4/5] add visual tests for annotation/shape/image reposition/axis adjustments and validate new ggmatrix baseline --- tests/figs/deps.txt | 4 +++- .../figs/subplot/plotly-subplot-ggmatrix.svg | 2 +- .../subplot-bump-axis-annotation-shared.svg | 1 + ...lot-bump-axis-annotation-traces-shared.svg | 1 + .../subplot-bump-axis-annotation-traces.svg | 1 + .../subplot/subplot-bump-axis-annotation.svg | 1 + .../figs/subplot/subplot-bump-axis-image.svg | 1 + .../subplot-bump-axis-shape-shared.svg | 1 + .../figs/subplot/subplot-bump-axis-shape.svg | 1 + .../subplot/subplot-reposition-annotation.svg | 1 + .../figs/subplot/subplot-reposition-image.svg | 1 + .../figs/subplot/subplot-reposition-shape.svg | 1 + tests/testthat/test-plotly-subplot.R | 21 ++++++++++--------- 13 files changed, 25 insertions(+), 12 deletions(-) create mode 100644 tests/figs/subplot/subplot-bump-axis-annotation-shared.svg create mode 100644 tests/figs/subplot/subplot-bump-axis-annotation-traces-shared.svg create mode 100644 tests/figs/subplot/subplot-bump-axis-annotation-traces.svg create mode 100644 tests/figs/subplot/subplot-bump-axis-annotation.svg create mode 100644 tests/figs/subplot/subplot-bump-axis-image.svg create mode 100644 tests/figs/subplot/subplot-bump-axis-shape-shared.svg create mode 100644 tests/figs/subplot/subplot-bump-axis-shape.svg create mode 100644 tests/figs/subplot/subplot-reposition-annotation.svg create mode 100644 tests/figs/subplot/subplot-reposition-image.svg create mode 100644 tests/figs/subplot/subplot-reposition-shape.svg diff --git a/tests/figs/deps.txt b/tests/figs/deps.txt index ae13abdcb7..059d357267 100644 --- a/tests/figs/deps.txt +++ b/tests/figs/deps.txt @@ -1 +1,3 @@ -vdiffr-svg-engine: 0.9000 +- vdiffr-svg-engine: 1.0 +- vdiffr: 0.3.0 +- freetypeharfbuzz: 0.2.5 diff --git a/tests/figs/subplot/plotly-subplot-ggmatrix.svg b/tests/figs/subplot/plotly-subplot-ggmatrix.svg index 5af6039154..066ad28f0d 100644 --- a/tests/figs/subplot/plotly-subplot-ggmatrix.svg +++ b/tests/figs/subplot/plotly-subplot-ggmatrix.svg @@ -1 +1 @@ -0.02.55.07.50.02.55.07.556780.02.55.07.50.00.51.01.52.02.52462.02.53.03.54.04.50.00.10.20.30.42.02.53.03.54.04.5Corr:-0.118246Corr:-0.428Corr:0.8720.00.51.01.52.02.5Corr:0.963Corr:-0.366Corr:0.818setosaversicolorvirginicaSepal.LengthSepal.WidthPetal.LengthPetal.WidthSpeciesPetal.WidthPetal.LengthSepal.WidthSepal.Lengthsetosaversicolorvirginicasetosaversicolorvirginicasetosasetosaversicolorvirginicasetosaversicolorvirginicasetosasetosaversicolorvirginicasetosaversicolorvirginicasetosasetosaversicolorvirginicasetosaversicolorvirginicasetosa +0.02.55.07.50.02.55.07.556780.02.55.07.50.00.51.01.52.02.52462.02.53.03.54.04.50.00.10.20.30.42.02.53.03.54.04.5Corr:-0.118246Corr:-0.428Corr:0.8720.00.51.01.52.02.5Corr:0.963Corr:-0.366Corr:0.818setosaversicolorvirginicaSepal.LengthSepal.WidthPetal.LengthPetal.WidthSpeciesPetal.WidthPetal.LengthSepal.WidthSepal.Lengthsetosaversicolorvirginicasetosaversicolorvirginicasetosaversicolorvirginicasetosaversicolorvirginica diff --git a/tests/figs/subplot/subplot-bump-axis-annotation-shared.svg b/tests/figs/subplot/subplot-bump-axis-annotation-shared.svg new file mode 100644 index 0000000000..2a26f1b6a2 --- /dev/null +++ b/tests/figs/subplot/subplot-bump-axis-annotation-shared.svg @@ -0,0 +1 @@ +2345152025303523456646868446688888844448888444868411100000000000000111000001111111 diff --git a/tests/figs/subplot/subplot-bump-axis-annotation-traces-shared.svg b/tests/figs/subplot/subplot-bump-axis-annotation-traces-shared.svg new file mode 100644 index 0000000000..454b0aca8f --- /dev/null +++ b/tests/figs/subplot/subplot-bump-axis-annotation-traces-shared.svg @@ -0,0 +1 @@ +11.5211.21.41.61.8211.52trace 0trace 1trace 2trace 3foobar diff --git a/tests/figs/subplot/subplot-bump-axis-annotation-traces.svg b/tests/figs/subplot/subplot-bump-axis-annotation-traces.svg new file mode 100644 index 0000000000..c75493bdb0 --- /dev/null +++ b/tests/figs/subplot/subplot-bump-axis-annotation-traces.svg @@ -0,0 +1 @@ +11.5211.21.41.61.8211.5211.21.41.61.82trace 0trace 1trace 2trace 3foobar diff --git a/tests/figs/subplot/subplot-bump-axis-annotation.svg b/tests/figs/subplot/subplot-bump-axis-annotation.svg new file mode 100644 index 0000000000..cdb8f141ce --- /dev/null +++ b/tests/figs/subplot/subplot-bump-axis-annotation.svg @@ -0,0 +1 @@ +23451520253035234515202530356646868446688888844448888444868411100000000000000111000001111111 diff --git a/tests/figs/subplot/subplot-bump-axis-image.svg b/tests/figs/subplot/subplot-bump-axis-image.svg new file mode 100644 index 0000000000..5fe892aeaf --- /dev/null +++ b/tests/figs/subplot/subplot-bump-axis-image.svg @@ -0,0 +1 @@ +00.511.5200.511.5200.511.5200.511.52trace 0trace 1 diff --git a/tests/figs/subplot/subplot-bump-axis-shape-shared.svg b/tests/figs/subplot/subplot-bump-axis-shape-shared.svg new file mode 100644 index 0000000000..0b12293cda --- /dev/null +++ b/tests/figs/subplot/subplot-bump-axis-shape-shared.svg @@ -0,0 +1 @@ +4567800.20.40.60.8145678 diff --git a/tests/figs/subplot/subplot-bump-axis-shape.svg b/tests/figs/subplot/subplot-bump-axis-shape.svg new file mode 100644 index 0000000000..a6176c3bc0 --- /dev/null +++ b/tests/figs/subplot/subplot-bump-axis-shape.svg @@ -0,0 +1 @@ +4567800.20.40.60.814567800.20.40.60.81 diff --git a/tests/figs/subplot/subplot-reposition-annotation.svg b/tests/figs/subplot/subplot-reposition-annotation.svg new file mode 100644 index 0000000000..dfc5666565 --- /dev/null +++ b/tests/figs/subplot/subplot-reposition-annotation.svg @@ -0,0 +1 @@ +0246−1012340246−101234foobar diff --git a/tests/figs/subplot/subplot-reposition-image.svg b/tests/figs/subplot/subplot-reposition-image.svg new file mode 100644 index 0000000000..4c23e1aa5a --- /dev/null +++ b/tests/figs/subplot/subplot-reposition-image.svg @@ -0,0 +1 @@ +00.511.5200.511.5200.511.5200.511.52trace 0trace 1 diff --git a/tests/figs/subplot/subplot-reposition-shape.svg b/tests/figs/subplot/subplot-reposition-shape.svg new file mode 100644 index 0000000000..e4ba416582 --- /dev/null +++ b/tests/figs/subplot/subplot-reposition-shape.svg @@ -0,0 +1 @@ +0246−1012340246−101234 diff --git a/tests/testthat/test-plotly-subplot.R b/tests/testthat/test-plotly-subplot.R index 01e39d3d1b..20e3fc03cb 100644 --- a/tests/testthat/test-plotly-subplot.R +++ b/tests/testthat/test-plotly-subplot.R @@ -144,8 +144,9 @@ test_that("annotation paper repositioning", { add_annotations(text = "foo", x = 0.5, y = 0.5, xref = "paper", yref = "paper") p2 <- plot_ly(mtcars) %>% add_annotations(text = "bar", x = 0.5, y = 0.5, xref = "paper", yref = "paper") + s <- subplot(p1, p2, margin = 0) - ann <- plotly_build(s)$x$layout$annotations + ann <- expect_doppelganger_built(s, "subplot-reposition-annotation")$layout$annotations expect_length(ann, 2) text <- sapply(ann, "[[", "text") @@ -191,7 +192,7 @@ test_that("shape paper repositioning", { ) s <- subplot(p1, p2) - shapes <- plotly_build(s)$x$layout$shapes + shapes <- expect_doppelganger_built(s, "subplot-reposition-shape")$layout$shapes expect_length(shapes, 2) x0 <- sapply(shapes, "[[", "x0") @@ -233,7 +234,7 @@ test_that("image paper repositioning", { ) s <- subplot(p, p, nrows = 1, margin = 0.02) - imgs <- plotly_build(s)$x$layout$images + imgs <- expect_doppelganger_built(s, "subplot-reposition-image")$layout$images expect_length(imgs, 2) @@ -255,7 +256,7 @@ test_that("annotation xref/yref bumping", { p2 <- plot_ly(mtcars) %>% add_annotations(text = ~am, x = ~wt, y = ~mpg) s <- subplot(p1, p2) - ann <- plotly_build(s)$x$layout$annotations + ann <- expect_doppelganger_built(s, "subplot-bump-axis-annotation")$layout$annotations txt <- sapply(ann, "[[", "text") xref <- sapply(ann, "[[", "xref") @@ -267,7 +268,7 @@ test_that("annotation xref/yref bumping", { expect_equal(yref, rep(c("y", "y2"), each = 32)) s2 <- subplot(p1, p2, shareY = TRUE) - ann2 <- plotly_build(s2)$x$layout$annotations + ann2 <- expect_doppelganger_built(s2, "subplot-bump-axis-annotation-shared")$layout$annotations xref2 <- sapply(ann2, "[[", "xref") yref2 <- sapply(ann2, "[[", "yref") @@ -285,7 +286,7 @@ test_that("annotation xref/yref bumping", { add_markers(x = 2, y = 2) %>% add_annotations(text = "bar", x = 1.5, y = 1.5) s <- subplot(p1, p2) - ann <- plotly_build(s)$x$layout$annotations + ann <- expect_doppelganger_built(s, "subplot-bump-axis-annotation-traces")$layout$annotations txt <- sapply(ann, "[[", "text") xref <- sapply(ann, "[[", "xref") @@ -297,7 +298,7 @@ test_that("annotation xref/yref bumping", { expect_equal(yref, c("y", "y2")) s2 <- subplot(p1, p2, shareY = TRUE) - ann2 <- plotly_build(s2)$x$layout$annotations + ann2 <- expect_doppelganger_built(s2, "subplot-bump-axis-annotation-traces-shared")$layout$annotations xref2 <- sapply(ann2, "[[", "xref") yref2 <- sapply(ann2, "[[", "yref") @@ -331,7 +332,7 @@ test_that("shape xref/yref bumping", { ) s <- subplot(p1, p2) - shapes <- plotly_build(s)$x$layout$shapes + shapes <- expect_doppelganger_built(s, "subplot-bump-axis-shape")$layout$shapes expect_length(shapes, 2) types <- sapply(shapes, "[[", "type") @@ -343,7 +344,7 @@ test_that("shape xref/yref bumping", { expect_equal(yref, c("y", "y2")) s2 <- subplot(p1, p2, shareY = TRUE) - shapes2 <- plotly_build(s2)$x$layout$shapes + shapes2 <- expect_doppelganger_built(s2, "subplot-bump-axis-shape-shared")$layout$shapes xref2 <- sapply(shapes2, "[[", "xref") yref2 <- sapply(shapes2, "[[", "yref") @@ -374,7 +375,7 @@ test_that("image xref/yref bumping", { ) s <- subplot(p, p, nrows = 1, margin = 0.02) - imgs <- plotly_build(s)$x$layout$images + imgs <- expect_doppelganger_built(s, "subplot-bump-axis-image")$layout$images expect_length(imgs, 2) From 1cacb486d07b09ba6c7cfb704a3ab28969df4ae3 Mon Sep 17 00:00:00 2001 From: Carson Sievert Date: Thu, 10 Jan 2019 18:57:02 -0600 Subject: [PATCH 5/5] take the first match --- R/subplots.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/subplots.R b/R/subplots.R index 81e32fa3d5..0909ce3e17 100644 --- a/R/subplots.R +++ b/R/subplots.R @@ -226,7 +226,7 @@ subplot <- function(..., nrows = 1, widths = NULL, heights = NULL, margin = 0.02 obj[[ref]] <- obj[[ref]] %||% ref_default if (identical(obj[[ref]], "paper")) return(obj) refIdx <- match(obj[[ref]], axisMap) - if (!is.na(refIdx)) obj[[ref]] <- names(axisMap)[refIdx] + if (!is.na(refIdx)) obj[[ref]] <- names(axisMap)[refIdx][1] obj } annotations[[i]] <- lapply(annotations[[i]], bump_axis_ref)