代码之家  ›  专栏  ›  技术社区  ›  skan

如何更改图表上标签边的颜色并避免重叠?。R

  •  0
  • skan  · 技术社区  · 8 年前

    我可以更改边缘颜色本身,但不能更改文本标签。 enter image description here

    玩具示例:

    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编辑结果。

    1 回复  |  直到 8 年前
        1
  •  0
  •   Marco Sandri    8 年前

    这是你的问题的一个可能的解决方案。

    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)
    

    enter image description here

    推荐文章