-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Stretching legends #5515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stretching legends #5515
Changes from all commits
0d592ea
ea8e855
dc8f940
d2fae2f
d722392
5a9d4d9
394ee59
44fd15d
52eafac
beb6e64
39fe78e
6da4b2b
cbb7b66
2be4e91
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,12 +15,11 @@ NULL | |
#' see [guides()]. | ||
#' | ||
#' @inheritParams guide_legend | ||
#' @param barwidth A numeric or a [grid::unit()] object specifying | ||
#' the width of the colourbar. Default value is `legend.key.width` or | ||
#' `legend.key.size` in [theme()] or theme. | ||
#' @param barheight A numeric or a [grid::unit()] object specifying | ||
#' the height of the colourbar. Default value is `legend.key.height` or | ||
#' `legend.key.size` in [theme()] or theme. | ||
#' @param barwidth,barheight A numeric or [grid::unit()] object specifying the | ||
#' width and height of the bar respectively. Default value is derived from | ||
#' `legend.key.width`, `legend.key.height` or `legend.key` in [theme()].\cr | ||
#' `r lifecycle::badge("experimental")`: optionally a `"null"` unit to stretch | ||
#' the bar to the available space. | ||
#' @param frame A theme object for rendering a frame drawn around the bar. | ||
#' Usually, the object of `element_rect()` is expected. If `element_blank()` | ||
#' (default), no frame is drawn. | ||
|
@@ -452,29 +451,29 @@ GuideColourbar <- ggproto( | |
) | ||
grob <- rasterGrob( | ||
image = image, | ||
width = elements$key.width, | ||
height = elements$key.height, | ||
default.units = "cm", | ||
width = 1, | ||
height = 1, | ||
default.units = "npc", | ||
gp = gpar(col = NA), | ||
interpolate = TRUE | ||
) | ||
} else{ | ||
if (params$direction == "horizontal") { | ||
width <- elements$key.width / nrow(decor) | ||
height <- elements$key.height | ||
width <- 1 / nrow(decor) | ||
height <- 1 | ||
x <- (seq(nrow(decor)) - 1) * width | ||
y <- 0 | ||
} else { | ||
width <- elements$key.width | ||
height <- elements$key.height / nrow(decor) | ||
width <- 1 | ||
height <- 1 / nrow(decor) | ||
Comment on lines
+454
to
+468
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Setting these sizes to npcs is benign because the actual size is set in |
||
y <- (seq(nrow(decor)) - 1) * height | ||
x <- 0 | ||
} | ||
grob <- rectGrob( | ||
x = x, y = y, | ||
vjust = 0, hjust = 0, | ||
width = width, height = height, | ||
default.units = "cm", | ||
default.units = "npc", | ||
gp = gpar(col = NA, fill = decor$colour) | ||
) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,12 +36,11 @@ | |
#' (right-aligned) for expressions. | ||
#' @param label.vjust A numeric specifying vertical justification of the label | ||
#' text. | ||
#' @param keywidth A numeric or a [grid::unit()] object specifying | ||
#' the width of the legend key. Default value is `legend.key.width` or | ||
#' `legend.key.size` in [theme()]. | ||
#' @param keyheight A numeric or a [grid::unit()] object specifying | ||
#' the height of the legend key. Default value is `legend.key.height` or | ||
#' `legend.key.size` in [theme()]. | ||
#' @param keywidth,keyheight A numeric or [grid::unit()] object specifying the | ||
#' width and height of the legend key respectively. Default value is | ||
#' `legend.key.width`, `legend.key.height` or `legend.key` in [theme()].\cr | ||
#' `r lifecycle::badge("experimental")`: optionally a `"null"` unit to stretch | ||
#' keys to the available space. | ||
#' @param key.spacing,key.spacing.x,key.spacing.y A numeric or [grid::unit()] | ||
#' object specifying the distance between key-label pairs in the horizontal | ||
#' direction (`key.spacing.x`), vertical direction (`key.spacing.y`) or both | ||
|
@@ -603,8 +602,19 @@ GuideLegend <- ggproto( | |
# Measure title | ||
title_width <- width_cm(grobs$title) | ||
title_height <- height_cm(grobs$title) | ||
extra_width <- max(0, title_width - sum(widths)) | ||
extra_height <- max(0, title_height - sum(heights)) | ||
|
||
# Titles are assumed to have sufficient size when keys are null units | ||
if (is.unit(params$keywidth) && unitType(params$keywidth) == "null") { | ||
extra_width <- 0 | ||
} else { | ||
extra_width <- max(0, title_width - sum(widths)) | ||
} | ||
if (is.unit(params$keyheight) && unitType(params$keyheight) == "null") { | ||
extra_height <- 0 | ||
} else { | ||
extra_height <- max(0, title_height - sum(heights)) | ||
} | ||
|
||
just <- with(elements$title, rotate_just(angle, hjust, vjust)) | ||
hjust <- just$hjust | ||
vjust <- just$vjust | ||
|
@@ -699,11 +709,19 @@ GuideLegend <- ggproto( | |
}, | ||
|
||
assemble_drawing = function(grobs, layout, sizes, params, elements) { | ||
widths <- unit(c(sizes$padding[4], sizes$widths, sizes$padding[2]), "cm") | ||
if (is.unit(params$keywidth) && unitType(params$keywidth) == "null") { | ||
i <- unique(layout$layout$key_col) | ||
widths[i] <- params$keywidth | ||
} | ||
|
||
gt <- gtable( | ||
widths = unit(c(sizes$padding[4], sizes$widths, sizes$padding[2]), "cm"), | ||
heights = unit(c(sizes$padding[1], sizes$heights, sizes$padding[3]), "cm") | ||
) | ||
heights <- unit(c(sizes$padding[1], sizes$heights, sizes$padding[3]), "cm") | ||
if (is.unit(params$keyheight) && unitType(params$keyheight) == "null") { | ||
i <- unique(layout$layout$key_row) | ||
heights[i] <- params$keyheight | ||
} | ||
|
||
gt <- gtable(widths = widths, heights = heights) | ||
Comment on lines
+712
to
+724
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is step 1 |
||
|
||
# Add background | ||
if (!is.zero(elements$background)) { | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.