rvisnetwork

R visNetwork: adding edge types and arrow placement


I need to produce a network graph where (A) some nodes are a subcategory of other nodes, (B) some nodes are considered equivalent to other nodes, and (C) some nodes are specifically highlighted as being especially distinct from other nodes. Edges showing relationship A might have a uni-directional arrow (i.e., an arrowhead at only one end of the edge). Edges showing relationship B might have bi-directional arrows (i.e., an arrowhead at each end of the edge), or might have no arrowheads at all. Edges showing relationship C might be shown in a distinct color, or might be dashed, or might have opposing arrowheads along the edge, etc. For the sake of argument, let's say that for relationships of type B I wanted an arrowhead at each end of the relevant edge. How would I go about doing that with visNetwork?

Simon Larson previously answered a similar question in the sole response to this post. He proposed setting individual arrow types in the edges data frame by adding columns arrows.to.type and arrows.from.type, as shown in the following example:

library(visNetwork)
library(magrittr)

nodes <- data.frame(id=c("a","b","c","d"), label=c("a","b","c","d"))
edges <- data.frame(
  from = c("a","a","a"),
  to = c("b","c","d"),
  arrows.from.type = c(NA,"circle","circle"),
  arrows.to.type = c("arrow","circle",NA)
)

visNetwork(nodes, edges)

In his response, the above code produced this network graph, which has the expected arrows and circles at the edge end-points: expected result

When I run the above code, I get the following network graph, which has no arrows or circles at the edge end-points: actual result, with now shapes on edges

Any idea why I'm not getting the desired result? Deleting the arrows.from.type and arrows.from.type specifications and replacing them with arrows = c("to", "to;from", "from") produces a workable result (shown below), but it is much less flexible than the code originally proposed by Simon in the linked post.

It would be great to be able to selectively place different shapes at different points along the edge. This is workable result, but not ideal: 3

Additionally, I'd be open to hearing other means of graphing the types of relationships I described, especially if there are common conventions for illustrating a negative relationship.


Solution

  • It has been around six years since that post you linked to, and it appears visNetwork has changed how edge arrows need to be enabled:

    library(visNetwork)
    
    nodes <- data.frame(id = c("a", "b", "c", "d"), 
                        label = c("a", "b", "c", "d"))
    
    edges <- data.frame(from = c("a", "a", "a"),
                        to = c("b", "c", "d"),
                        arrows.from.enabled = c(F, T, T), 
                        arrows.from.type = c(NA, "circle", "circle"),
                        arrows.to.enabled = c(T, T, F), 
                        arrows.to.type = c("arrow", "circle", NA))
    
    visNetwork(nodes, edges)
    

    1