多层SankeyNetwork (NetworkD3)不在R中绘图



我已经成功地使用NetworkD3包来绘制2层Sankey网络。我创建了一个函数,它接受包含列source、target和value的数据框,并输出Sankey图。我使用这个功能来帮助快速生成类似的图。我的问题不是关于这个函数的效率——尽管我的问题的根源可能在它里面。

下面我提供一个可重复的例子。我演示了我的函数如何为两个数据集生成SankeyNetwork - z1 &z2。然而,当我将这些数据集与创建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有更好的了解,我很好奇。

相关内容

  • 没有找到相关文章

最新更新