Skip to content

Commit 3764c97

Browse files
committed
Try to preserve aspect ratio
1 parent a837ef5 commit 3764c97

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

R/facet-.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,21 @@ Facet <- ggproto("Facet", NULL,
734734
rows <- panel_rows(table)
735735
cols <- panel_cols(table)
736736

737+
if (isTRUE(table$respect) && # Has fixed aspect ratio
738+
xor(is.null(new_widths), is.null(new_heights)) && # One dimension is set
739+
nrow(rows) == 1 && nrow(cols) == 1) { # Just a single panel
740+
old_width <- table$widths[cols$l]
741+
old_height <- table$heights[rows$t]
742+
# Try to reconstruct aspect ratio from panel size
743+
# We shouldn't attempt this with mixed or compound (e.g. "sum") units
744+
if (identical(unitType(old_width), "null") &&
745+
identical(unitType(old_height), "null")) {
746+
ratio <- as.numeric(old_height) / as.numeric(old_width)
747+
new_widths <- new_widths %||% (new_heights / ratio)
748+
new_heights <- new_heights %||% (new_widths * ratio)
749+
}
750+
}
751+
737752
if (length(new_widths) == 1L && nrow(cols) > 1L) {
738753
# Get total size of non-panel widths in between panels
739754
extra <- setdiff(seq(min(cols$l), max(cols$r)), union(cols$l, cols$r))

0 commit comments

Comments
 (0)