assign_node_colors: Assign node colors based on a set of attributes

View source: R/assign_node_colors.R

assign_node_colorsR Documentation

Assign node colors based on a set of attributes

Description

This function assigns colors to nodes in a graph based on specified attributes.

Usage

assign_node_colors(graph, attributes, custom_colors = NULL)

Arguments

graph

An igraph object. The graph must contain a vertex attribute name.

attributes

A two-column matrix or data frame. The first column must contain node names, and the second column must contain the attributes that colors will be assigned off of.

custom_colors

A character vector of colors to be used for different attribute values. If not provided, a default palette from rainbow will be used.

Value

The input graph with updated vertex color attributes.

Examples

  library(igraph)

  # Creating a sample graph
  g <- erdos.renyi.game(10, 0.3)
  V(g)$name <- letters[1:10]

  # Creating a sample attributes data frame
  attributes <- data.frame(
    Node = letters[1:10],
    Attribute = rep(c("Group1", "Group2", "Group3"), length.out = 10)
  )

  # Assigning node colors using default colors
  g <- assign_node_colors(g, attributes)

  # Plotting the graph
  plot(g, vertex.color = V(g)$color)

  ##### Example with custom colors ####

  # Defining custom colors
  custom_colors <- c("red", "yellow", "pink")

  # Assigning node colors
  g <- assign_node_colors(g, attributes, custom_colors)

  # Plotting the graph
  plot(g, vertex.color = V(g)$color)


GephiForR documentation built on Sept. 11, 2024, 8:05 p.m.