rgraph-theoryigraphdirected-acyclic-graphsvisnetwork

Color a subset of vertices but not their edges with visIgraph


How can I set the color of a subset of the graph nodes without also coloring their edges using visNetwork::visIgraph?

Currently, my function vis_graph_prototyping produces the desired plot except that the selected nodes in green also have their associated edges colored in green as well. How can I have these edges appear in the default color, and only have those edges that I separately select via either E(g, P = as.vector(t(linkages)) or E(g, path = pathway, directed = TRUE) colored in red?

An image of the current output is shown below the reproducible example R snippet.

rnd_dag <- function(p = 25, p_edge = 0.2, weighted = FALSE, seed = 123) {
  if (seed) set.seed(seed)
  A <- matrix(0, p, p)
  A[lower.tri(A)] <- sample(c(0, 1), p*(p-1)/2, replace = TRUE, 
                            prob = c(1 - p_edge, p_edge))
  if (weighted) {A[A == 1] <- runif(length(A[A == 1]), min = -1, max = 1)} 
  return(A)
}

linkages <- matrix(c(9, 1, 
                     12, 1, 
                     11, 2), ncol = 2, byrow = TRUE)

vis_graph_prototyping <- function(A, linkages = NULL, pathway = NULL) {
  g <- graph_from_adjacency_matrix(A, mode = "directed", weighted = TRUE)
  stopifnot(is.null(linkages) || is.null(pathway))
  if (!is.null(linkages)) {
    g <- set_vertex_attr(g, name = "color",
                         index = unique(as.vector(linkages)),
                         value = "green") %>%
      set_edge_attr(name = "color",
                    index = E(g, P = as.vector(t(linkages)), directed = TRUE),
                    value = "red")
  } else if (!is.null(pathway)) {
    g <- set_vertex_attr(g, name = "color", index = pathway, value = "green") %>%
      set_edge_attr(name = "color",
                    index = E(g, path = pathway, directed = TRUE), value = "red")
  }
  visIgraph(g, layout = "layout_with_sugiyama") %>%
    visOptions(highlightNearest = list(enabled = TRUE, hover = TRUE),
               nodesIdSelection = TRUE)
}

vis_graph_prototyping(rnd_dag(12), linkages = linkages)

Current Output from visIgraph


Solution

  • You can set the colors so that you have to set them (instead of inheriting them from the nodes).

    When you create your graph object, set the color to default before you set the edges that will be red. You can find the default colors here.

    The only change I made was in your function. Check it out.

    vis_graph_prototyping <- function(A, linkages = NULL, pathway = NULL) {
      g <- graph_from_adjacency_matrix(A, mode = "directed", weighted = TRUE)
      stopifnot(is.null(linkages) || is.null(pathway))
      if (!is.null(linkages)) {
        # message(print(edges))
        # message(print(linkages))
        g <- set_vertex_attr(g, name = "color",
                             index = unique(as.vector(linkages)),
                             value = "green") %>% 
          set_edge_attr(name = "color", 
                        index = E(g),           # <----- all edges
                        value = "#97C2FC") %>%  # <----- default blue
          set_edge_attr(name = "color",
                        index = E(g, P = as.vector(t(linkages)), directed = TRUE),
                        value = "red") 
      } else if (!is.null(pathway)) {
        g <- set_vertex_attr(g, name = "color", index = pathway, value = "green") %>%
          set_edge_attr(name = "color",
                        index = E(g, path = pathway, directed = TRUE), value = "red")
        message(" from null path ")
        message(E(g))
      }
      visIgraph(g, layout = "layout_with_sugiyama") %>%
        visOptions(highlightNearest = list(enabled = TRUE, hover = TRUE),
                   nodesIdSelection = TRUE) 
    }
    

    enter image description here