File: layers2layout.R

package info (click to toggle)
r-cran-plotly 4.10.4%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 30,636 kB
  • sloc: javascript: 195,272; sh: 24; makefile: 6
file content (29 lines) | stat: -rw-r--r-- 1,077 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# convert layout-specific geoms/layers 
layers2layout <- function(gglayout, layers, layout) {
  geoms <- sapply(layers, function(x) class(x[["geom"]])[1])
  RasterGeom <- which(geoms %in% "GeomRasterAnn")
  for (i in RasterGeom) {
    params <- layers[[i]]$computed_geom_params %||% layers[[i]]$geom_params
    for (j in seq_len(nrow(layout))) {
      lay <- layout[j, ]
      
      img <- list(
        source = raster2uri(params$raster),
        # TODO: ask plotly.js to implement explicit placement between traces?
        layer = if (RasterGeom / length(geoms) > 0.5) "above" else "below",
        xref = sub("axis", "", lay[["xaxis"]]), 
        yref = sub("axis", "", lay[["yaxis"]]), 
        x = params$xmin, 
        xanchor = "left",
        sizex = with(params, abs(xmax - xmin)),
        y = params$ymin, 
        yanchor = "bottom",
        sizey = with(params, abs(ymax - ymin)),
        sizing = "stretch"
      )
      gglayout$images <- c(gglayout$images, list(img))
    }
  }
  # TODO: maybe we could support a subset of grobs in GeomCustomAnn?
  gglayout
}