briatte / ggnetwork

Geoms to plot networks with ggplot2
https://briatte.github.io/ggnetwork/
146 stars 28 forks source link

Interaction with ggiraph #6

Open jalapic opened 8 years ago

jalapic commented 8 years ago

Is there a way of adding interactivity such as nodes changing color or tooltip. I have made a limited tooltip to an example using ggiraph. Are there any other examples?

library(ggplot2)
library(rvg)
library(ggiraph)
library(network)
library(sna)
library(ggnetwork)

n <- network(rgraph(10, tprob = 0.2), directed = FALSE)
n %v% "family" <- sample(letters[1:3], 10, replace = TRUE)
n %v% "importance" <- sample(1:3, 10, replace = TRUE)
e <- network.edgecount(n)
set.edge.attribute(n, "type", sample(letters[24:26], e, replace = TRUE))
set.edge.attribute(n, "day", sample(1:3, e, replace = TRUE))

df<-ggnetwork(n, layout = "fruchtermanreingold", cell.jitter = 0.75)
#df$tooltip <- paste0("Node ID = ", df$vertex.names)
df$tooltip <- paste0("Betweenness = ", round(betweenness(n)[df$vertex.names],2))

gg_point_1 <- ggplot(df, aes(x = x, y = y, xend=xend, color=family, yend=yend, tooltip = tooltip) )  +
                     geom_edges(aes(linetype = type), color = "grey50") +
                     geom_nodes(color = "black", size = 8) +
                     theme_blank() +
                     geom_nodetext(aes(label = LETTERS[vertex.names]), fontface = "bold") + 
                     geom_point_interactive(size=5)

# htmlwidget call
ggiraph(code = {print(gg_point_1)}, width = 7, height = 6,  hover_css = "{fill:orange;r:6px;}") 
briatte commented 8 years ago

Thanks for that question. I bookmarked ggiraph some time ago, but am not too much into interactive widgets and did not explore interactivity with ggnetwork.

The vignettes of ggiraph suggest that there are ways to make changes to the hovered part of the widget, as well as to the tooltip.

I will take a further look and add a vignette to the package if I find something interesting.

briatte commented 8 years ago

P.S. @jalapic just found your interactive network viz slides, very impressive job!

jalapic commented 8 years ago

Thanks - love the package- thank you