隔离“分支”在使用 networkd3 的桑基图中
我正在使用 networkD3
包中的 sankeyNetwork()
来可视化一些数据。我想知道是否有一种方法可以从头到尾“隔离”一个分支,忽略不相关的链接。
示例:我有这个:SankeyGot
我想要提取此: SankeyWant
可重现的示例:
set.seed(9)
df <- tibble(
source = sample(stringr::words, 5) %>% rep(2),
target = c(sample(words, 7), source[1:3]),
values = rnorm(10, 10, 7) %>% round(0) %>% abs)
nodes <- data.frame(names = unique(c(df$source, df$target)))
links <- tibble(
source = match(
df$source, nodes$names) -1,
target = match(
df$target, nodes$names) -1,
value = df$values
)
sankeyNetwork(Links = links, Nodes = nodes, Source = "source",
Target = "target", Value = "value", NodeID = "names",
iterations = 64, sinksRight = F, fontSize = 14)
我希望能够过滤掉例如“名称”,并获取上游和下游所有级别上与其连接的所有内容 - 我将如何去做呢?
I am using sankeyNetwork()
from the networkD3
package for visualizing some data. I was wondering if theres a way to "isolate" a branch from start to finish, ignoring the irrelevant links.
Example: I've got this: SankeyGot
And I want to extract this: SankeyWant
reproducible example:
set.seed(9)
df <- tibble(
source = sample(stringr::words, 5) %>% rep(2),
target = c(sample(words, 7), source[1:3]),
values = rnorm(10, 10, 7) %>% round(0) %>% abs)
nodes <- data.frame(names = unique(c(df$source, df$target)))
links <- tibble(
source = match(
df$source, nodes$names) -1,
target = match(
df$target, nodes$names) -1,
value = df$values
)
sankeyNetwork(Links = links, Nodes = nodes, Source = "source",
Target = "target", Value = "value", NodeID = "names",
iterations = 64, sinksRight = F, fontSize = 14)
I'd like to be able to filter out "name" for example and get everything that connects to that on all levels upstream and downstream - how would i go about doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
计算图中节点的路径并非易事,但
igraph
包可以帮助使用all_simple_paths()
。但是,请注意帮助文件中的警告......(我不知道你的
words
向量是什么,所以我手动重新创建了links
data.frame)Calculating the paths from a node in a graph is non-trivial, but the
igraph
package can help with theall_simple_paths()
. However, heed that warning in the help file...(I don't know what your
words
vector is, so I recreated thelinks
data.frame manually)如果您将 sankeyNetwork 编码为对象,则可以使用 str(object) 将其标识为列表,并使用一个名为
x
的矩阵来保存您的输入df
list_sankey < ;- sankeyNetwork(链接=链接,节点=节点,源=“源”,目标=“目标”,值=“值”,NodeID =“名称”,迭代= 64,sinksRight = F,fontSize = 14)
str(list_sankey)
然后,您可以过滤
x
矩阵以仅约束所需的输入source
和输出目标
节点list_sankey_filter <- list_sankey
list_sankey_filter$x$links <- list_sankey_filter$x$links %>% 过滤器(源%in% c(4, 2, 0), target %in% c(4, 2, 0, 10))
这将为您提供下面的对象。
If you code sankeyNetwork as an object you can use str(object) to identify it as a list, with a matrix called
x
that holds your inputdf
list_sankey <- sankeyNetwork(Links = links, Nodes = nodes, Source = "source", Target = "target", Value = "value", NodeID = "names", iterations = 64, sinksRight = F, fontSize = 14)
str(list_sankey)
You can then filter the
x
matrix to only contrain your desired inputsource
and outputtarget
nodeslist_sankey_filter <- list_sankey
list_sankey_filter$x$links <- list_sankey_filter$x$links %>% filter(source %in% c(4, 2, 0), target %in% c(4, 2, 0, 10))
This then gives you the object below.