这是用networkd3制作的sankey图的代码。我之前曾在Sankeys上取得过成功-我的目标是创建类似https://susan-wilson.shinyapps.io/2016FederalElectionPreferences/的东西(尽管由于参议院偏好系统的性质,它会变得有些疯狂),但我无法解决我的问题是什么。该代码运行没有错误,然后我得到一个空白查看器。
“源”和“目标”节点的索引为零,并且它们是连续的整数。我知道我可以导入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)
答案 0 :(得分:0)
这是您似乎在尝试使用上面的代码执行的工作版本,尽管我怀疑结果是否是您实际上想要执行的操作...
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)