Skip to content

Commit 9afbb5d

Browse files
committed
use grid's viewports to estimate relative sizes for specified height/width. Thanks @alyst
1 parent 7f243f4 commit 9afbb5d

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

R/ggplotly.R

+15
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,19 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all",
114114
# Our internal version of ggplot2::ggplot_build(). Modified from
115115
# https://github.com/hadley/ggplot2/blob/0cd0ba/R/plot-build.r#L18-L92
116116
# ------------------------------------------------------------------------
117+
118+
# create a viewport so we can convert relative sizes correctly
119+
# if height/width not specified, it is estimated from current viewport (npc)
120+
width <- width %||% unitConvert(grid::unit(1, "npc"), "pixels", "width")
121+
height <- height %||% unitConvert(grid::unit(1, "npc"), "pixels", "height")
122+
# assume 96 dots per inch...mm2pixels also does this....
123+
vp <- grid::viewport(
124+
width = grid::unit(width * 72.27 / 96, "points"),
125+
height = grid::unit(height * 72.27 / 96, "points")
126+
)
127+
grid::pushViewport(vp, recording = FALSE)
128+
129+
117130
p <- ggfun("plot_clone")(p)
118131
if (length(p$layers) == 0) {
119132
p <- p + geom_blank()
@@ -668,6 +681,8 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all",
668681
gglayout$width <- width
669682
gglayout$height <- height
670683

684+
grid::popViewport()
685+
671686
l <- list(
672687
data = setNames(traces, NULL),
673688
layout = compact(gglayout),

0 commit comments

Comments
 (0)