R 和plotly 中的桑基图:意外的连接

问题描述 投票:0回答:1

我正在尝试使用plotly包(plotly_4.10.2)在R中构建三层桑基图。尽管从“链接”数据来看,从源到目标的连接似乎是合理的,但绘图本身显示的连接不正确。

例如,“example.data” -> Gene3-Treatment-Catogory2 显示为 Gene3-Treatment-Category1,Gene8 的连接也是错误的。在绘图之前我应该重新排列标签吗?

剧情截图

library(plotly)

# this is an example data

example.data <- data.frame(
  genes = c("Gene1", "Gene2", "Gene3", "Gene4", "Gene5", "Gene6", "Gene7", "Gene8", "Gene9"),
  conditions = c("Control", "Control", "Treatment", "Treatment", "Treatment", "Treatment", "Treatment", "Treatment", "Treatment"),
  category = c("Category1", "Category1", "Category2", "Category2", "Category2", "Category2", "Category2", "Category1", "Category2")
)

nodes <- data.frame(name = unique(c(as.character(example.data$genes),
                                    as.character(example.data$conditions),
                                    as.character(example.data$category))))

links <- data.frame(source = match(example.data$genes, nodes$name) - 1,
                    target = match(example.data$conditions, nodes$name) - 1,
                    stringsAsFactors = FALSE)

links <- rbind(links,
               data.frame(source = match(example.data$conditions, nodes$name) - 1,
                          target = match(example.data$category, nodes$name) - 1,
                          stringsAsFactors = FALSE))


plotly::plot_ly(
  type = "sankey",
  domain = list(x =  c(0,1),
                y =  c(0,1)),
  orientation = "h",
  customdata = nodes$name,
  node = list(
    label = nodes$name,
    pad = 15,
    thickness = 15,
    line = list(color = "black",
                width = 0.5)),
  link = list(source = links$source,
              target = links$target,
              value =   rep(1, nrow(links))
  ))
r plotly sankey-diagram
1个回答
0
投票

连接实际上是正确的,桑基图显示了状态 1(源节点)

genes
和状态 2(目标节点)
conditions
之间的流,然后分别显示状态 2
conditions
和状态 3
 之间的流category

如果将鼠标悬停在流上,您会看到,例如,一个值从

Treatment
转到
Category1
,这更有意义。然而,仅仅因为流程似乎与
Gene3
相关,并不一定意味着它是:

© www.soinside.com 2019 - 2024. All rights reserved.