networkD3 Sankey 图没有出现在查看器中,没有错误消息

networkD3 Sankey diagram doesn't appear in the viewer, no error message

这是用 . I've had success with Sankeys before - I'm aiming to create something like this https://susan-wilson.shinyapps.io/2016FederalElectionPreferences/ 制作的 sankey 图的代码(尽管由于参议院偏好系统的性质,它会更疯狂一些),但我无法弄清楚我的问题是什么.代码运行没有错误,然后我得到一个空白的查看器。

源节点和目标节点是零索引的,它们是连续的整数。我知道我可以只导入 ACT,但这只是一个临时测试,我打算稍后使用整个数据集。此代码只是一个玩具示例,也没有绘制所有偏好流。

我很确定我只是犯了一个愚蠢的错误,但如果有人能向我指出,我将不胜感激。

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)

这是您似乎要对上述代码执行的操作的工作版本,但我怀疑结果是否是您实际想要执行的...

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)