This is code for a sankey diagram made with networkd3. I've had success with Sankeys before - I'm aiming to create something like this https://susan-wilson.shinyapps.io/2016FederalElectionPreferences/ (although it will be a little more wild because of the nature of the Senate preference system), but I can't work out what my issue is. The code runs without error and then I get a blank viewer.
The Source and Target nodes are zero indexed, and they are consecutive integers. I know that I could have just imported ACT, but this is just an interim test and I plan to use the whole data set later. This code is only a toy example and doesn't plot all the preference flows either.
I'm pretty sure I'm just making a dumb mistake, but I'd be super grateful if someone could point it out to me.
library(tidyverse)
library(data.table)
library(networkD3)
rm(list = ls())
#Download the data from here: https://results.aec.gov.au/20499/Website/External/SenateDopDownload-20499.zip
files <- list.files("~whereveryousavedit/SenateDopDownload-20499", pattern = ".csv", full.names = T)
SenatePreferences <- lapply(files, fread)
SenatePreferences <- rbindlist(SenatePreferences)
ACT <- SenatePreferences %>%
filter(State == "ACT")
# one node for each politician, while they're still in.
ACT <- ACT %>%
mutate(NameNode = paste(Surname, GivenNm, Count),
Name = paste(Surname, GivenNm)) %>%
group_by(Name) %>%
mutate(Status= case_when(
Status %>% lag() == "Excluded" ~ "Excluded in a previous round",
Status %>% lag() == "Excluded in a previous round" ~ "Excluded in a previous round",
TRUE ~ Status)) %>%
ungroup() %>%
filter(Status !="Excluded in a previous round") %>%
mutate(Node = c(0:(n()-1)))
# For each count i, the source is the round i node, the target is the equivalent node in round 2.
ACT <- ACT %>%
mutate(Source = Node) %>%
group_by(Name) %>%
mutate(Target = Source %>% lead()) %>%
ungroup() %>%
filter(!is.na(Target))
ACT_Sankey <- list(Nodes = ACT %>%
select(NameNode) %>% data.frame(),
Links = ACT %>%
select(Source, Target, VoteTransferred, Name) %>% data.frame()
)
sankeyNetwork(Links = ACT_Sankey$Links , Nodes = ACT_Sankey$Nodes, Source = 'Source',
Target = 'Target', Value = 'VoteTransferred', NodeID = 'NameNode',LinkGroup = 'Name',
fontSize = 12)