在这个页面的帮助下,我正在尝试制作一个桑基图:https://www.r-graph-gallery.com/321-introduction-to-interactive-sankey-diagram-2.html
现在,我对数据进行了一些修改,并想知道是否可以向右和向左沉入节点,即顶部节点始终向左(对齐),最后节点始终向右。看起来networkd3只有sinkright选项。
使用以下代码:
library(networkD3)
library(dplyr)
# A connection data frame is a list of flows with intensity for each flow
links <- data.frame(
source=c("group_A", "group_B", "group_C", "group_C"),
target=c("group_D", "group_C", "group_F", "group_G"),
value=c(3, 4, 3, 1)
)
# From these flows we need to create a node data frame: it lists every entities involved in the flow
nodes <- data.frame(
name=c(as.character(links$source),
as.character(links$target)) %>% unique()
)
# With networkD3, connection must be provided using id, not using real name like in the links dataframe.. So we need to reformat it.
links$IDsource <- match(links$source, nodes$name)-1
links$IDtarget <- match(links$target, nodes$name)-1
# Make the Network
p <- sankeyNetwork(Links = links, Nodes = nodes,
Source = "IDsource", Target = "IDtarget",
Value = "value", NodeID = "name",
fontSize=20)
p给出了以下输出:
它看起来已经很有希望了,但是我想把group_A移到左边(同时保持右边对齐)。这个是可能的吗?
发布于 2020-10-07 20:26:30
在networkD3开箱即用的情况下,这似乎是不可能的。然而,我发现plotly提供了一个x位置选项,它最终奏效了:
fig <- plot_ly(
type = "sankey",
arrangement = "snap",
node = list(
label = nodes$name,
x = c(0.1, 0.1, 0.5, 0.7, 0.7, 0.7),
pad = 10), # 10 Pixel
link = list(
source = links$IDsource,
target = links$IDtarget,
value = links$value))
fig <- fig %>% layout(title = "Sankey with manually positioned node")
fighttps://stackoverflow.com/questions/64232973
复制相似问题