javascript - R networkD3 package: node coloring in simpleNetwork() -


the networkd3 package (https://christophergandrud.github.io/networkd3/ , https://cran.r-project.org/web/packages/networkd3/networkd3.pdf) allows user create simple interactive networks (please copy/paste code below rstudio):

# load package library(networkd3)  # create fake data src <- c("a", "a", "a", "a",         "b", "b", "c", "c", "d") target <- c("b", "c", "d", "j",             "e", "f", "g", "h", "i") networkdata <- data.frame(src, target)  # plot simplenetwork(networkdata) 

is there way specify want elements in src vector color, while allowing elements in target vector different color? allow me visually distinguish src nodes target nodes in network.

this functionality doesn't seem supported in simplenetwork() (but i'm hoping me out homebrew script):

enter image description here

a similar not related question asked here: specifying colors each link in force directed network in networkd3::forcenetwork in r

here's how control colour of nodes forcenetwork. notice still won't tell direction of links because nodes source links , target others - you'll need rethink logic somehow. anyway, here's controlling colour of nodes.

# load package library(networkd3) library(dplyr) # make joins easier  # create fake data src <- c("a", "a", "a", "a",          "b", "b", "c", "c", "d") target <- c("b", "c", "d", "j",             "e", "f", "g", "h", "i") networkdata <- data.frame(src, target, stringsasfactors = false)  nodes <- data.frame(name = unique(c(src, target)), stringsasfactors = false) nodes$id <- 0:(nrow(nodes) - 1)   # create data frame of edges uses id 0:9 instead of names edges <- networkdata %>%    left_join(nodes, = c("src" = "name")) %>%    select(-src) %>%    rename(source = id) %>%    left_join(nodes, = c("target" = "name")) %>%    select(-target) %>%    rename(target = id)  edges$width <- 1  # make grouping variable match colours nodes$group <- ifelse(nodes$name %in% src, "lions", "tigers")  # simple default colours forcenetwork(links = edges, nodes = nodes,               source = "source",              target = "target",              nodeid ="name",              group = "group",              value = "width",              opacity = 0.9,              zoom = true)  # control colours js ordinal scale # edited 20 may 2017 updated code renal chesak's answer: colourscale <- 'd3.scaleordinal()             .domain(["lions", "tigers"])            .range(["#ff6900", "#694489"]);'  forcenetwork(links = edges, nodes = nodes,               source = "source",              target = "target",              nodeid ="name",              group = "group",              value = "width",              opacity = 0.9,              zoom = true,              colourscale = js(colourscale)) 

enter image description here


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -