RnetworkD3Sankey图的左右沉没?

问题描述

我试图借助此页面生成Sankey图: https://www.r-graph-gallery.com/321-introduction-to-interactive-sankey-diagram-2.html

现在,我稍稍修改了数据,想知道我是否可以向右和向左下沉节点,即,顶部节点始终在左侧(对齐),而最后节点始终在右侧。看来networkd3仅具有完全正确的选项。

使用以下代码

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_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),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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...