2

I would like to decrease the distance between nodes in DiagrammeR R package. I am aware that is a similar question here: How to increase distance between nodes in DiagrammeR R. However, such a solution is not working in the current DiagrammeR version (1.0.6.1). I have a found in the manual a given solution:

library(DiagrammeR)
graph <-
  create_graph() %>%
  add_path(
    n = 3,
    type = "path",
    edge_aes = edge_aes(
      style = "solid",
      color = c("yellow", "blue"),
      len= c(10, 0.2), ## Parameter to change edge distance
    ))

render_graph(graph, layout="tree")

As pointed in the code above, the len parameter is supposed to give different distances to the two different edges (i.e. nodes 1-2 to 2-3). However the resulting diagram don´t have different edge distances. It prints as follows:

enter image description here

I am aware that in the dot language (i.e. Graphviz) the distance between nodes is governed by minlen parameter. However, minlen is not a parameter in the edge_aes(). I even tried to modify the source code of the edge_aes() to include a minlen parameter, but it did not work as well:

edge_aesM <- function (style = NULL, penwidth = NULL, color = NULL, arrowsize = NULL, 
          arrowhead = NULL, arrowtail = NULL, fontname = NULL, fontsize = NULL, 
          fontcolor = NULL, len = NULL, tooltip = NULL, URL = NULL, 
          label = NULL, labelfontname = NULL, labelfontsize = NULL, 
          labelfontcolor = NULL, labeltooltip = NULL, labelURL = NULL, 
          edgetooltip = NULL, edgeURL = NULL, dir = NULL, headtooltip = NULL, 
          headURL = NULL, headclip = NULL, headlabel = NULL, headport = NULL, 
          tailtooltip = NULL, tailURL = NULL, tailclip = NULL, taillabel = NULL, 
          tailport = NULL, decorate = NULL, minlen=NULL) 
{
  attr_values <- list(style = style, penwidth = penwidth, color = color, 
                      arrowsize = arrowsize, arrowhead = arrowhead, arrowtail = arrowtail, 
                      fontname = fontname, fontsize = fontsize, fontcolor = fontcolor, 
                      len = len, tooltip = tooltip, URL = URL, label = label, 
                      labelfontname = labelfontname, labelfontsize = labelfontsize, 
                      labelfontcolor = labelfontcolor, labeltooltip = labeltooltip, 
                      labelURL = labelURL, edgetooltip = edgetooltip, edgeURL = edgeURL, 
                      dir = dir, headtooltip = headtooltip, headURL = headURL, 
                      headclip = headclip, headlabel = headlabel, headport = headport, 
                      tailtooltip = tailtooltip, tailURL = tailURL, tailclip = tailclip, 
                      taillabel = taillabel, tailport = tailport, decorate = decorate,
                      minlen=minlen)
  non_null_attrs <- 1:length(attr_values) %>% purrr::map_chr(.f = function(x) {
    if (!is.null(attr_values[[x]])) {
      attr_values[x] %>% names()
    }
    else {
      NA
    }
  })
  non_null_attrs <- non_null_attrs[which(!is.na(non_null_attrs))]
  attr_values[non_null_attrs]
}

Any ideas to make different node distances work in a DiagrammeR package?

user3091668
  • 2,230
  • 6
  • 25
  • 42
  • 1
    Maybe others will have good ideas, but I have not had success either with `edge_aes` if a `layout` is provided in `render_graph`. (If you remove `layout` then it should respect `len`.) I wish I could be of more help. – Ben Feb 04 '21 at 14:08
  • 2
    @Ben is correct.. the function `DiagrammeR::render_graph()` has the argument `layout = tree`, it calls the function `igraph::layout_with_sugiyama()` to calculate the coordinates of the nodes. This function uses a default vgap of 1. So all length arguments are ignored in the tree-layout. – Wimpel Feb 09 '21 at 16:30

0 Answers0