bnlearn + Rgraphviz:自定义图时,双箭头而不是无方向的边 [英] bnlearn + Rgraphviz: double arrows instead of undirected edges when customizing plots
问题描述
我正在尝试使用RGraphviz
自定义通过bnlearn
学习的图的图.当我有无向的边时,当我尝试自定义图形的外观时,RGraphviz
会将它们变成两个方向的有向边.
I am trying to customize a plot of a graph learned with bnlearn
using RGraphviz
. When I have undirected edges, RGraphviz
turns them into directed edges to both directions when I try to customize the appearance of the graph.
可重现的示例可能是:
set.seed(1)
x1 = rnorm(50, 0, 1)
x2 = rnorm(50, 0, 1)
x3 = x2 + rnorm(50, 0, 1)
x4 = -2*x1 + x3 + rnorm(50, 0, 1)
graph = data.frame(x1, x2, x3, x4)
library(bnlearn)
library(Rgraphviz)
res = gs(graph)
options(repr.plot.width=3, repr.plot.height=3)
g1 <- graphviz.plot(res)
图形未自定义:
到目前为止,一切都很好.但是,如果我尝试自定义它:
So far so good. But if I try to customize it:
plot(g1, attrs = list(node = list(fontsize=4, fillcolor = "lightgreen")))
自定义图
无方向的边缘被变换.
即使我只是使用plot(g1),我也会遇到这个问题.问题在于,这种方法(先保存g1,然后使用绘图)似乎会改变图形的外观.
I get this problem even if I just use plot(g1). The issue is that this (saving g1 and then using plot) seems to change the appearance of the graphs.
推荐答案
您可以使用graphviz.plot
的highlight
参数更改某些属性,但是,它似乎不允许更改标签大小.您可以全局设置它,但是这样做会失去控制.
You can change some of the attributes using the highlight
argument of graphviz.plot
, however, it doesn't seem to allow the label size to be altered. You could set this globally, but there is a loss of control doing it this way.
par(cex=0.05)
graphviz.plot(res, highlight =
list(nodes=nodes(res), fill="lightgreen", col="black"))
如您的问题所示,使用attrs
参数作为graphNEL对象的plot方法,您可以得到更多的控制,但是仍然存在方向问题.
As shown in your question, you can get a bit more control, using the attrs
argument for the plot method for the graphNEL object, but again there is the problem of direction.
g <- bnlearn::as.graphNEL(res) # use this to avoid printing of graphviz.plot
plot(g, attrs=list(node = list(fillcolor = "lightgreen", fontsize=4)))
还尝试使用graph.par
全局设置图形图参数,但是,当我尝试这样做时,字体大小会更改,但颜色不会呈现
Also try to set the graph plot parameters globally using graph.par
, however, when I try this, the fontsize changes but the colours don't render
graph.par(list(nodes=list(fill="lightgreen", fontsize=10)))
renderGraph( Rgraphviz::layoutGraph(bnlearn::as.graphNEL(res)))
因此,您可以使用功能nodeRenderInfo
更改graphviz.plot
返回的节点:
So you can alter the nodes returned by graphviz.plot
using the function nodeRenderInfo
:
g1 <- graphviz.plot(res)
graph::nodeRenderInfo(g1) <- list(fill="lightgreen", fontsize=8)
Rgraphviz::renderGraph(g1)
但是,这确实在分配graphviz.plot
时绘制了网络图.因此,您也可以更改graphNEL
对象(由graphviz.plot
返回).默认情况下,graphNEL
对象要么全部是有向的,要么全部是无向的,但是您可以手动调整边缘.这就是graphviz.plot
的作用.通过查看bnlearn:::graphviz.backend
的代码,它可以识别未定向的边缘,并使用graph::edgeRenderInfo(graph.plot)
进行渲染.您可以使用类似的方法执行所需的操作-使用功能nodeRenderInfo
更新节点属性.
This does however, plot the network when assigning graphviz.plot
. So as an alternative you can change the graphNEL
object (returned by graphviz.plot
). By default, a graphNEL
object is either all directed or all undirected, however you can tweak the edges manually. This is what graphviz.plot
does. By looking at the code of bnlearn:::graphviz.backend
, it identifies undirected edges, and the renders them using graph::edgeRenderInfo(graph.plot)
. You can use similar methods to do what you want - use the function nodeRenderInfo
to update node attributes.
所以在一起:
# this sets all edges to directed
g <- Rgraphviz::layoutGraph(bnlearn::as.graphNEL(res))
# set undirected edges
u <- names(which(graph::edgeRenderInfo(g)[["direction"]] == "both"))
graph::edgeRenderInfo(g)[["arrowhead"]][u] = "none"
graph::edgeRenderInfo(g)[["arrowtail"]][u] = "none"
# update node attributes: fill colour and label fontsize
graph::nodeRenderInfo(g) <- list(fill="lightgreen", fontsize=8)
# render
Rgraphviz::renderGraph(g)
这篇关于bnlearn + Rgraphviz:自定义图时,双箭头而不是无方向的边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!