You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用networkD3在R中绘制桑基图的行号匹配问题

关于Energy数据集中links与nodes索引不匹配的问题

Hey there, let's break this down clearly for you!

为啥links里的source/target对应nodes的x-1行?

这事儿其实是索引规则差异导致的:

  • 很多用于绘制能源流图的R包(比如networkD3,这类数据常用它来做可视化)采用0-based索引——也就是节点从0开始计数;
  • 但R本身的数据框默认是1-based行号——第一行的行号是1,不是0。

拿你关注的第62行links数据来说:

  • source=26是0-based索引,对应nodes数据框的第26+1=27行,也就是Thermal generation节点;
  • target=15是0-based索引,对应nodes数据框的第15+1=16行,也就是Electricity grid节点。
    这个525.531 TWh的流量对应关系完全正确,只是两种索引规则差了1而已。

除了手动算x-1,还有啥更省心的解决办法?

这里有两个实用的方法,帮你自动匹配,不用再手动加减:

方法1:给nodes加个0-based的id列

提前给nodes数据框添加一个从0开始的id列,这样links里的source/target就能直接和这个id对应,后续匹配或者可视化都更顺手:

# 给nodes添加0-based的id列
Energy$nodes$id <- 0:(nrow(Energy$nodes)-1)

# 验证你关注的第62行links对应关系
target_node <- Energy$nodes[Energy$nodes$id == Energy$links$target[62], ]
source_node <- Energy$nodes[Energy$nodes$id == Energy$links$source[62], ]

cat("Source node:", source_node$name, "\nTarget node:", target_node$name)

运行这段代码后,会直接输出:Source node: Thermal generation Target node: Electricity grid,一步验证对应关系。

方法2:把links的source/target转换成节点名称

如果只是想快速查看每一行links对应的节点,不用纠结行号,可以直接把索引转成名称:

# 给links添加节点名称列
Energy$links$source_name <- Energy$nodes$name[Energy$links$source + 1]
Energy$links$target_name <- Energy$nodes$name[Energy$links$target + 1]

# 查看第62行的详细信息
Energy$links[62, ]

这样你就能直接看到这一行的source_name是Thermal generation,target_name是Electricity grid,完全不用手动算了。


完整的Energy数据集

Energy$nodes

name
1  Agricultural 'waste'
2  Bio-conversion
3  Liquid
4  Losses
5  Solid
6  Gas
7  Biofuel imports
8  Biomass imports
9  Coal imports
10 Coal
11 Coal reserves
12 District heating
13 Industry
14 Heating and cooling - commercial
15 Heating and cooling - homes
16 Electricity grid
17 Over generation / exports
18 H2 conversion
19 Road transport
20 Agriculture
21 Rail transport
22 Lighting & appliances - commercial
23 Lighting & appliances - homes
24 Gas imports
25 Ngas
26 Gas reserves
27 Thermal generation
28 Geothermal
29 H2
30 Hydro
31 International shipping
32 Domestic aviation
33 International aviation
34 National navigation
35 Marine algae
36 Nuclear
37 Oil imports
38 Oil
39 Oil reserves
40 Other waste
41 Pumped heat
42 Solar PV
43 Solar Thermal
44 Solar
45 Tidal
46 UK land based bioenergy
47 Wave
48 Wind
source target value
1  0  1  124.729
2  1  2  0.597
3  1  3  26.862
4  1  4  280.322
5  1  5  81.144
6  6  2  35.000
7  7  4  35.000
8  8  9  11.606
9  10 9  63.965
10 9  4  75.571
11 11 12 10.639
12 11 13 22.505
13 11 14 46.184
14 15 16 104.453
15 15 14 113.726
16 15 17 27.140
17 15 12 342.165
18 15 18 37.797
19 15 19 4.412
20 15 13 40.858
21 15 3  56.691
22 15 20 7.863
23 15 21 90.008
24 15 22 93.494
25 23 24 40.719
26 25 24 82.233
27 5  13 0.129
28 5  3  1.401
29 5  26 151.891
30 5  19 2.096
31 5  12 48.580
32 27 15 7.013
33 17 28 20.897
34 17 3  6.242
35 28 18 20.897
36 29 15 6.995
37 2  12 121.066
38 2  30 128.690
39 2  18 135.835
40 2  31 14.458
41 2  32 206.267
42 2  19 3.640
43 2  33 33.218
44 2  20 4.413
45 34 1  4.375
46 24 5  122.952
47 35 26 839.978
48 36 37 504.287
49 38 37 107.703
50 37 2  611.990
51 39 4  56.587
52 39 1  77.810
53 40 14 193.026
54 40 13 70.672
55 41 15 59.901
56 42 14 19.263
57 43 42 19.263
58 43 41 59.901
59 4  19 0.882
60 4  26 400.120
61 4  12 46.477
62 26 15 525.531 # 你关注的流量:Thermal generation → Electricity grid
63 26 3  787.129
64 26 11 79.329
65 44 15 9.452
66 45 1  182.010
67 46 15 19.013
68 47 15 289.366

内容的提问来源于stack exchange,提问作者Display name

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 03:47:38