I'm currently working on a Sankey plot using NetworkD3 library in R. I have to plot a Sankey with fixed height irrespective of the population.
I tried using the value parameter but was able to do that for first column of nodes but for the other node columns its not working.
Here's the snippet.
library(dplyr)
library(tidyr)
library(data.table)
library(networkD3)
library(htmlwidgets)
df <- read.csv( header = TRUE, as.is = TRUE,
text = 'name,year1,year2,year3,year4
Bob,Hilton,Sheraton,Westin,Hyatt
Bob,Sheraton,Hilton,Westin,Hyatt
John,Four Seasons,Ritz-Carlton,Westin,Sheraton
Tom,Ritz-Carlton,Westin,Sheraton,Hyatt
Mary,Westin,Sheraton,Four Seasons,Ritz-Carlton
Sue,Hyatt,Ritz-Carlton,Hilton,Sheraton
Barb,Hilton,Sheraton,Ritz-Carlton,Four Seasons')
View(df)
links <-
df %>%
mutate(row = row_number()) %>%
gather('column', 'source', -row) %>%
mutate(group = source) %>%
mutate(column = match(column, names(df))) %>%
group_by(row) %>%
arrange(column) %>%
mutate(target = lead(source)) %>%
ungroup() %>%
filter(!is.na(target))
View(links)
links <-
links %>%
mutate(source = paste0(source, '_', column)) %>%
mutate(target = paste0(target, '_', column + 1)) %>%
select(source, target, group)
View(links)
nodes <- data.frame(name = unique(c(links$source, links$target)))
View(nodes)
links$source <- match(links$source, nodes$name) - 1
links$target <- match(links$target, nodes$name) - 1
links$value <- 1
drop(grp)
grp <- data.frame(source=links$source)
View(grp)
grp_2<-
grp %>% count(source)
View(grp_2)
colnames(grp_2)[which(names(grp_2) == "n")] <- "n_source"
colnames(grp_2)[which(names(grp_2) == "source")] <- "source"
View(grp_2)
links <- merge(x = links, y = grp_2, by = "source", all = TRUE)
links$value <- links$value/links$n_source
View(links)
nodes$name <- sub('_[0-9]+$', '', nodes$name)
nodes$group <- nodes$name
View(nodes)
View(links)
sankeyNetwork(sinksRight = FALSE,Links = links, Nodes = nodes,
Source = 'source', Target = 'target', Value = 'value',
NodeID = 'name',nodePadding = 1,
LinkGroup = "group", NodeGroup = "group",fontSize = 8.7)
Any sort of guidance or help would go a long way! Thanks in advance!