File: layout_focus.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 (51 lines) | stat: -rw-r--r-- 2,023 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
#' Place nodes in circles based on distance to a specific node
#'
#' This layout constrains node placement to a radius relative to its distance to
#' a given node. It then uses stress majorisation to find an optimal node
#' distribution according to this constraint.
#'
#' @param focus An expression evaluating to a selected node. Can either be a
#' single integer or a logical vector with a single `TRUE` element.
#' @inheritParams layout_tbl_graph_stress
#' @inheritParams layout_tbl_graph_centrality
#'
#' @return A data.frame with the columns `x`, `y`, `circular`, `distance` as
#' well as any information stored as node variables in the tbl_graph object.
#'
#' @references
#' Brandes, U., & Pich, C. (2011). *More flexible radial layout.* Journal of
#' Graph Algorithms and Applications, 15(1), 157-173.
#'
#' @family layout_tbl_graph_*
#'
#' @author The underlying algorithm is implemented in the graphlayouts package
#' by David Schoch
#'
#' @importFrom graphlayouts layout_with_focus layout_with_focus_group
#' @importFrom rlang eval_tidy enquo
layout_tbl_graph_focus <- function(graph, focus, weights = NULL, niter = 500, tolerance = 1e-4,
                                   group = NULL, shrink = 10, circular = TRUE) {
  focus <- eval_tidy(enquo(focus), .N())
  if (is.logical(focus)) focus <- which(focus)[1]

  weights <- eval_tidy(enquo(weights), .E())
  if (is.null(weights)) {
    weights <- NA
  }

  group <- eval_tidy(enquo(group), .N())

  if (is.null(group)) {
    layout <- layout_with_focus(graph, v = focus, weights = weights, iter = niter,
                                tol = tolerance)
  } else {
    layout <- layout_with_focus_group(graph, v = focus, group = group,
                                      shrink = shrink, weights = weights, iter = niter,
                                      tol = tolerance)
  }

  xy <- layout$xy

  nodes <- data_frame0(x = xy[,1],y = xy[,2], distance = layout$distance, circular = FALSE)
  combine_layout_nodes(nodes, as_tibble(graph, active = 'nodes'))
}