I am trying to plot several causal loop diagrams based on a data set that contains to, from, polarity, and group, the group a variable belongs to. In the diagram I want the nodes of each group to have different colors. These colors are shown in the legend. Even though I specify in the visGroups parameter of VisNetwork the name and color of each group, the nodes in my diagram default to blue. What can I do to make each node the color of their corresponding group?
This code worked a year ago (early 2023). I updated VisNetwork (April 2024), I updated R (May 2024). Still the code does not work. I looked at other posts with the same problem, but none were helpful.
Below is my code with a simplified example of my data. In the original code I loop through a list of nodes and edges.
#sample data set
id <- c(1,2,3,4,5,6)
label <- c("A","B","C","D","A","A")
group <- c("X,X,Y,Z,X,X")
color.border <- c("black","black","black","black","black","black")
borderWidth <- c(2,2,2,2,2,2)
article <- c("Einhorn 2021", "Einhorn 2021", "Einhorn 2021",
"Kunst & Hohle 2016","Kunst & Hohle 2016","Kunst & Hohle 2016")
nodes <- data.frame(id,label,group,color.border,borderWidth,article)
from <- c(3,4,5,6,3,4)
to <- c(1,2,1,2,1,2)
dashes <- c(FALSE,FALSE,FALSE,TRUE,TRUE,FALSE)
color <- c("black","black","black","black","grey","grey")
width <- c(1,1,1,1,1,1)
edges <- data.frame(from,to,dashes,color,width,article)
#set up legend and plotting of nodes and edges
nodes_DONElevel1 <- c("environment", "interpersonal", "individual", "policy",
"behaviour")
myPalette <- c("lightpink", "lightgreen", "lightgrey", "lightskyblue", "white")
nodes_colors <- myPalette
legend_nodes <- data.frame(label = nodes_DONElevel1,
shape = "ellipse",
color = list(background = myPalette, border = "black",
highlight = "black"),
title = "nodes_information")
edges_labels <- c("positive feedback", "negative feedback")
edges_colors <- c("black", "grey")
legend_edges <- data.frame(color = edges_colors,
label = edges_labels,
arrows = c("to","to"),
dashes = c(FALSE,TRUE),
font.align = "top")
#plot each article's data (nodes, edges) in a CLD
CLD <- visNetwork(nodes, edges,
main = title, width = "100%") %>%
visIgraphLayout(layout = "layout_in_circle",
randomSeed = 1,
smooth = TRUE) %>%
visEdges(arrows = list(to = list(enabled = TRUE, scaleFactor = 0.75)),
color = list(color = "grey")) %>%
visNodes(shape = "ellipse") %>%
visGroups(groupname = "A", color = myPalette[1]) %>%
visGroups(groupname = "B", color = myPalette[2]) %>%
visGroups(groupname = "C", color = myPalette[3]) %>%
visGroups(groupname = "D", color = myPalette[4]) %>%
visLegend(addEdges = legend_edges,
addNodes = legend_nodes, width = 0.25,
useGroups = FALSE, position = "left", main = "Legend", ncol = 2)
print(CLD)
Christa Blokhuis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.