@@ -120,6 +120,7 @@ subplot <- function(..., nrows = 1, widths = NULL, heights = NULL, margin = 0.02
120120 traces <- lapply(plots , " [[" , " data" )
121121 layouts <- lapply(plots , " [[" , " layout" )
122122 shapes <- lapply(layouts , " [[" , " shapes" )
123+ images <- lapply(layouts , " [[" , " images" )
123124 annotations <- lapply(layouts , function (x ) {
124125 # keep non axis title annotations (for rescaling)
125126 axes <- vapply(x $ annotations , function (a ) identical(a $ annotationType , " axis" ), logical (1 ))
@@ -263,11 +264,13 @@ subplot <- function(..., nrows = 1, widths = NULL, heights = NULL, margin = 0.02
263264 # reposition shapes and annotations
264265 annotations <- Map(reposition , annotations , split(domainInfo , seq_along(plots )))
265266 shapes <- Map(reposition , shapes , split(domainInfo , seq_along(plots )))
267+ images <- Map(reposition , images , split(domainInfo , seq_along(plots )))
266268 p $ layout $ annotations <- Reduce(c , annotations )
267269 p $ layout $ shapes <- Reduce(c , shapes )
270+ p $ layout $ images <- Reduce(c , images )
268271 # merge non-axis layout stuff
269272 layouts <- lapply(layouts , function (x ) {
270- x [! grepl(" ^[x-y]axis|^geo|^mapbox|annotations|shapes" , names(x ))] %|| % list ()
273+ x [! grepl(" ^[x-y]axis|^geo|^mapbox|annotations|shapes|images " , names(x ))] %|| % list ()
271274 })
272275 if (which_layout != " merge" ) {
273276 if (! is.numeric(which_layout )) warning(" which_layout must be numeric" )
@@ -396,24 +399,27 @@ list2df <- function(x, nms) {
396399# (useful mostly for repositioning annotations/shapes in subplots)
397400reposition <- function (obj , domains ) {
398401 # we need x and y in order to rescale them!
402+ xdom <- as.numeric(domains [c(" xstart" , " xend" )])
403+ ydom <- as.numeric(domains [c(" yend" , " ystart" )])
404+
399405 for (i in seq_along(obj )) {
400406 o <- obj [[i ]]
401- # TODO: this implementation currently assumes xref/yref == "paper"
402- # should we support references to axis objects as well?
403- xs <- if (identical(o $ xref , " paper" )) c(" x" , " x0" , " x1" )
407+ xs <- if (identical(o $ xref , " paper" )) {
408+ if (is.numeric(o $ sizex )) obj [[i ]]$ sizex <- o $ sizex * abs(diff(xdom ))
409+ c(" x" , " x0" , " x1" )
410+ }
404411 for (j in xs ) {
405412 if (is.numeric(o [[j ]])) {
406- obj [[i ]][[j ]] <- scales :: rescale(
407- o [[j ]], as.numeric(domains [c(" xstart" , " xend" )]), from = c(0 , 1 )
408- )
413+ obj [[i ]][[j ]] <- scales :: rescale(o [[j ]], xdom , from = c(0 , 1 ))
409414 }
410415 }
411- ys <- if (identical(o $ xref , " paper" )) c(" y" , " y0" , " y1" )
416+ ys <- if (identical(o $ yref , " paper" )) {
417+ if (is.numeric(o $ sizey )) obj [[i ]]$ sizey <- o $ sizey * abs(diff(ydom ))
418+ c(" y" , " y0" , " y1" )
419+ }
412420 for (j in ys ) {
413421 if (is.numeric(o [[j ]])) {
414- obj [[i ]][[j ]] <- scales :: rescale(
415- o [[j ]], as.numeric(domains [c(" yend" , " ystart" )]), from = c(0 , 1 )
416- )
422+ obj [[i ]][[j ]] <- scales :: rescale(o [[j ]], ydom , from = c(0 , 1 ))
417423 }
418424 }
419425 }
0 commit comments