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
|
#' Edge direction guide
#'
#' This guide is intended to show the direction of edges based on the aesthetics
#' mapped to its progression, such as changing width, colour and opacity.
#'
#' @inheritParams ggplot2::guide_colourbar
#' @inheritParams ggplot2::guide_legend
#'
#' @param arrow Logical. Should an arrow be drawn to illustrate the direction.
#' Defaults to `TRUE`. The arrow is styled with the `legend.axis.line` theme
#' element. If `FALSE` the direction will be indicated by the text
#' given in `labels`
#'
#' @param labels A vector with two strings giving the labels to place at the
#' start and the end of the legend to indicate direction if `arrow = FALSE`
#'
#' @param arrow.position `r lifecycle::badge('deprecated')` The position of the
#' arrow relative to the example edge. Use the `legend.text.position` argument
#' in `theme()` instead.
#'
#' @export
#'
#' @examples
#' gr <- tidygraph::as_tbl_graph(highschool)
#' ggraph(gr, layout = 'kk') +
#' geom_edge_fan(aes(alpha = after_stat(index))) +
#' guides(edge_alpha = guide_edge_direction())
#'
#' # Use text labels instead of an arrow
#' ggraph(gr, layout = 'kk') +
#' geom_edge_fan(aes(alpha = after_stat(index))) +
#' guides(edge_alpha = guide_edge_direction(labels = c('start', 'end')))
#'
#' # Style the indicator arrow
#' arrow_style <- element_line(linewidth = 3, arrow = grid::arrow(type = "closed"))
#' ggraph(gr, layout = 'kk') +
#' geom_edge_fan(aes(alpha = after_stat(index))) +
#' guides(
#' edge_alpha = guide_edge_direction(
#' theme = theme(legend.axis.line = arrow_style)
#' )
#' )
#'
guide_edge_direction <- function(title = NULL, theme = NULL, arrow = NULL,
labels = NULL,
nbin = 500, position = NULL, direction = NULL,
reverse = FALSE, order = 0,
override.aes = list(), ...,
available_aes = c("edge_colour", "edge_alpha", "edge_width"),
arrow.position = deprecated()) {
# Piggyback on non-exported `deprecated_guide_args()`
theme <- guide_colourbar(theme = theme, ...)$params$theme
if (!is.null(arrow) && !is.null(labels)) {
cli::cli_abort("{.arg arrow} and {.arg labels} can't be used at the same time")
}
if (is.null(arrow) && is.null(labels)) arrow <- TRUE
if (!is.null(labels)) arrow <- FALSE
if (!arrow && is.null(labels)) labels <- c("from", "to")
if (lifecycle::is_present(arrow.position)) {
lifecycle::deprecate_warn('2.2.0', 'guide_edge_direction(arrow.position)', 'guide_edge_direction(theme = "theme(legend.text.position)")')
if (!is.null(arrow.position)) {
if (is.null(theme)) theme <- theme()
theme$legend.text.position <- arrow.position
}
}
if (!is.null(position)) {
position <- arg_match0(position, c("top", "right", "bottom", "left", "inside"))
}
if (!is.null(labels) && (length(labels) != 2 || !is.character(labels))) {
cli::cli_abort('{.arg label} must be a vector of two strings')
}
new_guide(
title = title,
theme = theme,
arrow = arrow,
labels = labels,
nbin = nbin,
position = position,
direction = direction,
override.aes = expand_edge_aes(rename_aes(override.aes)),
reverse = reverse,
order = order,
available_aes = available_aes,
name = 'direction',
super = GuideEdgeDirection
)
}
GuideEdgeDirection <- ggproto(
"GuideEdgeDirection", GuideLegend,
params = list(
# title
title = NULL,
# theming
theme = NULL,
# bar
nbin = 300,
arrow = TRUE,
labels = c("from", "to"),
# general
direction = NULL,
override.aes = list(),
reverse = FALSE,
order = 0,
# parameter
name = "direction",
hash = character(),
position = NULL
),
available_aes = c('edge_colour', 'edge_alpha', 'edge_width'),
hashables = exprs(title, decor$value, name),
elements = list(
background = "legend.background",
margin = "legend.margin",
key = "legend.key",
key_height = "legend.key.height",
key_width = "legend.key.width",
text = "legend.text",
theme.title = "legend.title",
text_position = "legend.text.position",
title_position = "legend.title.position",
arrow_line = "legend.axis.line"
),
extract_key = function(scale, aesthetic, ...) {
if (scale$is_discrete()) {
cli::cli_warn("{.fn guide_edge_direction} needs continuous scales.")
return(NULL)
}
Guide$extract_key(scale, aesthetic, ...)
},
extract_decor = function(scale, aesthetic, nbin = 300, reverse = FALSE, ...) {
limits <- scale$get_limits()
bar <- seq(limits[1], limits[2], length.out = nbin)
if (length(bar) == 0) {
bar <- unique0(limits)
}
aes <- scale$aesthetics[1]
bar <- data_frame0(
{{aes}} := scale$map(bar),
value = bar,
.size = length(bar)
)
if (reverse) {
bar <- bar[rev(seq_len(nrow(bar))), , drop = FALSE]
}
return(bar)
},
merge = function(self, params, new_guide, new_params) {
new_params$decor$value <- NULL
params$decor <- vec_cbind(params$decor, new_params$decor)
return(list(guide = self, params = params))
},
get_layer_key = function(params, layers, data = NULL) {
decor <- GuideLegend$get_layer_key(params, layers, data)$decor
extra_data <- lapply(decor, `[[`, 'data')
missing_aes <- setdiff(c("edge_colour", "edge_alpha", "edge_width"), names(params$decor))
for (d in extra_data) {
for (aes in missing_aes) {
if (!is.null(d[[aes]])) params$decor[[aes]] <- d[[aes]][1]
}
}
params
},
setup_params = function(params) {
params$direction <- arg_match0(
params$direction,
c("horizontal", "vertical"), arg_nm = "direction"
)
params
},
setup_elements = function(params, elements, theme) {
elements <- GuideColourbar$setup_elements(params, elements, theme)
if (is.logical(elements$arrow_line$arrow)) {
elements$arrow_line$arrow <- grid::arrow()
}
elements$arrow_line$arrow$ends <- if (params$reverse) 1L else 2L
elements
},
build_labels = function(key, elements, params) {
if (params$arrow) {
list(labels = flip_element_grob(
elements$arrow_line,
x = unit(c(0, 1), "npc"),
y = unit(c(0.5, 0.5), "npc"),
flip = params$direction == "vertical"
))
} else {
list(labels = flip_element_grob(
elements$text,
label = params$labels,
x = unit(c(0, 1), "npc"),
margin_x = FALSE,
margin_y = TRUE,
flip = params$direction == "vertical"
))
}
},
build_ticks = function(key, elements, params, position = params$position) {
zeroGrob()
},
build_decor = function(decor, grobs, elements, params) {
y <- seq(0, 1, length.out = nrow(decor) + 1)
yend <- y[-1]
y <- y[-length(y)]
x <- rep(0.5, length(y))
xend <- x
if (params$direction == "horizontal") {
tmp <- x
x <- y
y <- tmp
tmp <- xend
xend <- yend
yend <- tmp
}
grob <- segmentsGrob(
x0 = x, y0 = y, x1 = xend, y1 = yend,
default.units = "npc",
gp = gpar(
col = alpha(decor$edge_colour, decor$edge_alpha),
lwd = decor$edge_width * .pt,
lty = decor$edge_linetyoe,
lineend = "butt"
)
)
list(bar = grob)
},
measure_grobs = function(grobs, params, elements) {
params$sizes <- list(
widths = elements$width_cm,
heights = elements$height_cm
)
sizes <- GuideLegend$measure_grobs(grobs, params, elements)
if (params$arrow) {
l <- convertHeight(elements$arrow_line$arrow$length, "cm", valueOnly = TRUE)
a <- 2 * pi * elements$arrow_line$arrow$angle / 360
width <- sin(a) * l * 2 + elements$arrow_line$linewidth / 10
if (params$direction == "vertical") {
sizes$widths[sizes$widths == 0] <- width
} else {
sizes$heights[sizes$heights == 0] <- width
}
}
sizes
}
)
|