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

在DiagrammeR/Graphviz中实现xlabel置于节点下方的方法

调整DiagrammeR中流符号标签的居中显示问题

在Quarto编写课程资料时,需要用DiagrammeR(Graphviz的R包)制作系统动力学图,需使用沙漏类流符号(也可通过双三角形实现)。尝试三种方案后选用xlabel方式,但当前xlabel“growth”未对齐在沙漏符号正下方,需调整到正下方位置。

原代码及效果

原代码

library(DiagrammeR)

graph_code <- '
digraph {
  graph [rankdir = "LR"]
  
  node [shape = rect]
  A  B
  
  node [shape=none, width=0, height=0, fontsize=20, fixedsize=true, forcelabels=true]
  C [label="&#10710;", xlabel=<<FONT POINT-SIZE="8">growth</FONT>>]
  
  A -> C [dir = none, color = "blue"]
  C -> B [dir = left, color = "blue"]

}
'

grViz(graph_code)

原效果

原效果:标签未居中在沙漏正下方

解决方案

问题根源在于shape=none且width=0的节点会导致xlabel定位偏移。我们可以直接将沙漏符号和标签整合到同一个节点的HTML格式label中,通过表格实现上下居中对齐,无需依赖xlabel:

修改后的代码

library(DiagrammeR)

graph_code <- '
digraph {
  graph [rankdir = "LR"]
  
  node [shape = rect]
  A  B
  
  node [shape=none, fontsize=20, fixedsize=true]
  # 用HTML表格将沙漏符号和标签上下居中排列
  C [label=<<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
        <TR><TD><FONT POINT-SIZE="20">&#10710;</FONT></TD></TR>
        <TR><TD><FONT POINT-SIZE="8">growth</FONT></TD></TR>
      </TABLE>>]
  
  A -> C [dir = none, color = "blue"]
  C -> B [dir = left, color = "blue"]

}
'

grViz(graph_code)

效果说明

通过HTML表格的无边框、零内边距设置,沙漏符号和“growth”标签会严格上下居中对齐,完美解决xlabel偏移的问题,同时保持流符号与前后节点的连接逻辑不变。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 17:10:04