r NetworkD3 Sankey在R中创建非常混乱的链接,即使我的代码应该很好。怎么了?
我正在尝试创建一个Sankey图,但是连接都弄乱了。这是我的数据:
#> A tibble: 61 x 3
#> # Groups: id_2 [55]
#> id_2 origin target
#> <int> <fct> <chr>
#> 1 4 Pendlerkort i app Rejsekort
#> 2 38 Ungdomskort Rejsekort
#> 3 84 Rejsekort Rejsekort
#> 4 89 Rejsekort Pendlerkort i app
#> 5 95 Rejsekort Rejsekort
#> 6 112 Rejsekort Pendlerkort Rejsekort
#> 7 118 Pendler20 Rejsekort med pendler kombi
#> 8 140 Pendlerkort i app Rejsekort
#> 9 167 DSB Orange Pendler20
#> 10 193 Rejsekort Rejsekort
#> # ... with 51 more rows
sankey_plot <-
tibble::tribble(
~id_2, ~origin, ~target,
4L, "Pendlerkort i app", "Rejsekort",
38L, "Ungdomskort", "Rejsekort",
84L, "Rejsekort", "Rejsekort",
89L, "Rejsekort", "Pendlerkort i app",
95L, "Rejsekort", "Rejsekort",
112L, "Rejsekort Pendlerkort", "Rejsekort"
)
然后我运行以下代码:
# create nodes
nodes <- data.frame(
name=c(as.character(sankey_plot$origin),
as.character(sankey_plot$target)) %>% unique()
)
# Get values
sankey_plot <- sankey_plot %>% group_by(origin, target) %>%
summarise(values = n())
sankey_plot <- sankey_plot %>%
dplyr::ungroup()
# Create source and target ID's
sankey_plot$IDsource <- match(sankey_plot$origin, nodes$name)-1
sankey_plot$IDtarget <- match(sankey_plot$target, nodes$name)-1
# Plot
sankeyNetwork(Links = sankey_plot, Nodes = nodes,
Source = "IDsource", Target = "IDtarget",
Value = "values",
NodeID = "name",
# fontSize = 10
nodeWidth = 20,
sinksRight = TRUE)
这给我一个杂乱的情节,看起来像这样:
我不知道我在做什么错。我试图以几种不同的方式创建节点。另外,我不group_by()
,只需让每一行代表每个运动(而不是计数)。情节最终看起来相同
I am trying to create a sankey diagram but the connections turn out all messed up. Here is my data:
#> A tibble: 61 x 3
#> # Groups: id_2 [55]
#> id_2 origin target
#> <int> <fct> <chr>
#> 1 4 Pendlerkort i app Rejsekort
#> 2 38 Ungdomskort Rejsekort
#> 3 84 Rejsekort Rejsekort
#> 4 89 Rejsekort Pendlerkort i app
#> 5 95 Rejsekort Rejsekort
#> 6 112 Rejsekort Pendlerkort Rejsekort
#> 7 118 Pendler20 Rejsekort med pendler kombi
#> 8 140 Pendlerkort i app Rejsekort
#> 9 167 DSB Orange Pendler20
#> 10 193 Rejsekort Rejsekort
#> # ... with 51 more rows
sankey_plot <-
tibble::tribble(
~id_2, ~origin, ~target,
4L, "Pendlerkort i app", "Rejsekort",
38L, "Ungdomskort", "Rejsekort",
84L, "Rejsekort", "Rejsekort",
89L, "Rejsekort", "Pendlerkort i app",
95L, "Rejsekort", "Rejsekort",
112L, "Rejsekort Pendlerkort", "Rejsekort"
)
Then I run the following code:
# create nodes
nodes <- data.frame(
name=c(as.character(sankey_plot$origin),
as.character(sankey_plot$target)) %>% unique()
)
# Get values
sankey_plot <- sankey_plot %>% group_by(origin, target) %>%
summarise(values = n())
sankey_plot <- sankey_plot %>%
dplyr::ungroup()
# Create source and target ID's
sankey_plot$IDsource <- match(sankey_plot$origin, nodes$name)-1
sankey_plot$IDtarget <- match(sankey_plot$target, nodes$name)-1
# Plot
sankeyNetwork(Links = sankey_plot, Nodes = nodes,
Source = "IDsource", Target = "IDtarget",
Value = "values",
NodeID = "name",
# fontSize = 10
nodeWidth = 20,
sinksRight = TRUE)
This gives me a messy plot looking like this:
I have no idea what I am doing wrong. I have tried to create the nodes in a few different ways. Also where I don't group_by()
and simply let each row represent each movement (instead of having a count). The plot ends up looking the same
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
鉴于您提供的示例数据,可以预期循环。例如,第3行代表以同一节点启动和结束的链接,第1行表示遵循“ rejsekort”节点的节点(“ pendlerkort i app”)的链接,然后链接到“ rejsekort”节点再次。
如果您期望的是有不同的节点都具有相同的名称“ Rejsekort”,那么您将不得不区分数据中的这些节点。
Given the sample data you provided, the loops are expected. For instance, line 3 represents a link that starts and ends at the same node, and line 1 represents a link from a node ("Pendlerkort I app") that follows the "Rejsekort" node and then links back to the "Rejsekort" node again.
If what you're expecting is that there are different nodes that all have the same name "Rejsekort", then you will have to distinguish those nodes in your data.