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

时间:2016-07-01 16:48:44

标签: r d3.js dplyr sankey-diagram networkd3

我已成功使用NetworkD3软件包绘制2层Sankey网络。我创建了一个函数,它接受列源,目标和值的数据帧,并输出一个Sankey图。我使用此功能来帮助快速生成类似的图。我的问题不是关于函数的效率 - 尽管问题的根源可能就在其中。

下面我提供一个可重复的例子。我演示了我的函数如何为两个数据集生成一个SankeyNetwork - z1和amp; 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

2 个答案:

答案 0 :(得分:3)

我认为答案应该是交叉发布的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)

答案 1 :(得分:0)

尽管做了很多繁琐的逐步工作来解决这个问题,我还是尴尬地试图扭转我将这两个数据帧组合在一起的顺序。

z3&lt; - rbind(z2,z1)与Sanktify函数一起使用,而z3&lt; -rbind(z1,z2)产生空白图。

不确定原因 - 因为我的函数旨在提供零索引ID#。所以,如果对JS / D3有更好理解的人知道,我很好奇。