首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R (networkD3)桑基图左右下沉?

问R (networkD3)桑基图左右下沉?
EN

Stack Overflow用户
提问于 2020-10-07 03:57:14
回答 1查看 464关注 0票数 0

在这个页面的帮助下,我正在尝试制作一个桑基图:https://www.r-graph-gallery.com/321-introduction-to-interactive-sankey-diagram-2.html

现在,我对数据进行了一些修改,并想知道是否可以向右和向左沉入节点,即顶部节点始终向左(对齐),最后节点始终向右。看起来networkd3只有sinkright选项。

使用以下代码:

代码语言:javascript
复制
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移到左边(同时保持右边对齐)。这个是可能的吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-07 20:26:30

在networkD3开箱即用的情况下,这似乎是不可能的。然而,我发现plotly提供了一个x位置选项,它最终奏效了:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64232973

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档