diff --git a/NAMESPACE b/NAMESPACE index 26a2aad..ce3388b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -221,6 +221,7 @@ export(graph_size) export(graph_unconn_count) export(group_biconnected_component) export(group_by) +export(group_color) export(group_components) export(group_data) export(group_edge_betweenness) @@ -519,6 +520,7 @@ importFrom(igraph,graph_from_adjacency_matrix) importFrom(igraph,graph_from_data_frame) importFrom(igraph,graph_from_edgelist) importFrom(igraph,graph_from_incidence_matrix) +importFrom(igraph,greedy_vertex_coloring) importFrom(igraph,gsize) importFrom(igraph,harmonic_centrality) importFrom(igraph,has_eulerian_cycle) diff --git a/NEWS.md b/NEWS.md index 11beda4..b31c93e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -31,6 +31,7 @@ * Added `centrality_harmonic()` + deprecated `centrality_closeness_harmonic()`. The latter is an interface to netrankr while the former is a more efficient and flexible igraph implementation. +* Added `group_color()` as an interface to `greedy_vertex_coloring()` in igraph # tidygraph 1.2.3 diff --git a/R/group.R b/R/group.R index e112c7f..b64fb27 100644 --- a/R/group.R +++ b/R/group.R @@ -203,6 +203,17 @@ group_biconnected_component <- function() { group <- rep(seq_along(ind), lengths(ind))[order(unlist(ind))][focus_ind(.G(), 'edges')] desc_enumeration(group) } +#' @describeIn group_graph Groups nodes by their color using [igraph::greedy_vertex_coloring()]. Be aware that this is not a clustering algorithm as coloring specifically provide a color to each node so that no neighbors have the same color +#' @importFrom igraph greedy_vertex_coloring +#' @export +group_color <- function() { + expect_nodes() + graph <- .G() + group <- greedy_vertex_coloring(graph) + + group <- as.integer(group[focus_ind(.G(), 'nodes')]) + desc_enumeration(group) +} # HELPERS ----------------------------------------------------------------- diff --git a/man/centrality.Rd b/man/centrality.Rd index 3717360..a1c51be 100644 --- a/man/centrality.Rd +++ b/man/centrality.Rd @@ -93,7 +93,12 @@ centrality_degree( centrality_edge_betweenness(weights = NULL, directed = TRUE, cutoff = NULL) -centrality_harmonic(weights, mode = "out", normalized = FALSE, cutoff = NULL) +centrality_harmonic( + weights = NULL, + mode = "out", + normalized = FALSE, + cutoff = NULL +) centrality_manual(relation = "dist_sp", aggregation = "sum", ...) diff --git a/man/group_graph.Rd b/man/group_graph.Rd index 49f89ca..6fbe7c5 100644 --- a/man/group_graph.Rd +++ b/man/group_graph.Rd @@ -13,6 +13,7 @@ \alias{group_spinglass} \alias{group_walktrap} \alias{group_biconnected_component} +\alias{group_color} \title{Group nodes and edges based on community structure} \usage{ group_components(type = "weak") @@ -42,6 +43,8 @@ group_spinglass(weights = NULL, ...) group_walktrap(weights = NULL, steps = 4, n_groups = NULL) group_biconnected_component() + +group_color() } \arguments{ \item{type}{The type of component to find. Either \code{'weak'} or \code{'strong'}} @@ -112,6 +115,8 @@ grouping of edges supported is biconnected components. \item \code{group_biconnected_component()}: Group edges by their membership of the maximal binconnected components using \code{\link[igraph:biconnected_components]{igraph::biconnected_components()}} +\item \code{group_color()}: Groups nodes by their color using \code{\link[igraph:greedy_vertex_coloring]{igraph::greedy_vertex_coloring()}}. Be aware that this is not a clustering algorithm as coloring specifically provide a color to each node so that no neighbors have the same color + }} \examples{ create_notable('tutte') \%>\% diff --git a/tests/testthat/test-group.R b/tests/testthat/test-group.R index 085bcf0..8c51ba7 100644 --- a/tests/testthat/test-group.R +++ b/tests/testthat/test-group.R @@ -20,6 +20,7 @@ test_that("grouping returns integer vector", { #expect_type(get_group(gr, group_optimal()), 'integer') expect_type(get_group(gr, group_spinglass()), 'integer') expect_type(get_group(gr, group_walktrap()), 'integer') + expect_type(get_group(gr, group_color()), 'integer') gr1 <- activate(gr, edges) expect_type(get_group(gr1, group_biconnected_component()), 'integer') @@ -37,6 +38,7 @@ test_that("grouping returns integer of correct length", { #expect_length(get_group(gr, group_optimal()), igraph::gorder(gr)) expect_length(get_group(gr, group_spinglass()), igraph::gorder(gr)) expect_length(get_group(gr, group_walktrap()), igraph::gorder(gr)) + expect_length(get_group(gr, group_color()), igraph::gorder(gr)) gr1 <- activate(gr, edges) expect_length(get_group(gr1, group_biconnected_component()), igraph::gsize(gr1)) @@ -56,6 +58,7 @@ test_that("grouping requires correct activation", { #expect_error(get_group(gr1, group_optimal())) expect_error(get_group(gr1, group_spinglass())) expect_error(get_group(gr1, group_walktrap())) + expect_error(get_group(gr1, group_color())) skip_on_os('windows') expect_error(get_group(gr1, group_leading_eigen()))