R语言chorddiag包技术问询:为何必须转置起讫点(OD)矩阵?
Great question—this is a common point of confusion with the chorddiag package, and it boils down to a combination of the package's design conventions and how matrix indices are interpreted for chord diagrams. Let’s break it down clearly:
1. The Core Design Rule of chorddiag
First and foremost, chorddiag has an explicit input convention for bipartite chord diagrams:
- Rows of the matrix represent destination nodes (the end points of your flows)
- Columns represent origin nodes (the start points of your flows)
In other words, the value at matrix[row, column] is interpreted as the flow volume from the column node to the row node.
2. Conflict with Standard OD Matrix Structure
Your origin-destination (OD) matrix is almost certainly structured the way most people build them:
- Rows = origin nodes (where flows start)
- Columns = destination nodes (where flows end)
So your OD[origin, destination] value represents the flow from origin to destination—which is the exact opposite of what chorddiag expects. If you don’t transpose, the package will read OD[origin, destination] as flow from destination to origin, hence the backwards arrows you’re seeing.
Quick Example to Illustrate
Suppose your OD matrix looks like this (rows = origins, columns = destinations):
Dest A Dest B Origin X 10 20 Origin Y 15 5
This means 10 units flow from X→A, 20 from X→B, 15 from Y→A, and 5 from Y→B.
If you pass this directly to chorddiag, it will treat rows (X, Y) as destinations and columns (A, B) as origins. The result? It will draw flows from A→X (10), B→X (20), A→Y (15), B→Y (5)—total reverse of what you want.
Transposing the matrix swaps rows and columns:
Origin X Origin Y Dest A 10 15 Dest B 20 5
Now, chorddiag reads rows (A, B) as destinations and columns (X, Y) as origins. The flows will correctly show X→A (10), Y→A (15), X→B (20), Y→B (5)—matching your intended direction.
3. Is There a Mathematical Reason?
Not really a fundamental mathematical requirement—this is purely a design choice by the package authors. However, it does align with how some matrix-based flow visualizations interpret indices (treating rows as "sinks" and columns as "sources"). The key takeaway is that it’s a convention you need to match to get the correct flow direction.
To recap: transpose your OD matrix to swap the origin/destination mapping so it fits chorddiag’s expected input structure, and your flows will display correctly from origin to destination.
内容的提问来源于stack exchange,提问作者salwa

