Skip to content

Commit c6311bd

Browse files
authored
Merge pull request #1445 from ropensci/axis-ref-subplot
Improve subplot()'s handling of annotations, shapes, and images
2 parents ef39ac7 + 1cacb48 commit c6311bd

15 files changed

+226
-57
lines changed

NEWS.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515

1616
## BUG FIXES
1717

18-
* `subplot()` now bumps annotation `xref`/`yref` anchors correctly (#1181).
19-
* `subplot()` now accumulates images, repositions paper coordinates, and reanchors axis references (#1332).
18+
* `subplot()` now works much better with annotations, images, and shapes:
19+
- When `xref`/`yref` references an x/y axis these references are bumped accordingly (#1181).
20+
- When `xref`/`yref` references paper coordinates, these coordinates are updated accordingly (#1332).
2021
* `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).
2122
* The colorscale generated via the `color` argument in `plot_ly()` now uses an evenly spaced grid of values instead of quantiles (#1308).
2223
* When using **shinytest** to test a **shiny** that contains **plotly** graph, false positive differences are no longer reported (rstudio/shinytest#174).

R/subplots.R

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -207,30 +207,31 @@ subplot <- function(..., nrows = 1, widths = NULL, heights = NULL, margin = 0.02
207207
map <- xMap[xMap %in% sub("x", "xaxis", yAxes[[i]][[j]]$anchor %||% "x")]
208208
yAxes[[i]][[j]]$anchor <- sub("axis", "", names(map))
209209
}
210-
# map trace xaxis/yaxis/geo attributes
210+
211211
for (key in c("geo", "subplot", "xaxis", "yaxis")) {
212+
# bump trace axis references
212213
oldAnchors <- unlist(lapply(traces[[i]], "[[", key))
213214
if (!length(oldAnchors)) next
214215
axisMap <- if (key == "yaxis") yMap else xMap
215216
axisMap <- setNames(sub("axis", "", axisMap), sub("axis", "", names(axisMap)))
216217
newAnchors <- names(axisMap)[match(oldAnchors, axisMap)]
217218
traces[[i]] <- Map(function(tr, a) { tr[[key]] <- a; tr }, traces[[i]], newAnchors)
218-
# also map annotation and image xaxis/yaxis references
219-
# TODO: do this for shapes as well?
219+
220+
# bump annotation, image, shape xref/yref
221+
# (none of these layout components have geo/subplot support)
220222
ref <- list(xaxis = "xref", yaxis = "yref")[[key]]
221223
if (is.null(ref)) next
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)
224+
bump_axis_ref <- function(obj, ref_default = sub("ref", "", ref)) {
225+
# TODO: throw error/warning if ref_default doesn't match axisMap?
226+
obj[[ref]] <- obj[[ref]] %||% ref_default
227+
if (identical(obj[[ref]], "paper")) return(obj)
228+
refIdx <- match(obj[[ref]], axisMap)
229+
if (!is.na(refIdx)) obj[[ref]] <- names(axisMap)[refIdx][1]
230+
obj
233231
}
232+
annotations[[i]] <- lapply(annotations[[i]], bump_axis_ref)
233+
shapes[[i]] <- lapply(shapes[[i]], bump_axis_ref)
234+
images[[i]] <- lapply(images[[i]], bump_axis_ref, "paper")
234235
}
235236

236237

tests/figs/deps.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
vdiffr-svg-engine: 0.9000
1+
- vdiffr-svg-engine: 1.0
2+
- vdiffr: 0.3.0
3+
- freetypeharfbuzz: 0.2.5

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

Lines changed: 1 addition & 1 deletion
Loading

tests/figs/subplot/subplot-bump-axis-annotation-shared.svg

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

tests/figs/subplot/subplot-bump-axis-annotation-traces.svg

Lines changed: 1 addition & 0 deletions
Loading

tests/figs/subplot/subplot-bump-axis-annotation.svg

Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)