Skip to content

Instantly share code, notes, and snippets.

@martinctc
Created April 22, 2020 09:53
Show Gist options
  • Save martinctc/827001b05ca0fae7226866cdcf1d880a to your computer and use it in GitHub Desktop.
Save martinctc/827001b05ca0fae7226866cdcf1d880a to your computer and use it in GitHub Desktop.
[ForceNetwork network example with {networkD3}] #R
library(tidyverse)
library(networkD3)
## Nodes data frame describing all the nodes in the network
## The first entry in nodes dataframe is node 0, the next entry is node 1 and so on.
## The nodes dataframe must be sorted according to this sequence.
## This is the only way to tie the nodes dataframe to the links dataframe.
TestNodes <- data.frame(name = c("Alpha",
"Beta",
"Cat",
"Dog",
"Elephant",
"Greek alphabets",
"Animals"),
group = c(1, 1, 2, 2, 2, 1, 2),
value = rep(1, 7))
## Each entry in the source and target of the links dataframe is a node (integers 0,1,...,n).
TestLinks <- data.frame(source = 0:4,
target = c(5, 5, 6, 6, 6),
value = rep(1, 5))
## Plot
forceNetwork(Links = TestLinks, Nodes = TestNodes,
Source = "source", Target = "target",
Value = "value", NodeID = "name",
Group = "group", opacity = 0.8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment