我正在尝试使用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))
))