[英]R-Customized tooltip in networkD3::sankeyNetwork
[英]Multi-layer SankeyNetwork (NetworkD3) does not plot in R
我已經成功使用NetworkD3程序包繪制了2層Sankey Networks。 我創建了一個函數,該函數采用列源,目標和值的數據框並輸出Sankey圖。 我使用此功能來幫助快速生成相似的圖。 我的問題不是關於功能的效率-盡管問題的根源可能在於它。
下面我提供一個可重現的示例。 我演示了我的函數如何為兩個數據集-z1和z2生成SankeyNetwork。 但是,當我將這些數據集與創建3層SankeyNetwork的想法結合在一起時,查看器中沒有任何繪圖(並且我也嘗試增加寬度和高度)。 我猜測這可能與索引編制有關,盡管過去我會收到有關需要零索引編制的錯誤輸出。 我沒有收到任何錯誤,只是一片空白。
library(networkD3)
library(dplyr)
# The function used to create the plots
sanktify <- function(x) {
# Create nodes DF with the unique sources & targets from input
nodes <- unique(data.frame(c(unique(x$source), unique(x$target))))
nodes$ID <- as.numeric(rownames(nodes)) - 1 # sankeyNetwork requires IDs to be zero-indexed
names(nodes) <- c("name", "ID")
# Create two versions of nodes for merging
nodes_source <- nodes
nodes_target <- nodes
names(nodes_source) <- c("source", "source_ID")
names(nodes_target) <- c("target", "target_ID")
# Replace source & target in links DF with IDs
links <- merge(x, nodes_source, by="source", all.x=TRUE) %>%
merge(nodes_target, by="target", all.x=TRUE) %>%
select(source_ID, target_ID, value) %>%
arrange(source_ID)
# Create Sankey Plot
sank <- sankeyNetwork(
Links = links,
Nodes = nodes,
Source = "source_ID",
Target = "target_ID",
Value = "value",
NodeID = "name",
units = "USD",
fontSize = 12,
nodeWidth = 30
)
return(sank)
}
# Creating & plotting first data frame.
z1 <- tbl_df(data.frame(source = c("A", "A", "B", "B"),
target = c("Cardiovascular", "Neurological", "Cardiovascular", "Neurological"),
value = c(5, 8, 2, 10)))
z1$source <- as.character(z1$source)
z1$target <- as.character(z1$target)
sanktify(z1) # Correctly produces plot
# Creating & plotting 2nd data frame
z2 <- tbl_df(data.frame( source = c("Cardiovascular", "Cardiovascular", "Neurological", "Neurological"),
target = c("IP Surg", "IP Med", "IP Surg", "IP Med"),
value = c(3, 7, 6, 1)))
z2$source <- as.character(z2$source)
z2$target <- as.character(z2$target)
sanktify(z2) # Correctly produces plot
# Combining the two dataframes into a new DF with the goal of creating a '3-layer' plot.
z3 <- rbind(z1, z2)
sanktify(z3) # Blank output. No errors in the R console
我相信答案應該在交叉發布的Github問題https://github.com/christophergandrud/networkD3/issues/134中 。 我還將在此處復制並粘貼代碼。 unique
在錯誤的位置,需要在源和目標連接之后運行。
library(networkD3)
library(dplyr)
# The function used to create the plots
sanktify <- function(x) {
# Create nodes DF with the unique sources & targets from input
# ***** changing this is the key***********************************************************
nodes <- data.frame(unique(c(x$source,x$target)),stringsAsFactors=FALSE)
# ************************************************************************************************
nodes$ID <- as.numeric(rownames(nodes)) - 1 # sankeyNetwork requires IDs to be zero-indexed
names(nodes) <- c("name", "ID")
# use dplyr join over merge since much better; in this case not big enough to matter
# Replace source & target in links DF with IDs
links <- inner_join(x, nodes, by = c("source"="name")) %>%
rename(source_ID = ID) %>%
inner_join(nodes, by = c("target"="name")) %>%
rename(target_ID = ID)
# Create Sankey Plot
sank <- sankeyNetwork(
Links = links,
Nodes = nodes,
Source = "source_ID",
Target = "target_ID",
Value = "value",
NodeID = "name",
units = "USD",
fontSize = 12,
nodeWidth = 30
)
return(sank)
}
# use data_frame to avoid tbl_df(data.frame(
z1 <- data_frame(
source = c("A", "A", "B", "B"),
target = c("Cardiovascular", "Neurological", "Cardiovascular", "Neurological"),
value = c(5, 8, 2, 10)
)
z2 <- data_frame(
source = c("Cardiovascular", "Cardiovascular", "Neurological", "Neurological"),
target = c("IP Surg", "IP Med", "IP Surg", "IP Med"),
value = c(3, 7, 6, 1)
)
z3 <- bind_rows(z1,z2)
sanktify(z3)
盡管進行了大量繁瑣的分步工作來解決問題,但我尷尬地從未嘗試顛倒我將兩個數據幀重新綁定在一起的順序。
z3 <-rbind(z2,z1)與Sanktify函數一起工作,而z3 <-rbind(z1,z2)產生空白圖。
不知道為什么-因為我的函數旨在提供零索引ID#。 因此,如果對JS / D3有更好理解的任何人都知道,我很好奇。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.