1
#' Cluster
2
#' 
3
#' Color nodes by cluster.
4
#' 
5
#' @inheritParams sg_nodes
6
#' @param nodes,edges Nodes and edges as prepared for sigmajs.
7
#' @param colors Palette to color the nodes.
8
#' @param directed Whether or not to create a directed graph, passed to \code{\link[igraph]{graph_from_data_frame}}.
9
#' @param algo An \code{igraph} clustering function.
10
#' @param quiet Set to \code{TRUE} to print the number of clusters to the console.
11
#' @param save_igraph Whether to save the \code{igraph} object used internally.
12
#' @param ... Any parameter to pass to \code{algo}.
13
#' 
14
#' @details The package uses \code{igraph} internally for a lot of computations the \code{save_igraph} 
15
#' allows saving the object to speed up subsequent computations.
16
#' 
17
#' @section Functions:
18
#' \itemize{
19
#'   \item{\code{sg_cluster} Color nodes by cluster.}
20
#'   \item{\code{sg_get_cluster} helper to get graph's nodes color by cluster.}
21
#' }
22
#' 
23
#' @examples 
24
#' nodes <- sg_make_nodes() 
25
#' edges <- sg_make_edges(nodes, 15)
26
#' 
27
#' sigmajs() %>% 
28
#'   sg_nodes(nodes, id, size) %>% 
29
#'   sg_edges(edges, id, source, target) %>% 
30
#'   sg_layout() %>% 
31
#'   sg_cluster() 
32
#'   
33
#' clustered <- sg_get_cluster(nodes, edges)
34
#' 
35
#' @return \code{sg_get_cluster} returns nodes with \code{color} variable while 
36
#' \code{sg_cluster} returns an object of class \code{htmlwidget} which renders 
37
#' the visualisation on print.
38
#' 
39
#' @rdname cluster
40
#' @export
41
sg_cluster <- function(sg, colors = c("#B1E2A3", "#98D3A5", "#328983", "#1C5C70", "#24C96B"),
42
                       directed = TRUE, algo = igraph::cluster_walktrap, quiet = !interactive(), 
43
                       save_igraph = TRUE, ...){
44
  
45 1
  if (missing(sg))
46 1
    stop("missing sg", call. = FALSE)
47
  
48 1
  .test_sg(sg)
49
  
50
  # build graph
51 1
  nodes <- .data_2_df(sg$x$data$nodes)
52 1
  edges <- .data_2_df(sg$x$data$edges) 
53 1
  nodes <- sg_get_cluster(nodes, edges, colors, directed, algo, quiet, save_igraph, ...)
54
  
55 1
  nodes <- apply(nodes, 1, as.list)
56
  
57 1
  sg$x$data$nodes <- nodes
58 1
  sg
59
}
60

61
#' @rdname cluster
62
#' @export
63
sg_get_cluster <- function(nodes, edges, colors = c("#B1E2A3", "#98D3A5", "#328983", "#1C5C70", "#24C96B"),
64
                       directed = TRUE, algo = igraph::cluster_walktrap, quiet = !interactive(), 
65
                       save_igraph = TRUE, ...){
66
  
67 1
  if (missing(nodes) || missing(edges))
68 1
    stop("missing nodes or edges", call. = FALSE)
69
  
70 1
  edges <- .re_order(edges)
71 1
  nodes <- .re_order_nodes(nodes)
72 1
  g <- .build_igraph(edges, directed = directed, nodes, save = save_igraph)
73
  
74
  # get communities
75 1
  communities <- algo(g, ...)
76 1
  membership <- igraph::as_membership(communities)
77
  
78
  # build color table
79 1
  grps <- unique(membership$membership)
80 1
  n_grps <- length(grps)
81
  
82 1
  if(!isTRUE(quiet))
83 1
    cat("Found #", n_grps, "clusters\n")
84
  
85 1
  colors <- colorRampPalette(colors)(n_grps)
86 1
  colors <- data.frame(
87 1
    grp = as.character(grps),
88 1
    color = colors,
89 1
    stringsAsFactors = FALSE
90
  )
91
  
92
  # merge coms & grps
93 1
  nodes$grp <- as.character(membership$membership)
94 1
  nodes$color <- NULL
95 1
  nodes <- dplyr::inner_join(colors, nodes, by = "grp")
96
  
97 1
  return(nodes)
98
}

Read our documentation on viewing source code .

Loading