0

This is code for a sankey diagram made with . 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)
CJ Yetman
  • 8,373
  • 2
  • 24
  • 56
Susan
  • 101
  • 8
  • You almost definitely need to remove the `[1]` in `ACT <- SenatePreferences[1] `, otherwise you end up with empty links and nodes data frames. Assuming that, you end up with a nodes data frame with 476 rows and a links data frame with 476 rows, which seems very unlikely. Your links data frame source and target vectors are incremental numbers, which seems pretty unlikely. So it's pretty hard to tell what you're trying to do. – CJ Yetman May 04 '19 at 08:05
  • Thank you. The intention was to first visualise vote transfer from person to themselves (while they're still in) each round, and then to simulate the transfer between excluded candidates when I got that right but this is the technical answer to what I was asking and, as you point out, it looks pretty shoddy. Back to the drawing board. Thanks. – Susan May 04 '19 at 22:57

1 Answers1

1

here's a working version of what you seem to be trying to do with your code above, though I doubt the result is what you actually want to do...

library(tidyverse)
library(networkD3)

url <- "https://results.aec.gov.au/20499/Website/External/SenateDopDownload-20499.zip"

mytempfile <- tempfile(fileext = ".zip")
download.file(url = url, destfile = mytempfile)

mytempdir <- tempdir()
unzip(mytempfile, exdir = mytempdir)
unlink(mytempfile)

SenatePreferences <-
  list.files(mytempdir, pattern = ".csv", full.names = TRUE) %>% 
  map_dfr(read_csv)
unlink(mytempdir, recursive = TRUE)

cleaned <-
  SenatePreferences %>% 
  as_tibble() %>% 
  filter(State == "ACT") %>% 
  filter(!Surname %in% c("Exhausted", "Gain/Loss")) %>% 
  mutate(Name = paste(Surname, GivenNm)) %>% 
  mutate(NameNode = paste(Name, Count)) %>% 
  select(NameNode, Name, Ticket, round = Count, Status, VoteTransferred) %>% 
  group_by(Name) %>%
  arrange(round) %>% 
  filter(row_number() <= min(which(Status == "Excluded" | row_number() == n()))) %>% 
  ungroup() %>% 
  mutate(Node = row_number() - 1)

links <-
  cleaned %>% 
  mutate(Source = Node) %>% 
  group_by(Name) %>%
  mutate(Target = Source %>% lead()) %>% 
  ungroup() %>% 
  filter(!is.na(Source) & !is.na(Target)) %>% 
  select(Source, Target, Name, VoteTransferred)

nodes <-
  cleaned %>% 
  select(NameNode, Name, Node)

sankeyNetwork(Links = links, Nodes = nodes, Source = 'Source', 
              Target = 'Target', Value = 'VoteTransferred', NodeID = 'NameNode',
              LinkGroup = 'Name', fontSize = 12)

enter image description here

CJ Yetman
  • 8,373
  • 2
  • 24
  • 56