我已经成功地使用 NetworkD3 包来绘制 2 层 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