4

I am looking at a Twitter network of United States senators. I am looking to be able to select a senator (with nodeIdSelection) and only highlight the nodes connected to that selected node AND highlight only these connected edges (this is what I cannot figure out). I do not want edges between other connected nodes shown. When I set degree = 0, this gives me the result I am looking for in terms of only highlighting the connected edges, but the connected nodes (circles) are not shown.

visNetwork(nodes, edges) %>% 
 visIgraphLayout(layout = "layout_with_fr") %>% 
 visOptions(highlightNearest = list(enabled = TRUE, algorithm = "all",
                                    degree = 0, hideColor = "rgba(0,0,0,0)"), 
                                    nodesIdSelection = TRUE) %>% 
 visInteraction(navigationButtons = TRUE)

This code gives me:

When I set degree = 1, the correct connected nodes are shown, but there are also secondary connected edges between other nodes NOT connected to the selected node also shown. Since there are 100 senators and over 2,000 connections, the plot is far too "busy" to visualize with these secondary connections shown.

visNetwork(nodes, edges) %>% 
 visIgraphLayout(layout = "layout_with_fr") %>% 
 visOptions(highlightNearest = list(enabled = TRUE, algorithm = "all",
                                    degree = 1, hideColor = "rgba(0,0,0,0)"), 
                                    nodesIdSelection = TRUE) %>% 
 visInteraction(navigationButtons = TRUE)

This code gives me:

As you can see, the plot becomes far too busy. Is there a way to get the result shown with degree = 0 in terms of only showing edges connected to the selected node but also showing the connected nodes, too?

Thank you so much for any help. Sorry if the images are a bit hard to read. Thanks again.

jp334
  • 165
  • 1
  • 9
  • I was bumping into the same issue. @knapply 's solution below seemed to work correctly for me when highlighting etc. I found though that the fix for me was not coloring my edges. For whatever reason, when I specified color to my edges I got this highlighting issue, but when I didn't specify a color for the edges it worked as expected. – Bryan Shalloway Mar 15 '22 at 22:25

1 Answers1

0

You want degree = 1.

library(igraph)
g <- graph("Zachary")

library(visNetwork)
vis_g <- toVisNetworkData(g)

visNetwork(vis_g$nodes, vis_g$edges) %>%
  visIgraphLayout(layout = "layout_with_fr") %>%
  visEdges(color = list(highlight = "blue", hover = "blue")) %>%   # explicit edge options
  visOptions(highlightNearest = list(enabled = TRUE, degree = 1,
                                     labelOnly = FALSE, hover = TRUE),
             nodesIdSelection = list(selected = 6))

Created on 2018-05-26 by the reprex package (v0.2.0).

knapply
  • 647
  • 1
  • 5
  • 11
  • 1
    Thanks for the response. Greatly appreciated. However, this is still highlighting edges not connected to the selected node (6). I am looking to be able to select a node and only have shown the nodes connected to the selected node AND the edges connected to the selected node. I do not want edges not connected to the selected node to be seen. I'm not sure there's any way of doing this in this package. – jp334 May 26 '18 at 16:04
  • Gotcha. Setting the edge options does the trick. `nodesIdSelection = list(selected = 6)` isn't necessary, but just made showing the intended output easier. – knapply May 26 '18 at 16:39
  • Thanks for the response, but what do you mean by setting the edge options? What is the code to remove the secondary connections when degree = 1? Thanks for the help. Greatly appreciated. – jp334 May 26 '18 at 16:45
  • The answer is updated: `visEdges(color = list(highlight = "blue", hover = "blue"))` – knapply May 26 '18 at 16:46
  • Thanks, but I still can't get it to work for my example. I have a "color" column in my edges df. visNetwork(nodes, edges) %>% visIgraphLayout(layout = "layout_with_fr") %>% visEdges(color = list(highlight = edges$color, hover = edges$color)) %>% visOptions(highlightNearest = list(enabled = TRUE, degree = 1, labelOnly = FALSE, hover = TRUE, hideColor = "rgba(0,0,0,0)"), nodesIdSelection = TRUE) %>% visInteraction(navigationButtons = TRUE) – jp334 May 27 '18 at 02:39
  • Sorry, that didn't format correctly. The color column corresponds to whether the Senators are from the same party (red or blue) or different parties (purple). There is also a width column (corresponding to strength of connection), as well as from and to columns in the edges df. I'm just ready to give up. I'm not sure it's possible. Thanks for the help, though. – jp334 May 27 '18 at 02:42
  • You'd have to provide a subset of the data. – knapply May 27 '18 at 03:19
  • @knapply I think my question [here](https://stackoverflow.com/questions/70312952/r-visnetwork-visoptions-not-highlighting-desired-edge-using-highlightnearest-in) is a similar question, but fully reproducible. – TMo Dec 12 '21 at 00:15