Skip to content

Instantly share code, notes, and snippets.

@tmasjc
Created July 2, 2021 04:08
Show Gist options
  • Save tmasjc/7840588376066a2f71665102c751f33c to your computer and use it in GitHub Desktop.
Save tmasjc/7840588376066a2f71665102c751f33c to your computer and use it in GitHub Desktop.
Network visualization of team projects
library(tidyverse)
library(networkD3)
raw <- pins::pin_get("projects", "local")
df <- raw %>%
mutate(associate = str_split(associate, pattern = ",|、")) %>%
unnest_longer(associate) %>%
filter(!is.na(associate)) %>%
count(owner, associate)
nodes <-
df %>%
select(owner, associate) %>%
unlist() %>%
unique() %>%
data.frame() %>%
set_names("name") %>%
rownames_to_column(var = "id") %>%
mutate(id = as.numeric(id) - 1) %>%
mutate(group = if_else(name %in% df$owner, 1L, 0L)) %>%
as.data.frame()
links <-
df %>%
left_join(nodes, by = c("owner" = "name")) %>%
rename("source" = id) %>%
left_join(nodes, by = c("associate" = "name")) %>%
rename("target" = id) %>%
select(source, target, n) %>%
as.data.frame()
forceNetwork(
Links = links,
Nodes = nodes,
Source = "source",
Target = "target",
Value = "n",
NodeID = "name",
Group = "group",
linkDistance = JS("function(d){return d.value * 40}")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment