gpt4 book ai didi

r - 将不同的 grViz 组合成一个图

转载 作者:行者123 更新时间:2023-12-04 08:30:39 25 4
gpt4 key购买 nike

我想将不同的 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

enter image description here

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

最佳答案

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

  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;
}")

enter image description here

  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")

enter image description here

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

关于r - 将不同的 grViz 组合成一个图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65040221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com