R (networkD3) 桑基图左下沉和右下沉?
R (networkD3) Sankey Diagram left AND right sinking?
我正在尝试借助此页面生成 Sankey 图:
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
给我这个输出:
Sankey Plot
看起来很有希望,但我想将 group_A 移到左侧(同时保持右侧对齐)。这可能吗?
开箱即用的 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")
fig
我正在尝试借助此页面生成 Sankey 图: 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
给我这个输出:
Sankey Plot
看起来很有希望,但我想将 group_A 移到左侧(同时保持右侧对齐)。这可能吗?
开箱即用的 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")
fig