thomasp85 / ggraph

Grammar of Graph Graphics
https://ggraph.data-imaginist.com
Other
1.07k stars 116 forks source link

"Facets through time" #86

Open gertstulp opened 7 years ago

gertstulp commented 7 years ago

Thank you for your amazing package!

I have a question with respect to showing networks across time using facets, where both node and edge characteristics can change over time. I believe that this is currently not available, although it may relate to the igraph-objects that are a bit daunting to me. In essence, I want the different facets to be able to show different node and edge features.

I can best show what I want using the ggnetwork-package (because in this package, igraph/network-objects are turned into dataframes that are more easy to understand):

Packages needed

library(dplyr)
library(sna)
library(ggnetwork)

Creating two (random) networks

Network with 6 nodes and each node has some characteristic-value. One network is from timepoint 2016, and one from 2017

net_2016 <- network(rgraph(6, tprob = 0.3), directed = FALSE) 
net_2016 %v% "characteristic" <- sample(letters[1:2], 6, replace = TRUE) 
net_2016 %v% "year" <- 2016

net_2017 <- network(rgraph(6, tprob = 0.7), directed = FALSE) 
net_2017 %v% "characteristic" <- sample(letters[1:2], 6, replace = TRUE) 
net_2017 %v% "year" <- 2017

Generating dataframe with ggnetwork based on manually specified coordinates of the 6 nodes and create ggplot

First for "year" 2016:

# Specify matrix of coordinates for nodes manually
man_coord_matrix <- as.matrix(data.frame(x_c = c(0.5, 1.0, 1.0, 0.5, 0.0, 0.0),
                              y_c = c(1.00, 0.75, 0.25, 0.00, 0.25, 0.75)))

# "Fortifies" network to data.frame, layout specified with manual coordinates  
df_net_2016 <- ggnetwork(net_2016, layout = man_coord_matrix)

# Create network-plot
ggplot(df_net_2016, aes(x = x, y = y, xend = xend, yend = yend)) +
  geom_edges() +
  geom_point(aes(colour=characteristic), size=10) +
  geom_nodetext(aes(label = vertex.names),
                fontface = "bold", colour="white") +
  theme_blank()

2016

Then for "year" 2017:

# "Fortifies" network to data.frame, layout specified with manual coordinates  
df_net_2017 <- ggnetwork(net_2017, layout = man_coord_matrix)

# Create network-plot
ggplot(df_net_2017, aes(x = x, y = y, xend = xend, yend = yend)) +
  geom_edges() +
  geom_point(aes(colour=characteristic), size=10) +
  geom_nodetext(aes(label = vertex.names),
                fontface = "bold", colour="white") +
  theme_blank()

2017

One could now use grid.arrange or some such to create these plots besides one another, but I would very much like facets to be able to do this. With ggnetwork, we can do this by combining the data.frames and facetting on "year":

# Combine dataframes  
df_net_comb <- bind_rows(df_net_2016, df_net_2017)

# Create network-plot
ggplot(df_net_comb, aes(x = x, y = y, xend = xend, yend = yend)) +
  geom_edges() +
  geom_point(aes(colour=characteristic), size=10) +
  geom_nodetext(aes(label = vertex.names),
                fontface = "bold", colour="white") +
  theme_blank() +
  facet_grid(~year)

combined

So here we have for "one individual" the network across time; both edge- and node-characteristics can change within the facets.

I have a much harder time envisioning something similar for igraph objects, because you can specify either node-attributes or edge-attributes. Also combining different igraph objects seems difficult Perhaps I am missing something fundamental.

Thanks again for your great package.

thomasp85 commented 6 years ago

Thanks - I have thoughts on this but it all ties into a yet to materialise idea about how to handle dynamic graphs in tidygraph - thus im afraid you'd have to wait a bit for it...

gertstulp commented 6 years ago

No worries. Thank you for taking the time to respond.

More importantly, thank you for your amazing work on the R-packages. I use them frequently. Your "generative graph art" is also really cool.

G

On Wed, Jan 10, 2018 at 9:31 PM, Thomas Lin Pedersen < notifications@github.com> wrote:

Thanks - I have thoughts on this but it all ties into a yet to materialise idea about how to handle dynamic graphs in tidygraph - thus im afraid you'd have to wait a bit for it...

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/thomasp85/ggraph/issues/86#issuecomment-356727311, or mute the thread https://github.com/notifications/unsubscribe-auth/AYtdIrtRKf6--NwnLaMd7K_AlWwqGwXBks5tJR4vgaJpZM4PgmEO .

paulb20 commented 5 years ago

Not facets, but presumably one can gganimate these?

kanefos commented 3 years ago

any luck achieving this with continuous versus categorical node attributes? I would like to do precisely what you have shown above @gertstulp, but with changes to node size across the facets. Trying to do this with grid.arrange/ggarange is equally impossible as the size/edge weights seem to get normalised at graph creation (which I cannot disable). Thanks a lot