Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To make a network diagram with tooltips using igraph, ggraph, and networkD3, follow these steps:

  1. First, create your network graph using igraph. Use the code below to create a simple network graph:

```{r} library(igraph)

edges <- data.frame(from=c(1,2,3), to=c(2,3,1)) nodes <- data.frame(id=1:3, label=c("A","B","C")) graph <- graphfromdata_frame(edges, vertices=nodes, directed=FALSE)

plot(graph)


2. Next, use ggraph to create a layout for your graph. This will arrange your nodes and edges in a visually appealing way. Use the code below to create a layout:

```{r}
library(ggraph)

layout <- ggraph(graph, layout = "circle") + 
  geom_edge_link() +
  geom_node_point() +
  theme_void()

layout
  1. Now, use networkD3 to add tooltips to your graph. Tooltips provide extra information about nodes and edges when you hover over them. Use the code below to add tooltips to your graph:

```{r} library(networkD3)

nodes$tooltip <- c("Color: red", "Color: blue", "Color: green")

networkD3::forceNetwork( Links = data.frame(from = get.edgelist(graph)[,1], to = get.edgelist(graph)[,2]), Nodes = nodes, Source = "from", Target = "to", NodeID = "id", Group = "id", radiusCalculation = JS("Math.sqrt(d.nodesize)+5"), linkDistance = 100, fontSize = 12, opacity = 0.9, zoom = TRUE, legend = TRUE, bounded = TRUE, showLabels = TRUE, linkAlpha = 0.5, nodesize = 20, fontFamily = "serif", linkWidth = 2, linkColour = "grey", NodeProps = "tooltip", NodeColour = "label", colourScale = "{}", legendPosition = "top", legendWidth = 200, legendFontSize = 15 ) ```

  1. Your network diagram with tooltips is now complete! You can modify the layout and tooltips to suit your needs.