0

如何更改 Diagrammer 图表上边缘标签的颜色?

我可以更改边缘颜色本身,但不能更改文本标签。在此处输入图像描述

玩具示例:

niv <- c("A","B","C","D","E","X","Y")
from <- c("A","A","A","A","B","C","D","E", "X", "B")
to <- c("B","C","D","E","X","X","Y","Y","Y", "C")
temp <- data.table(from=factor(from, levels=niv),
to=factor(to,levels=niv), col=c(rep("blue",5), rep("black",5)))

nodes <-   create_node_df(  n=length(niv), label=niv,  width=0.3) 
edges <- create_edge_df(from = temp$from, to = temp$to, 
rel = "leading_to", label=temp$from, color=temp$col)   
graph <- create_graph(  nodes_df = nodes, edges_df = edges)
render_graph(graph)

知道如何自动避免边缘标签与边缘重叠也会很好吗?目前我必须用 Inkscape 编辑结果。

4

1 回答 1

0

这是您问题的可能解决方案。

library(DiagrammeR)
library(data.table)
niv <- c("A","B","C","D","E","X","Y")
from <- c("A","A","A","A","B","C","D","E", "X", "B")
to <- c("B","C","D","E","X","X","Y","Y","Y", "C")
temp <- data.table(from=factor(from, levels=niv),
to=factor(to,levels=niv), col=c(rep("blue",5), rep("black",5)))

nodes <- create_node_df(n=length(niv), label=niv,  width=0.3)
# Add a vector of colors for fontcolor 
edges <- create_edge_df(from=temp$from, to=temp$to, 
         rel="leading_to", label=temp$from, color=temp$col,
         fontcolor=temp$col) 

graph <- create_graph(nodes_df = nodes, edges_df = edges) 

# Delete the default "layout" graph attribute and
# set direction of graph layout
graphAttr <- get_global_graph_attrs(graph)
graphAttr <- rbind(graphAttr[-1,], 
                   c("rankdir", "LR", "graph"))

graph <- set_global_graph_attrs(graph,
      attr = graphAttr$attr,
      value = graphAttr$value,
      attr_type = graphAttr$attr_type)

render_graph(graph)

在此处输入图像描述

于 2017-11-24T17:16:41.437 回答