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
|
#' Bundle edges along the minimal spanning tree
#'
#' This geom performs edge bundling by letting edges follow the shortest path
#' along the minimal spanning tree of the graph. Due to it's simplicity it is
#' very fast but does enforce a tree-like appearance to the bundling. Adjusting
#' the `max_distortion` and `tension` parameters may alleviate this to some
#' extend.
#'
#' @inheritSection geom_edge_link Edge variants
#' @inheritSection geom_edge_link Edge aesthetic name expansion
#'
#' @section Aesthetics:
#' `geom_edge_force_minimal` and `geom_edge_force_minimal0` understand the following
#' aesthetics. Bold aesthetics are automatically set, but can be overwritten.
#'
#' - **x**
#' - **y**
#' - **xend**
#' - **yend**
#' - **edge_id** (should not be overwritten)
#' - edge_colour
#' - edge_width
#' - edge_linetype
#' - edge_alpha
#' - filter
#'
#' `geom_edge_force_minimal2` understand the following aesthetics. Bold aesthetics are
#' automatically set, but can be overwritten.
#'
#' - **x**
#' - **y**
#' - **group**
#' - **edge_id** (should not be overwritten)
#' - edge_colour
#' - edge_width
#' - edge_linetype
#' - edge_alpha
#' - filter
#'
#' `geom_edge_force_minimal` and `geom_edge_force_minimal2` furthermore takes the following
#' aesthetics.
#'
#' - start_cap
#' - end_cap
#' - label
#' - label_pos
#' - label_size
#' - angle
#' - hjust
#' - vjust
#' - family
#' - fontface
#' - lineheight
#'
#'
#' @section Computed variables:
#'
#' \describe{
#' \item{index}{The position along the path (not computed for the *0 version)}
#' }
#'
#' @inheritParams geom_edge_link
#' @inheritParams ggplot2::geom_path
#' @inheritParams geom_edge_bundle_path
#'
#' @author Thomas Lin Pedersen
#'
#' @family geom_edge_*
#'
#' @rdname geom_edge_bundle_minimal
#' @name geom_edge_bundle_minimal
#'
#' @examples
#' ggraph(highschool) +
#' geom_edge_bundle_minimal()
#'
#' # Allow more edges to bundle
#' ggraph(highschool) +
#' geom_edge_bundle_minimal(max_distortion = 5, tension = 0.9)
#'
NULL
#' @rdname ggraph-extensions
#' @format NULL
#' @usage NULL
#' @importFrom ggforce StatBspline
#' @export
StatEdgeBundleMinimal <- ggproto("StatEdgeBundleMinimal", Stat,
setup_data = function(data, params) {
StatEdgeBundleMinimal0$setup_data(data, params)
},
compute_panel = function(data, scales, n = 100, max_distortion = 2,
weight_fac = 2, tension = 1) {
edges <- StatEdgeBundleMinimal0$compute_panel(
data, scales, max_distortion = max_distortion, weight_fac = weight_fac,
tension = tension
)
StatBspline$compute_layer(edges, list(n = n), NULL)
},
required_aes = c('x', 'y', 'xend', 'yend', 'edge_id'),
default_aes = aes(filter = TRUE),
extra_params = c("na.rm")
)
#' @rdname geom_edge_bundle_minimal
#'
#' @export
geom_edge_bundle_minimal <- function(mapping = NULL, data = get_edges(),
position = "identity", arrow = NULL,
n = 100, max_distortion = 2, weight_fac = 2,
tension = 1, lineend = 'butt',
linejoin = 'round', linemitre = 1,
label_colour = 'black', label_alpha = 1,
label_parse = FALSE, check_overlap = FALSE,
angle_calc = 'rot', force_flip = TRUE,
label_dodge = NULL, label_push = NULL,
show.legend = NA, ...) {
mapping <- complete_edge_aes(mapping)
mapping <- aes_intersect(mapping, aes(
x = x, y = y, xend = xend, yend = yend, group = edge.id, edge_id = edge.id
))
layer(
data = data, mapping = mapping, stat = StatEdgeBundleMinimal,
geom = GeomEdgePath, position = position,
show.legend = show.legend, inherit.aes = FALSE,
params = expand_edge_aes(
list2(
arrow = arrow, lineend = lineend, linejoin = linejoin,
linemitre = linemitre, n = n, interpolate = FALSE,
max_distortion = max_distortion, weight_fac = weight_fac,
tension = tension, label_colour = label_colour,
label_alpha = label_alpha, label_parse = label_parse,
check_overlap = check_overlap, angle_calc = angle_calc,
force_flip = force_flip, label_dodge = label_dodge,
label_push = label_push, ...
)
)
)
}
#' @rdname ggraph-extensions
#' @format NULL
#' @usage NULL
#' @importFrom ggforce StatBspline
#' @export
StatEdgeBundleMinimal2 <- ggproto("StatEdgeBundleMinimal2", Stat,
setup_data = function(data, params) {
data <- StatFilter$setup_data(data, params)
remove_loop2(data)
},
compute_panel = function(data, scales, n = 100, max_distortion = 2,
weight_fac = 2, tension = 1) {
graph <- .G()
nodes <- data_frame0(x = .N()$.ggraph_layout_x, y = .N()$.ggraph_layout_y)
data <- data[order(data$group), ]
edge_id <- data$edge_id[c(TRUE, FALSE)]
edges <- minimal_bundle_mem(graph, nodes, .E()$from[edge_id], .E()$to[edge_id],
max_distortion = max_distortion, weight_fac = weight_fac)
if (tension < 1) edges <- relax(edges, tension)
edges$PANEL <- data$PANEL[1]
edges$group <- data$group[edges$group * 2]
edges <- StatBspline$compute_layer(edges, list(n = n), NULL)
extra_data <- data[1, !names(data) %in% c("x", "y", "group", "PANEL")][rep(NA, nrow(edges)), ]
edges$.interp <- TRUE
ends <- !duplicated(edges$group) | !duplicated(edges$group, fromLast = TRUE)
edges$.interp[ends] <- FALSE
extra_data[ends, ] <- data[, names(extra_data)]
cbind(edges, extra_data)
},
required_aes = c("x", "y", "edge_id"),
default_aes = aes(filter = TRUE),
extra_params = c("na.rm")
)
#' @rdname geom_edge_bundle_minimal
#'
#' @export
geom_edge_bundle_minimal2 <- function(mapping = NULL, data = get_edges("long"),
position = "identity", arrow = NULL,
n = 100, max_distortion = 2, weight_fac = 2,
tension = 1, lineend = 'butt',
linejoin = 'round', linemitre = 1,
label_colour = 'black', label_alpha = 1,
label_parse = FALSE, check_overlap = FALSE,
angle_calc = 'rot', force_flip = TRUE,
label_dodge = NULL, label_push = NULL,
show.legend = NA, ...) {
mapping <- complete_edge_aes(mapping)
mapping <- aes_intersect(mapping, aes(
x = x, y = y, group = edge.id, edge_id = edge.id
))
layer(
data = data, mapping = mapping, stat = StatEdgeBundleMinimal2,
geom = GeomEdgePath, position = position,
show.legend = show.legend, inherit.aes = FALSE,
params = expand_edge_aes(
list2(
arrow = arrow, lineend = lineend, linejoin = linejoin,
linemitre = linemitre, n = n, interpolate = TRUE,
max_distortion = max_distortion, weight_fac = weight_fac,
tension = tension, label_colour = label_colour,
label_alpha = label_alpha, label_parse = label_parse,
check_overlap = check_overlap, angle_calc = angle_calc,
force_flip = force_flip, label_dodge = label_dodge,
label_push = label_push, ...
)
)
)
}
#' @rdname ggraph-extensions
#' @format NULL
#' @usage NULL
#' @export
StatEdgeBundleMinimal0 <- ggproto('StatEdgeBundleMinimal0', Stat,
setup_data = function(data, params) {
data <- StatFilter$setup_data(data, params)
remove_loop(data)
},
compute_panel = function(data, scales, max_distortion = 2, weight_fac = 2,
tension = 1) {
graph <- .G()
nodes <- data_frame0(x = .N()$.ggraph_layout_x, y = .N()$.ggraph_layout_y)
edges <- minimal_bundle_mem(graph, nodes, .E()$from[data$edge_id], .E()$to[data$edge_id],
max_distortion = max_distortion, weight_fac = weight_fac)
if (tension < 1) edges <- relax(edges, tension)
edges$PANEL <- data$PANEL[1]
edges$group <- data$group[edges$group]
cbind(edges, data[edges$group, !names(data) %in% c("x", "y", "xend", "yend", "PANEL", "group")])
},
required_aes = c('x', 'y', 'xend', 'yend', 'edge_id'),
default_aes = aes(filter = TRUE),
extra_params = c('na.rm')
)
#' @rdname geom_edge_bundle_minimal
#'
#' @export
geom_edge_bundle_minimal0 <- function(mapping = NULL, data = get_edges(),
position = "identity", arrow = NULL,
max_distortion = 2, weight_fac = 2, tension = 1,
lineend = 'butt', show.legend = NA, ...) {
mapping <- complete_edge_aes(mapping)
mapping <- aes_intersect(mapping, aes(
x = x, y = y, xend = xend, yend = yend, group = edge.id, edge_id = edge.id
))
layer(
data = data, mapping = mapping, stat = StatEdgeBundleMinimal0,
geom = GeomEdgeBspline, position = position,
show.legend = show.legend, inherit.aes = FALSE,
params = expand_edge_aes(
list2(
arrow = arrow, lineend = lineend, max_distortion = max_distortion,
weight_fac = weight_fac, tension = tension, ...
)
)
)
}
#' @importFrom igraph mst shortest_paths
minimal_bundle <- function(graph, nodes, from, to, max_distortion = 2, weight_fac = 2) {
edge_length <- sqrt((nodes$x[from] - nodes$x[to])^2 + (nodes$y[from] - nodes$y[to])^2)
weights <- edge_length^weight_fac
g_temp <- mst(graph, weights)
paths <- lapply(seq_along(from), function(f) {
s <- from[f]
t <- to[f]
path <- suppressWarnings(shortest_paths(g_temp, s, t, mode = "all", output = "vpath")$vpath[[1]])
path_length <- sum(sqrt((nodes$x[path[-length(path)]] - nodes$x[path[-1]])^2 + (nodes$y[path[-length(path)]] - nodes$y[path[-1]])^2))
if (path_length >= max_distortion * edge_length[f]) {
c(s, t)
} else {
path
}
})
ids <- rep(seq_along(from), lengths(paths))
paths <- unlist(paths)
data_frame0(x = nodes$x[paths], y = nodes$y[paths], group = ids)
}
minimal_bundle_mem <- memoise(minimal_bundle)
|