使用DiagrammeR grViz设置流程图整行节点居中对齐的技术问询
Hey Steve, nice work on your flowchart so far! Getting specific rank rows centered instead of the default left-aligned is totally doable with a small tweak to your Graphviz code—since grViz uses Graphviz under the hood, we can leverage subgraph attributes to control alignment.
The Fix: Wrap Target Ranks in Centered Subgraphs
By default, rank=same groups align left relative to the graph canvas. To center an entire rank row, wrap the nodes in a subgraph and set the center=true attribute for that subgraph. This tells Graphviz to center the entire row of nodes within the graph's width.
Here's your modified code with the 3rd and 4th ranks (your chem/bio/pass/sed/Anc and analysis node groups) set to center alignment:
library("DiagrammeR") grViz(" digraph CFA { # Graph-level settings (optional, for better spacing) graph [nodesep=0.5, ranksep=0.8, compound=true] # Multiple level nodes node [shape = rectangle, color=CornflowerBlue] a [label = <<FONT COLOR='blue' POINT-SIZE='18'><b>Surveillance 2017 </b> <br/>Urban Mixtures</FONT>> ]; # Tiers node [shape = ellipse, color=CornflowerBlue] T1 [label = <<u><b><font color = 'forestgreen' point-size = '16'>Tier 1</font></b></u><br/>Water Quality <br/> 16 Watersheds <br/> Quarterly sampling>]; T2 [label = <<u><b><font color = 'forestgreen' point-size = '16'>Tier 2</font></b></u> <br/> Sediment and Passive sampling <br/> 72 Sites>]; #Media assays and ancillary chem [label = 'Water \nChemistry']; bio [label = 'Bio-effects']; Anc [label = 'Ancillary \ndata'] sed [label = 'Sediment \nChemistry']; pass [label = 'Passive Sampler \nChemistry']; # Analyses node [shape = box, color = Crimson] owc [label = 'Waste Indicators']; PFAS [label = 'PFAS: 30 sites'] PAH [label = 'PAHs']; Att [label = 'Attagene']; Met [label = 'Metabolomics']; Tr [label = 'Transcriptomics']; Hyd [label = 'Hydrology']; WA [label = 'Watershed \nAttributes'] # Synthesis node [shape = 'egg',color = 'forestgreen'] Syn [label = 'Synthesis']; Tx [label = 'ToxEval\nHTS/AOP analysis'] # Rank groups with centering {rank = same; T1 T2} # 3rd rank - centered subgraph rank_3 { rank=same; center=true; chem bio pass sed Anc; } # 4th rank - split into two centered sub-groups subgraph rank_4a { rank=same; center=true; PFAS PAH Hyd owc WA; } subgraph rank_4b { rank=same; center=true; Att Met Tr; } {rank = same; Tx; Syn} # Connect nodes with edges and labels T1 -> a[dir=back] a -> T2#[dir=back] T1 -> {bio chem Anc} T2 -> {bio sed Anc pass chem} Anc -> {Hyd WA} chem -> {owc} chem -> PFAS [label = 'Tier 2'] sed -> {owc PAH} pass -> PAH bio -> {Met Tr} [label = 'Tier 1'] bio -> {Att} [label = 'Tier 2'] {owc Att Met Tr Hyd WA PAH PFAS} -> Syn {owc PAH PFAS} -> Tx Tx -> Syn } ")
Key Changes Explained:
- Wrapped the 3rd rank nodes (
chem bio pass sed Anc) intosubgraph rank_3withcenter=trueto center the entire row. - Split your original 4th rank nodes into two centered subgraphs (
rank_4aandrank_4b) to keep their alignment consistent. - Removed the duplicate
Hydentry in your original analysis rank group to avoid layout glitches. - Added optional graph-level spacing settings (
nodesep,ranksep) to improve overall readability—adjust these values to tweak the gap between nodes/ranks.
This should give you the centered alignment you want for those specific rows while keeping your existing grouping and vertical sorting intact.
内容的提问来源于stack exchange,提问作者Steve

