0

Hello I am trying to get my nodes to be colored based on a column in a table (if possible). The program I am creating will have set columns but different amount of rows based on the CVS file. I would like to be able to color nodes automatically once file is read. I am not quite sure on how to do this though. Here is my code so far. As you can see I manually entered colors for each node. Any suggestions or links to sites that could help me would be great help, thank you.

library(tidyverse)
library(DiagrammeR)
library(dplyr)
library(igraph)


e <- read.csv("edges.csv",stringsAsFactors  = FALSE)

n <- read.csv("nodes.csv", stringsAsFactors = FALSE)

e$to <- as.character(e$to)
 

create_graph()  %>%
  
  add_nodes_from_table(table=n,label_col = task) %>%
  
  add_edges_from_table(table=e,from_col = from,to_col = to,from_to_map = label) %>%

  set_node_attrs(
    
    node_attr = "shape",
    
    values = "square")%>%
  
  set_node_attrs(

    node_attr = "fixedsize",

    values = FALSE) %>%
  
  set_node_attrs(

    node_attr = "fillcolor",

    values = "#add8e6",

    nodes = c(1)) %>%

  set_node_attrs(
    
    node_attr = "fillcolor",
    
    values = "#07B8B4",
    
    nodes = c(2,3,4)) %>%
  
  set_node_attrs(
    
    node_attr = "fillcolor",
    
    values = "#A7EEC9",
    
    nodes = c(5,6,7,8,9,10)) %>%
  
  set_node_attrs(
    
    node_attr = "fillcolor",
    
    values = "#FC6C85",
    
    nodes = c(11)) %>%
  
  set_edge_attrs(
    
    edge_attr = color,
    values = "#7D7D7D") %>%
  
  
  render_graph(layout = "tree")

Example of diagram

Edges data


e <- structure(list(from = c("Parent 1\\nIdaho\\nAge 23\\n130 lbs", 
"Parent 1\\nIdaho\\nAge 23\\n130 lbs", "Parent 1\\nIdaho\\nAge 23\\n130 lbs", 
"Child 1\\nIowa\\nAge 7\\n85 lbs", "Child 1\\nIowa\\nAge 7\\n85 lbs", 
"Child 1\\nIowa\\nAge 7\\n85 lbs", "Child 3\\nIdaho\\nAge 6\\n70 lbs", 
"Child 3\\nIdaho\\nAge 6\\n70 lbs", "Child 3\\nIdaho\\nAge 6\\n70 lbs", 
"Stuffy 3\\nBrown\\nMonkey"), to = c("Child 1\\nIowa\\nAge 7\\n85 lbs", 
"Child 2\\nKansas\\nAge 7\\n80 lbs", "Child 3\\nIdaho\\nAge 6\\n70 lbs", 
"Stuffy 1\\nRed\\nBear", "Stuffy 2\\nWhite\\nCat", "Stuffy 3\\nBrown\\nMonkey", 
"Stuffy 4\\nRainbow\\nUnicorn", "Stuffy 5\\nGreen\\nSnake", "Stuffy 6\\nPurple\\nElephant", 
"Cat 1\\nBlack&White")), row.names = c(NA, -10L), class = "data.frame")

Nodes data

n <- structure(list(task = c("Parent 1\\nIdaho\\nAge 23\\n130 lbs", 
"Child 1\\nIowa\\nAge 7\\n85 lbs", "Child 2\\nKansas\\nAge 7\\n80 lbs", 
"Child 3\\nIdaho\\nAge 6\\n70 lbs", "Stuffy 1\\nRed\\nBear", 
"Stuffy 2\\nWhite\\nCat", "Stuffy 3\\nBrown\\nMonkey", "Stuffy 4\\nRainbow\\nUnicorn", 
"Stuffy 5\\nGreen\\nSnake", "Stuffy 6\\nPurple\\nElephant", "Cat 1\\nBlack&White"
)), class = "data.frame", row.names = c(NA, -11L))

Colors data

cols <- structure(list(colors = c("#add8e6", "#0078aa", "#00a0b0", "#00c49e", 
"#8be381", "#00baa5", "#00a7bb", "#005bbe", "#663399", "#a15497"
)), class = "data.frame", row.names = c(NA, -10L))

Peter
  • 11,500
  • 5
  • 21
  • 31
Lizzie S
  • 1
  • 1
  • 1
    Images are not a good way for posting data (or code). See [this Meta](https://meta.stackoverflow.com/a/285557/8245406) and a [relevant xkcd](https://xkcd.com/2116/). Can you post sample data in `dput` format? Please edit **the question** with the output of `dput(df)`. (Note: `df` is the name of each of your datasets.) – Rui Barradas Apr 09 '21 at 18:44
  • @RuiBarradas is this the correct format? Sorry I am very new to R and I just joined stackoverflow yesterday... – Lizzie S Apr 09 '21 at 19:20

0 Answers0