Skip to content

Commit 44fd15d

Browse files
committed
better detection of relative legend sizes
1 parent 394ee59 commit 44fd15d

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

R/plot-build.R

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -189,16 +189,16 @@ ggplot_gtable.ggplot_built <- function(data) {
189189
position <- "none"
190190
} else {
191191
# these are a bad hack, since it modifies the contents of viewpoint directly...
192-
legend_width <- gtable_width(legend_box)
193-
legend_height <- gtable_height(legend_box)
194192

195-
relative_height <- unitType(legend_height) == "sum"
196-
relative_width <- unitType(legend_width) == "sum"
197-
if (relative_height) {
198-
legend_height <- unit(1, "npc")
193+
if (any(unitType(legend_box$widths) == "sum")) {
194+
legend_width <- unit(1, "npc")
195+
} else {
196+
legend_width <- gtable_width(legend_box)
199197
}
200-
if (relative_width) {
201-
legend_width <- unit(1, "npc")
198+
if (any(unitType(legend_box$heights) == "sum")) {
199+
legend_height <- unit(1, "npc")
200+
} else {
201+
legend_height <- gtable_height(legend_box)
202202
}
203203

204204
# Set the justification of the legend box
@@ -234,11 +234,11 @@ ggplot_gtable.ggplot_built <- function(data) {
234234
width = legend_width
235235
)
236236
)
237-
if (!relative_height) {
237+
if (unitType(legend_height) != "npc") {
238238
legend_box <- gtable_add_rows(legend_box, unit(yjust, 'null'))
239239
legend_box <- gtable_add_rows(legend_box, unit(1 - yjust, 'null'), 0)
240240
}
241-
if (!relative_width) {
241+
if (unitType(legend_width) != "npc") {
242242
legend_box <- gtable_add_cols(legend_box, unit(xjust, 'null'), 0)
243243
legend_box <- gtable_add_cols(legend_box, unit(1 - xjust, 'null'))
244244
}

0 commit comments

Comments
 (0)