color_networks: Color Nodes and Edges of Networks or Alluvial

View source: R/color_networks.R

color_networksR Documentation

Color Nodes and Edges of Networks or Alluvial

Description

[Experimental]

color_networks() takes as input a tibble graph (from tidygraph) or a list of tibble graphs and associates a color for each graphs' edges and nodes, depending on a chosen categorical variable in columnr_to_color (most likely a cluster column).

color_alluvial() takes a data.frame and associates a color to each value of the chosen categorical variable in column_to_color. This function may be used with any data.frame even if it aims at coloring alluvial data frame created with networks_to_alluv().

You may either provide the color palette, provide a data frame associating the different values of the categorical variable with colors, or let the function provide colors (see details).

Usage

color_networks(
  graphs,
  column_to_color,
  color = NULL,
  unique_color_across_list = FALSE
)

color_alluvial(alluv_dt, column_to_color, color = NULL)

Arguments

graphs

A tibble graph from tidygraph or a list of tibble graphs.

column_to_color

The column of the categorical variable to use to color nodes and edges. For instance, the cluster_{clustering_method} created with add_clusters() or the dynamic_cluster_{clustering_method} created with merge_dynamic_clusters().

color

The colors to use. It may be a vector of colors (in a character format) or a two columns data.frame with the first column as the distinct observations of the column_to_color and a second column with the vector of colors you want to use.

unique_color_across_list

If set to TRUE, in a list of tibble graphs, the same categorical variable will be considered as a different variable in different graphs and thus receive a different color. In other words, if set to TRUE cluster "01" in two different graphs will have two different colors. If set to FALSE (by default), cluster "01" will have the same color in every graphs it exists.

alluv_dt

A data.frame of an alluvial created with networks_to_alluv()

Details

The best practice is to provide a list of colors equals to the number of categorical variable to color. If you provide more colors, excess colors will not be used. If you provide less colors, colors will be recycled. If you provide no colors, palette.colors() of base R will be used: the 7 colors of ggplot2 palette will be used (black is excluded) and then the 7 colors of Okabe-Ito palette (black and gray are excluded). Above 14 colors, the colors of the two palettes will be recycled.

Value

The same tibble graph or list of tibble graphs as input, but with a new color column for both nodes and edges.

Examples

library(networkflow)

nodes <- Nodes_stagflation |>
dplyr::rename(ID_Art = ItemID_Ref) |>
dplyr::filter(Type == "Stagflation")

references <- Ref_stagflation |>
dplyr::rename(ID_Art = Citing_ItemID_Ref)

temporal_networks <- build_dynamic_networks(nodes = nodes,
directed_edges = references,
source_id = "ID_Art",
target_id = "ItemID_Ref",
time_variable = "Year",
cooccurrence_method = "coupling_similarity",
time_window = 20,
edges_threshold = 1,
overlapping_window = TRUE,
filter_components = TRUE,
verbose = FALSE)

temporal_networks <- add_clusters(temporal_networks,
objective_function = "modularity",
clustering_method = "leiden",
verbose = FALSE)

temporal_networks <- color_networks(graphs = temporal_networks,
column_to_color = "cluster_leiden",
color = NULL)

temporal_networks[[1]]


agoutsmedt/networkflow documentation built on March 15, 2023, 11:51 p.m.