Neighbor highlighting via mouseover and click at the same time
1 |
#' Layouts
|
|
2 |
#'
|
|
3 |
#' Layout your graph.
|
|
4 |
#'
|
|
5 |
#' @inheritParams sg_nodes
|
|
6 |
#' @param nodes,edges Nodes and edges as prepared for sigmajs.
|
|
7 |
#' @param directed Whether or not to create a directed graph, passed to \code{\link[igraph]{graph_from_data_frame}}.
|
|
8 |
#' @param layout An \code{igraph} layout function.
|
|
9 |
#' @param save_igraph Whether to save the \code{igraph} object used internally.
|
|
10 |
#' @param ... Any other parameter to pass to \code{layout} function.
|
|
11 |
#'
|
|
12 |
#' @details The package uses \code{igraph} internally for a lot of computations the \code{save_igraph}
|
|
13 |
#' allows saving the object to speed up subsequent computations.
|
|
14 |
#'
|
|
15 |
#' @section Functions:
|
|
16 |
#' \itemize{
|
|
17 |
#' \item{\code{sg_layout} layout your graph.}
|
|
18 |
#' \item{\code{sg_get_layout} helper to get graph's \code{x} and \code{y} positions.}
|
|
19 |
#' }
|
|
20 |
#'
|
|
21 |
#' @examples
|
|
22 |
#' nodes <- sg_make_nodes(250) # 250 nodes
|
|
23 |
#' edges <- sg_make_edges(nodes, n = 500)
|
|
24 |
#'
|
|
25 |
#' sigmajs() %>%
|
|
26 |
#' sg_nodes(nodes, id, size, color) %>%
|
|
27 |
#' sg_edges(edges, id, source, target) %>%
|
|
28 |
#' sg_layout()
|
|
29 |
#'
|
|
30 |
#' nodes_coords <- sg_get_layout(nodes, edges)
|
|
31 |
#'
|
|
32 |
#' @return \code{sg_get_layout} returns nodes with \code{x} and \code{y} coordinates.
|
|
33 |
#'
|
|
34 |
#' @rdname layout
|
|
35 |
#' @export
|
|
36 |
sg_layout <- function(sg, directed = TRUE, layout = igraph::layout_nicely, save_igraph = TRUE, ...){ |
|
37 |
|
|
38 | 1 |
if (missing(sg)) |
39 | 1 |
stop("missing sg", call. = FALSE) |
40 |
|
|
41 | 1 |
if (!inherits(sg, "sigmajs")) |
42 | 1 |
stop("sg must be of class sigmajs", call. = FALSE) |
43 |
|
|
44 | 1 |
nodes <- .data_2_df(sg$x$data$nodes) |
45 | 1 |
edges <- .data_2_df(sg$x$data$edges) |
46 |
|
|
47 |
# clean
|
|
48 | 1 |
nodes <- .rm_x_y(nodes) |
49 |
|
|
50 | 1 |
nodes <- sg_get_layout(nodes, edges, directed, layout, save_igraph = save_igraph, ...) |
51 |
|
|
52 | 1 |
nodes <- apply(nodes, 1, as.list) |
53 |
|
|
54 | 1 |
sg$x$data$nodes <- nodes |
55 | 1 |
sg |
56 |
}
|
|
57 |
|
|
58 |
#' @rdname layout
|
|
59 |
#' @export
|
|
60 |
sg_get_layout <- function(nodes, edges, directed = TRUE, layout = igraph::layout_nicely, save_igraph = TRUE, ...){ |
|
61 |
|
|
62 | 1 |
if (missing(nodes) || missing(edges)) |
63 | 1 |
stop("missing nodes or edges", call. = FALSE) |
64 |
|
|
65 |
# clean
|
|
66 | 1 |
edges <- .re_order(edges) |
67 | 1 |
nodes <- .rm_x_y(nodes) |
68 | 1 |
nodes <- .re_order_nodes(nodes) |
69 |
|
|
70 | 1 |
g <- .build_igraph(edges, directed = directed, nodes, save = save_igraph) |
71 |
|
|
72 | 1 |
l <- layout(g, ...) |
73 | 1 |
l <- as.data.frame(l) %>% |
74 | 1 |
dplyr::select("x" = "V1", "y" = "V2") |
75 |
|
|
76 | 1 |
nodes <- dplyr::bind_cols(nodes, l) |
77 |
|
|
78 | 1 |
return(nodes) |
79 |
}
|
Read our documentation on viewing source code .