我正在尝试使用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))
))
也许尝试按以下顺序绘制
condition -> genes -> category
::连接实际上是正确的,桑基图显示了状态 1(源节点)
genes
和状态 2(目标节点)之间的流,然后分别显示了状态 2和状态 3conditions
之间的流。conditions
category
如果将鼠标悬停在流上,则更有意义,例如,您会看到一个值 from
Treatment
gone toCategory1
。然而,仅仅因为流程似乎与以下内容相关Gene3
并不一定意味着它是: