首页 > 解决方案 > 将不同的 grViz 组合成一个图

问题描述

我想将不同DiagrammeR的图组合成一个图形。这些图的生成如下例所示:

library(DiagrammeR)
pDia <- grViz("
digraph boxes_and_circles {

  # a 'graph' statement
  graph [overlap = true, fontsize = 10]

  # several 'node' statements
  node [shape = box,
        fontname = Helvetica]
  A; B; C; D; E; F

  node [shape = circle,
        fixedsize = true,
        width = 0.9] // sets as circles
  1; 2; 3; 4; 5; 6; 7; 8

  # several 'edge' statements
  A->1 B->2 B->3 B->4 C->A
  1->D E->A 2->4 1->5 1->F
  E->6 4->6 5->7 6->7 3->8
}
")

pDia

在此处输入图像描述

因此,我想将像pDia, class "grViz" "htmlwidget", 这样的图组合成一个带有标签的图像AB等等。我尝试使用该exportSVG函数导出绘图的 svg 文件。因此,将有可能使用magick包来导入和处理此类(即"grViz" "htmlwidget")的不同情节。DiagrammeR但是,此功能在较新版本的软件包中不可用。有什么想法可以将这些图组合在一个可以导出到图形文件的图形中,例如pdfor tiff

标签: rdiagramhtmlwidgets

解决方案


也许以下方法之一将满足您的需求。

  1. 您可以通过将它们添加到同一个digraph调用来添加其他/断开连接的图表。例如,我们可以通过在另一个图之后添加节点和边来添加另外两个图(U -> V -> W 和 X -> Y -> Z)到图的右侧;您只需要确保节点的命名与前面图中的节点不同。但是,这可能会导致大型复杂脚本,并且可能不适合您的工作流程。
library(DiagrammeR)
pDia <- grViz("
digraph boxes_and_circles {

  # your existing graph here

  # 2nd graph
  U -> V -> W;
  
  # 3rd graph
  X -> Y -> Z;
}")

在此处输入图像描述

  1. 鉴于您想要一个静态输出,直接进入graphviz. 组合图的一种方法是将它们作为图像添加到现有节点。例如,如果您将两个图形保存为 png (其他格式)
cat(file="so-65040221.dot", 
" 
digraph boxes_and_circles {
  graph [overlap = true, fontsize = 10]
  node [shape = box, fontname = Helvetica]
  A; B; C; D; E; F
  node [shape = circle, fixedsize = true, width = 0.9] 
  1; 2; 3; 4; 5; 6; 7; 8
  A->1 B->2 B->3 B->4 C->A
  1->D E->A 2->4 1->5 1->F
  E->6 4->6 5->7 6->7 3->8
}")

# This will write out two pngs. We will use these as examples for us to combine
system("dot -Tpng -Gdpi=300 so-65040221.dot -o so-65040221A.png -o so-65040221B.png")

然后创建一个新图以读取 png 并将它们添加到节点

cat(file="so-65040221-combine.dot", 
'graph {
        node [shape=none]
        a [label="", image="so-65040221A.png"];
        b [label="", image="so-65040221B.png"];
}')

我们执行这个并输出到pdf

system("dot -Tpdf so-65040221-combine.dot > so-65040221-combine.pdf")
# or output tiff etc
# system("dot -Ttif so-65040221-combine.dot > so-65040221-combine.tiff")

在此处输入图像描述

然后,您可以通过在组合脚本中排列节点的方式排列多个图表。


推荐阅读