File: geom_edge_bundle_path.R

package info (click to toggle)
r-cran-ggraph 2.2.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,832 kB
  • sloc: cpp: 1,630; makefile: 2
file content (344 lines) | stat: -rw-r--r-- 13,162 bytes parent folder | download
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
#' Bundle edges using edge path bundling
#'
#' This geom performs edge bundling using the edge path algorithm. This approach
#' uses the underlying graph structure to find shortest paths for each edge in
#' a graph the is gradually removed of it's edges. Since it is based on the
#' topology of the graph it should lead to less spurious bundling of unrelated
#' edges compared to [geom_edge_bundle_force()] and also has a simpler parameter
#' space.
#'
#' @inheritSection geom_edge_link Edge variants
#' @inheritSection geom_edge_link Edge aesthetic name expansion
#'
#' @section Aesthetics:
#' `geom_edge_force_path` and `geom_edge_force_path0` 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_path2` 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_path` and `geom_edge_force_path2` 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
#'
#' @param directed Logical. Should the shortest paths be calculated using
#' direction information of the graph. Setting this to `TRUE` can help split up
#' bundles that flows in opposite directions. Ignored for undirected graphs
#' @param max_distortion A multiplication factor to determine the maximum
#' allowed distortion of the path during bundling. If the new edge is longer
#' than `max_distortion` times the old length it is rejected.
#' @param weight_fac The exponent used to assign weights to the graph when
#' calculating the shortest path. The final weights are given as
#' `edge_length ^ weight_fac` meaning that sorter edges are prioritised when
#' calculating the weights.
#' @param tension A loosening factor when calculating the b-spline of the edge
#' based on the shortest path. Will move control points closer and closer to
#' the direct line as it approaches 0
#'
#' @author Thomas Lin Pedersen and David Schoch
#'
#' @family geom_edge_*
#'
#' @references
#' Wallinger, M., Archambault, D., Auber, D., Nöllenburg, M., and Peltonen, J.
#' (2022). *Edge-Path Bundling: A Less Ambiguous Edge Bundling Approach.* IEEE
#' Transactions on Visualization and Computer Graphics 28(1) 313-323.
#' https://doi.org/10.1109/TVCG.2021.3114795
#'
#' @rdname geom_edge_bundle_path
#' @name geom_edge_bundle_path
#'
#' @examples
#' ggraph(highschool) +
#'   geom_edge_bundle_path()
#'
#' # Use tension to lessen the effect
#' ggraph(highschool) +
#'   geom_edge_bundle_path(tension = 0.8)
#'
NULL

#' @rdname ggraph-extensions
#' @format NULL
#' @usage NULL
#' @importFrom ggforce StatBspline
#' @export
StatEdgeBundlePath <- ggproto("StatEdgeBundlePath", Stat,
  setup_data = function(data, params) {
    StatEdgeBundlePath0$setup_data(data, params)
  },
  compute_panel = function(data, scales, n = 100, directed = NULL, max_distortion = 2,
                           weight_fac = 2, tension = 1) {
    edges <- StatEdgeBundlePath0$compute_panel(
      data, scales, directed = directed, 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_path
#'
#' @export
geom_edge_bundle_path <- function(mapping = NULL, data = get_edges(),
                                  position = "identity", arrow = NULL,
                                  n = 100, directed = NULL, 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 = StatEdgeBundlePath,
    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, directed = directed,
        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
StatEdgeBundlePath2 <- ggproto("StatEdgeBundlePath2", Stat,
  setup_data = function(data, params) {
    data <- StatFilter$setup_data(data, params)
    remove_loop2(data)
  },
  compute_panel = function(data, scales, n = 100, directed = NULL, 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 <- path_bundle_mem(graph, nodes, .E()$from[edge_id], .E()$to[edge_id],
                             directed = directed, 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_path
#'
#' @export
geom_edge_bundle_path2 <- function(mapping = NULL, data = get_edges("long"),
                                    position = "identity", arrow = NULL,
                                    n = 100, directed = NULL, 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 = StatEdgeBundlePath2,
    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, directed = directed,
        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
StatEdgeBundlePath0 <- ggproto('StatEdgeBundlePath0', Stat,
  setup_data = function(data, params) {
    data <- StatFilter$setup_data(data, params)
    remove_loop(data)
  },
  compute_panel = function(data, scales, directed = NULL, max_distortion = 2,
                           weight_fac = 2, tension = 1) {
    graph <- .G()
    nodes <- data_frame0(x = .N()$.ggraph_layout_x, y = .N()$.ggraph_layout_y)
    from <- .E()$from[data$edge_id]
    to <- .E()$to[data$edge_id]
    edges <- path_bundle_mem(graph, nodes, from, to, directed = directed,
                             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_path
#'
#' @export
geom_edge_bundle_path0 <- function(mapping = NULL, data = get_edges(),
                                    position = "identity", arrow = NULL,
                                    directed = 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 = StatEdgeBundlePath0,
    geom = GeomEdgeBspline, position = position,
    show.legend = show.legend, inherit.aes = FALSE,
    params = expand_edge_aes(
      list2(
        arrow = arrow, lineend = lineend, directed = directed,
        max_distortion = max_distortion, weight_fac = weight_fac,
        tension = tension, ...
      )
    )
  )
}
#' @importFrom igraph gsize delete_edges shortest_paths is_directed as_edgelist
path_bundle <- function(graph, nodes, from, to, directed = directed, max_distortion = 2, weight_fac = 2) {
  m <- gsize(graph)
  lock <- rep(FALSE, m)
  skip <- rep(FALSE, m)

  edge_length <- sqrt((nodes$x[from] - nodes$x[to])^2 + (nodes$y[from] - nodes$y[to])^2)
  weights <- edge_length^weight_fac
  edges_order <- order(weights, decreasing = TRUE)
  paths <- vector("list", m)
  if (is_directed(graph)) {
    directed <- directed %||% TRUE
  } else if (!is.null(directed)) {
    cli::cli_warn("Ignoring {.arg directed} for undirected graphs")
  } else {
    directed <- FALSE
  }
  mode <- if (is.null(directed) || !directed) "all" else "out"

  all_edges <- as_edgelist(graph)
  if (directed) {
    all_edges <- paste(all_edges[,1], "-", all_edges[,2])
  } else {
    all_edges <- paste(pmin(all_edges[,1], all_edges[,2]), "-", pmax(all_edges[,1], all_edges[,2]))
  }
  # iterate
  for (e in edges_order) {
    s <- from[e]
    t <- to[e]
    paths[[e]] <- c(s, t)
    if (lock[e]) {
      next()
    }
    skip[e] <- TRUE
    g_temp <- delete_edges(graph, which(skip))
    path <- suppressWarnings(shortest_paths(g_temp, s, t, weights = weights[!skip], mode = mode, output = "vpath")$vpath[[1]])
    if (length(path) < 2) {
      skip[e] <- FALSE
      next()
    }
    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[e]) {
      skip[e] <- FALSE
      next()
    }
    all_edges_on_path <- rep(as.integer(path), each = 2)
    all_edges_on_path <- matrix(all_edges_on_path[-c(1, length(all_edges_on_path))], ncol = 2, byrow = TRUE)
    if (directed) {
      all_edges_on_path <- paste(all_edges_on_path[,1], "-", all_edges_on_path[,2])
    } else {
      all_edges_on_path <- paste(pmin(all_edges_on_path[,1], all_edges_on_path[,2]),
                                 "-",
                                 pmax(all_edges_on_path[,1], all_edges_on_path[,2]))
    }
    all_edges_on_path <- all_edges %in% all_edges_on_path
    lock[all_edges_on_path] <- TRUE
    paths[[e]] <- path
  }
  ids <- rep(seq_along(from), lengths(paths))
  paths <- unlist(paths)
  data_frame0(x = nodes$x[paths], y = nodes$y[paths], group = ids)
}

path_bundle_mem <- memoise(path_bundle)