【问题标题】:Combine different grViz into a single plot将不同的 grViz 组合成一个图
【发布时间】:2020-11-27 15:55:54
【问题描述】:

我想将不同的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"grViz" "htmlwidget" 类等图组合成带有 AB 等标签的单个图像。我尝试使用 exportSVG 函数导出绘图的 svg 文件。因此,可以使用magick 包来导入和处理此类的不同图(即"grViz" "htmlwidget")。但是,此功能在较新版本的DiagrammeR 包中不可用。有什么想法可以将这些图组合在一个可以导出到图形文件(例如 pdftiff)的图形中?

【问题讨论】:

  • 对于要组合的图表,您是否能够在一个 dag 调用中定义它们,或者它们是否单独呈现并且您想要组合生成的 pdf?如果您对 pdf(或 png 或 tiff ...等)输出感到满意,为什么不跳过图表生成器而只使用 graphviz?
  • 我想在一张图中组合不同的图表。与cowplot 对ggplots 的处理方式相同。
  • 好的,谢谢。鉴于您提到 pdf 和 tiff,我是否正确,因为您不需要 html 工具/交互?

标签: r diagram htmlwidgets


【解决方案1】:

也许以下方法之一将适合您的需求。

  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 (other formats)
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")

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

【讨论】:

  • ... 只是另一个想法;在您将图形写入 png(就此而言,不是 svg)后,您可以将其读入 r,然后使用常用工具进行组合。
  • 嗨 user20650,你能回答我的问题吗(shttps://stackoverflow.com/questions/68094669/how-to-change-the-font-size-color-and-style-while- setting-engine-htmlwidget) 哪个是关于 htmlwidget 的?我已经尝试了很多。但失败了。
猜你喜欢
  • 2016-03-19
  • 2015-09-25
  • 1970-01-01
  • 2021-12-08
  • 1970-01-01
  • 2016-07-20
  • 1970-01-01
  • 1970-01-01
  • 2020-07-14
相关资源
最近更新 更多