11# ' @inheritParams grid::viewport
22# ' @inheritDotParams grid::viewport -x -y -width -height
3+ # ' @param resize A logical value. If `TRUE`, the viewport will be resized to
4+ # ' accommodate the underlying gtable. This only applies when both the viewport
5+ # ' width/height and the underlying gtable widths/heights are specified using
6+ # ' absolute units. If `TRUE` and the viewport width/height is `NA`, the
7+ # ' width/height will be set to match the gtable's widths/heights. Otherwise,
8+ # ' the width/height will be set to `unit(1, "npc")`. See
9+ # ' [`absolute.size()`][grid::absolute.size] for absolute unit.
310# ' @return
411# ' - `free_vp`: A modified version of `plot` with a `ggalign_free_vp` class.
512# ' @export
613# ' @rdname free
7- free_vp <- function (plot , x = 0.5 , y = 0.5 , width = NA , height = NA , ... ) {
14+ free_vp <- function (plot , x = 0.5 , y = 0.5 , width = NA , height = NA , ... ,
15+ resize = TRUE ) {
816 UseMethod(" free_vp" )
917}
1018
1119# ' @importFrom grid viewport
1220# ' @export
1321free_vp.default <- function (plot , x = 0.5 , y = 0.5 ,
14- width = NA , height = NA , ... ) {
15- attr( plot , " ggalign_free_vp " ) <- viewport(
16- x = x , y = y , width = width , height = height , ... ,
17- )
22+ width = NA , height = NA , ... ,
23+ resize = TRUE ) {
24+ vp <- viewport( x = x , y = y , width = width , height = height , ... , )
25+ attr( plot , " ggalign_free_vp " ) <- list ( vp = vp , resize = resize )
1826 add_class(plot , " ggalign_free_vp" )
1927}
2028
2129# ' @importFrom grid viewport
2230# ' @export
2331free_vp.ggalign_free_vp <- function (plot , x = 0.5 , y = 0.5 ,
24- width = NA , height = NA , ... ) {
25- attr( plot , " ggalign_free_vp " ) <- viewport(
26- x = x , y = y , width = width , height = height , ... ,
27- )
32+ width = NA , height = NA , ... ,
33+ resize = TRUE ) {
34+ vp <- viewport( x = x , y = y , width = width , height = height , ... , )
35+ attr( plot , " ggalign_free_vp " ) <- list ( vp = vp , resize = resize )
2836 plot
2937}
3038
@@ -40,23 +48,37 @@ patch.ggalign_free_vp <- function(x) {
4048 vp = attr(x , " ggalign_free_vp" , exact = TRUE ),
4149 place = function (self , gtable , gt , t , l , b , r , i , bg_z , plot_z ) {
4250 if (is.grob(gt )) {
43- vp <- self $ vp
44-
45- if (all(is_absolute_unit(widths <- .subset2(gt , " widths" )))) {
46- vp $ width <- sum(widths )
47- } else if (! is.na(as.numeric(vp $ width ))) {
48- # we guess the width from the gtable
49- vp $ width <- max(vp $ width , sum(widths ))
50- } else {
51- vp $ width <- unit(1 , " npc" )
52- }
53- if (all(is_absolute_unit(heights <- .subset2(gt , " heights" )))) {
54- vp $ height <- sum(heights )
55- } else if (! is.na(as.numeric(vp $ height ))) {
56- # we guess the height from the gtable
57- vp $ height <- max(vp $ height , sum(heights ))
51+ vp <- self $ vp $ vp
52+ widths <- .subset2(gt , " widths" )
53+ heights <- .subset2(gt , " heights" )
54+ if (isTRUE(self $ vp $ resize )) {
55+ if (is.na(as.numeric(vp $ width ))) {
56+ # we guess the width from the gtable
57+ if (all(is_absolute_unit(widths ))) {
58+ vp $ width <- sum(widths )
59+ } else {
60+ vp $ width <- unit(1 , " npc" )
61+ }
62+ } else if (is_absolute_unit(vp $ width )) {
63+ vp $ width <- max(vp $ width , sum(widths ))
64+ }
65+ if (is.na(as.numeric(vp $ height ))) {
66+ # we guess the height from the gtable
67+ if (all(is_absolute_unit(heights ))) {
68+ vp $ height <- sum(heights )
69+ } else {
70+ vp $ height <- unit(1 , " npc" )
71+ }
72+ } else if (is_absolute_unit(vp $ height )) {
73+ vp $ height <- max(vp $ height , sum(heights ))
74+ }
5875 } else {
59- vp $ height <- unit(1 , " npc" )
76+ if (is.na(as.numeric(vp $ width ))) {
77+ vp $ width <- unit(1 , " npc" )
78+ }
79+ if (is.na(as.numeric(vp $ height ))) {
80+ vp $ height <- unit(1 , " npc" )
81+ }
6082 }
6183 gt <- editGrob(gt , vp = vp )
6284 }
0 commit comments