1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
|
#' Set guides for each scale
#'
#' Guides for each scale can be set scale-by-scale with the `guide`
#' argument, or en masse with `guides()`.
#'
#' @param ... List of scale name-guide pairs. The guide can either
#' be a string (i.e. "colorbar" or "legend"), or a call to a guide function
#' (i.e. [guide_colourbar()] or [guide_legend()])
#' specifying additional arguments.
#' @return A list containing the mapping between scale and guide.
#' @export
#' @family guides
#' @examples
#' \donttest{
#' # ggplot object
#'
#' dat <- data.frame(x = 1:5, y = 1:5, p = 1:5, q = factor(1:5),
#' r = factor(1:5))
#' p <-
#' ggplot(dat, aes(x, y, colour = p, size = q, shape = r)) +
#' geom_point()
#'
#' # without guide specification
#' p
#'
#' # Show colorbar guide for colour.
#' # All these examples below have a same effect.
#'
#' p + guides(colour = "colorbar", size = "legend", shape = "legend")
#' p + guides(colour = guide_colorbar(), size = guide_legend(),
#' shape = guide_legend())
#' p +
#' scale_colour_continuous(guide = "colorbar") +
#' scale_size_discrete(guide = "legend") +
#' scale_shape(guide = "legend")
#'
#' # Remove some guides
#' p + guides(colour = "none")
#' p + guides(colour = "colorbar",size = "none")
#'
#' # Guides are integrated where possible
#'
#' p +
#' guides(
#' colour = guide_legend("title"),
#' size = guide_legend("title"),
#' shape = guide_legend("title")
#' )
#' # same as
#' g <- guide_legend("title")
#' p + guides(colour = g, size = g, shape = g)
#'
#' p + theme(legend.position = "bottom")
#'
#' # position of guides
#'
#' # Set order for multiple guides
#' ggplot(mpg, aes(displ, cty)) +
#' geom_point(aes(size = hwy, colour = cyl, shape = drv)) +
#' guides(
#' colour = guide_colourbar(order = 1),
#' shape = guide_legend(order = 2),
#' size = guide_legend(order = 3)
#' )
#' }
guides <- function(...) {
args <- list2(...)
if (length(args) > 0) {
if (is.list(args[[1]]) && !inherits(args[[1]], "guide")) args <- args[[1]]
args <- rename_aes(args)
}
idx_false <- vapply(args, isFALSE, FUN.VALUE = logical(1L))
if (isTRUE(any(idx_false))) {
deprecate_warn0("3.3.4", "guides(`<scale>` = 'cannot be `FALSE`. Use \"none\" instead')")
args[idx_false] <- "none"
}
structure(args, class = "guides")
}
update_guides <- function(p, guides) {
p <- plot_clone(p)
p$guides <- defaults(guides, p$guides)
p
}
# building non-position guides - called in ggplotGrob (plot-build.r)
#
# the procedure is as follows:
#
# 1. guides_train()
# train each scale and generate guide definition for all guides
# here, one gdef for one scale
#
# 2. guides_merge()
# merge gdefs if they are overlayed
# number of gdefs may be less than number of scales
#
# 3. guides_geom()
# process layer information and generate geom info.
#
# 4. guides_gengrob()
# generate ggrob from each gdef
# one ggrob for one gdef
#
# 5. guides_build()
# arrange all ggrobs
build_guides <- function(scales, layers, default_mapping, position, theme, guides, labels) {
theme$legend.key.width <- theme$legend.key.width %||% theme$legend.key.size
theme$legend.key.height <- theme$legend.key.height %||% theme$legend.key.size
# Layout of legends depends on their overall location
position <- legend_position(position)
if (position == "inside") {
theme$legend.box <- theme$legend.box %||% "vertical"
theme$legend.direction <- theme$legend.direction %||% "vertical"
theme$legend.box.just <- theme$legend.box.just %||% c("center", "center")
} else if (position == "vertical") {
theme$legend.box <- theme$legend.box %||% "vertical"
theme$legend.direction <- theme$legend.direction %||% "vertical"
theme$legend.box.just <- theme$legend.box.just %||% c("left", "top")
} else if (position == "horizontal") {
theme$legend.box <- theme$legend.box %||% "horizontal"
theme$legend.direction <- theme$legend.direction %||% "horizontal"
theme$legend.box.just <- theme$legend.box.just %||% c("center", "top")
}
# scales -> data for guides
gdefs <- guides_train(
scales = scales$non_position_scales(),
theme = theme,
guides = guides,
labels = labels
)
if (length(gdefs) == 0) return(zeroGrob())
# merge overlay guides
gdefs <- guides_merge(gdefs)
# process layer information
gdefs <- guides_geom(gdefs, layers, default_mapping)
if (length(gdefs) == 0) return(zeroGrob())
# generate grob of each guides
ggrobs <- guides_gengrob(gdefs, theme)
# build up guides
grobs <- guides_build(ggrobs, theme)
grobs
}
# Simplify legend position to one of horizontal/vertical/inside
legend_position <- function(position) {
if (length(position) == 1) {
if (position %in% c("top", "bottom")) {
"horizontal"
} else {
"vertical"
}
} else {
"inside"
}
}
# resolve the guide from the scale and guides
resolve_guide <- function(aesthetic, scale, guides, default = "none", null = "none") {
guides[[aesthetic]] %||% scale$guide %|W|% default %||% null
}
# validate guide object
validate_guide <- function(guide) {
# if guide is specified by character, then find the corresponding guide
# when guides are officially extensible, this should use find_global()
if (is.character(guide))
match.fun(paste("guide_", guide, sep = ""))()
else if (inherits(guide, "guide"))
guide
else
cli::cli_abort("Unknown guide: {guide}")
}
# train each scale in scales and generate the definition of guide
guides_train <- function(scales, theme, guides, labels) {
gdefs <- list()
for (scale in scales$scales) {
for (output in scale$aesthetics) {
# guides(XXX) is stored in guides[[XXX]],
# which is prior to scale_ZZZ(guide=XXX)
# guide is determined in order of:
# + guides(XXX) > + scale_ZZZ(guide=XXX) > default(i.e., legend)
guide <- resolve_guide(output, scale, guides)
if (identical(guide, "none") || inherits(guide, "guide_none")) next
if (isFALSE(guide)) {
deprecate_warn0("3.3.4", I("The `guide` argument in `scale_*()` cannot be `FALSE`. This "), I('"none"'))
next
}
# check the validity of guide.
# if guide is character, then find the guide object
guide <- validate_guide(guide)
# check the consistency of the guide and scale.
if (!identical(guide$available_aes, "any") && !any(scale$aesthetics %in% guide$available_aes)) {
cli::cli_abort("Guide {.var {guide$name}} cannot be used for {.field {scale$aesthetics}}.")
}
guide$title <- scale$make_title(guide$title %|W|% scale$name %|W|% labels[[output]])
# direction of this grob
guide$direction <- guide$direction %||% theme$legend.direction
# each guide object trains scale within the object,
# so Guides (i.e., the container of guides) need not to know about them
guide <- guide_train(guide, scale, output)
if (!is.null(guide)) gdefs[[length(gdefs) + 1]] <- guide
}
}
gdefs
}
# merge overlapped guides
guides_merge <- function(gdefs) {
# split gdefs based on hash, and apply Reduce (guide_merge) to each gdef group.
gdefs <- lapply(gdefs, function(g) {
if (g$order == 0) {
order <- "99"
} else {
order <- sprintf("%02d", g$order)
}
g$hash <- paste(order, g$hash, sep = "_")
g
})
tapply(gdefs, sapply(gdefs, function(g)g$hash), function(gs)Reduce(guide_merge, gs))
}
# process layer information
# TODO: `default_mapping` is unused internally but kept for backwards compitability until guide rewrite
guides_geom <- function(gdefs, layers, default_mapping) {
compact(lapply(gdefs, guide_geom, layers, default_mapping))
}
# generate grob from each gdef (needs to write this function?)
guides_gengrob <- function(gdefs, theme) {
# common drawing process for all guides
gdefs <- lapply(gdefs,
function(g) {
g$title.position <- g$title.position %||% switch(g$direction, vertical = "top", horizontal = "left")
if (!g$title.position %in% c("top", "bottom", "left", "right")) {
cli::cli_abort(c(
"Title position {.val {g$title.position}} is invalid",
"i" = "Use one of {.val top}, {.val bottom}, {.val left}, or {.val right}"
))
}
g
})
lapply(gdefs, guide_gengrob, theme)
}
# build up all guide boxes into one guide-boxes.
guides_build <- function(ggrobs, theme) {
theme$legend.spacing <- theme$legend.spacing %||% unit(0.5, "lines")
theme$legend.spacing.y <- theme$legend.spacing.y %||% theme$legend.spacing
theme$legend.spacing.x <- theme$legend.spacing.x %||% theme$legend.spacing
widths <- lapply(ggrobs, function(g) sum(g$widths))
widths <- inject(unit.c(!!!widths))
heights <- lapply(ggrobs, function(g) sum(g$heights))
heights <- inject(unit.c(!!!heights))
# Set the justification of each legend within the legend box
# First value is xjust, second value is yjust
just <- valid.just(theme$legend.box.just)
xjust <- just[1]
yjust <- just[2]
# setting that is different for vertical and horizontal guide-boxes.
if (identical(theme$legend.box, "horizontal")) {
# Set justification for each legend
for (i in seq_along(ggrobs)) {
ggrobs[[i]] <- editGrob(ggrobs[[i]],
vp = viewport(x = xjust, y = yjust, just = c(xjust, yjust),
height = heightDetails(ggrobs[[i]])))
}
guides <- gtable_row(name = "guides",
grobs = ggrobs,
widths = widths, height = max(heights))
# add space between the guide-boxes
guides <- gtable_add_col_space(guides, theme$legend.spacing.x)
} else { # theme$legend.box == "vertical"
# Set justification for each legend
for (i in seq_along(ggrobs)) {
ggrobs[[i]] <- editGrob(ggrobs[[i]],
vp = viewport(x = xjust, y = yjust, just = c(xjust, yjust),
width = widthDetails(ggrobs[[i]])))
}
guides <- gtable_col(name = "guides",
grobs = ggrobs,
width = max(widths), heights = heights)
# add space between the guide-boxes
guides <- gtable_add_row_space(guides, theme$legend.spacing.y)
}
# Add margins around the guide-boxes.
theme$legend.box.margin <- theme$legend.box.margin %||% margin()
guides <- gtable_add_cols(guides, theme$legend.box.margin[4], pos = 0)
guides <- gtable_add_cols(guides, theme$legend.box.margin[2], pos = ncol(guides))
guides <- gtable_add_rows(guides, theme$legend.box.margin[1], pos = 0)
guides <- gtable_add_rows(guides, theme$legend.box.margin[3], pos = nrow(guides))
# Add legend box background
background <- element_grob(theme$legend.box.background %||% element_blank())
guides <- gtable_add_grob(guides, background, t = 1, l = 1,
b = -1, r = -1, z = -Inf, clip = "off", name = "legend.box.background")
guides$name <- "guide-box"
guides
}
# Generics ----------------------------------------------------------------
#' S3 generics for guides.
#'
#' You will need to provide methods for these S3 generics if you want to
#' create your own guide object. They are currently undocumented; use at
#' your own risk!
#'
#' @param guide The guide object
#' @keywords internal
#' @name guide-exts
NULL
#' @export
#' @rdname guide-exts
guide_train <- function(guide, scale, aesthetic = NULL) UseMethod("guide_train")
#' @export
#' @rdname guide-exts
guide_merge <- function(guide, new_guide) UseMethod("guide_merge")
#' @export
#' @rdname guide-exts
guide_geom <- function(guide, layers, default_mapping) UseMethod("guide_geom")
#' @export
#' @rdname guide-exts
guide_transform <- function(guide, coord, panel_params) UseMethod("guide_transform")
#' @export
guide_transform.default <- function(guide, coord, panel_params) {
cli::cli_abort(c(
"Guide with class {.cls {class(guide)}} does not implement {.fn guide_transform}",
"i" = "Did you mean to use {.fn guide_axis}?"
))
}
#' @export
#' @rdname guide-exts
guide_gengrob <- function(guide, theme) UseMethod("guide_gengrob")
# Helpers -----------------------------------------------------------------
matched_aes <- function(layer, guide) {
all <- names(c(layer$computed_mapping, layer$stat$default_aes))
geom <- c(layer$geom$required_aes, names(layer$geom$default_aes))
# Make sure that size guides are shown if a renaming layer is used
if (layer$geom$rename_size && "size" %in% all && !"linewidth" %in% all) geom <- c(geom, "size")
matched <- intersect(intersect(all, geom), names(guide$key))
matched <- setdiff(matched, names(layer$computed_geom_params))
setdiff(matched, names(layer$aes_params))
}
# This function is used by guides in guide_geom.* to determine whether
# a given layer should be included in the guide
# `matched` is the set of aesthetics that match between the layer and the guide
include_layer_in_guide <- function(layer, matched) {
if (!is.logical(layer$show.legend)) {
cli::cli_warn("{.arg show.legend} must be a logical vector.")
layer$show.legend <- FALSE # save back to layer so we don't issue this warning more than once
return(FALSE)
}
if (length(matched) > 0) {
# This layer contributes to the legend
# check if this layer should be included, different behaviour depending on
# if show.legend is a logical or a named logical vector
if (is_named(layer$show.legend)) {
layer$show.legend <- rename_aes(layer$show.legend)
show_legend <- layer$show.legend[matched]
# we cannot use `isTRUE(is.na(show_legend))` here because
# 1. show_legend can be multiple NAs
# 2. isTRUE() was not tolerant for a named TRUE
show_legend <- show_legend[!is.na(show_legend)]
return(length(show_legend) == 0 || any(show_legend))
}
return(all(is.na(layer$show.legend)) || isTRUE(layer$show.legend))
}
# This layer does not contribute to the legend.
# Default is to exclude it, except if it is explicitly turned on
isTRUE(layer$show.legend)
}
|