Skip to content

Instantly share code, notes, and snippets.

@stephlocke
Last active July 10, 2017 10:25
Show Gist options
  • Save stephlocke/3028541b9e2bcdb6b73469af7ebcfe40 to your computer and use it in GitHub Desktop.
Save stephlocke/3028541b9e2bcdb6b73469af7ebcfe40 to your computer and use it in GitHub Desktop.
library(dplyr)
library(readr)
library(DiagrammeR)
staff <- read_csv(
"ID,Name,Title,ManagerID
1,Jane,CEO,
2,Bill,CIO,1
3,Lydia,CTO,1
4,Jack,CMO,1
5,Anne,Data Scientist,2
6,Rhodri,Head of Security,2
7,Chris,Security Analyst,6",
)
staff %>%
mutate(label = paste0(Name, "\n", Title),
shape = "square", type=NA) %>%
select(nodes = ID, label,shape, type) ->
nodes
staff %>%
filter(!is.na(ManagerID)) %>%
select(from = ManagerID, to = ID) ->
edges
# Doesn't work
hierarchy <- create_graph(nodes_df = nodes,
edges_df = edges,
directed = TRUE)
render_graph(hierarchy)
# Sorta works
staff %>%
mutate(label = paste0(Name, "\n", Title),
shape = "square") %>%
select(nodes = ID, everything(), -ManagerID) ->
nodes
hierarchy <- create_graph(nodes_df = nodes,
edges_df = edges,
directed = TRUE)
render_graph(hierarchy)
# Works
hierarchy <- create_graph(nodes_df = create_node_df(7,label = nodes$label,shape=nodes$shape),
edges_df = edges,
directed = TRUE)
render_graph(hierarchy)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment