Reputation: 11
I want to plot a network object using GGally or ggnetwork and I want to be able to produce a layout where the nodes are grouped by a vertex attribute. I have spent some time searching for a way to do this, but have not figured it out. Can nodes be grouped in the layout by attribute such that all nodes with attribute "a" are in a cluster, nodes with attribute "b" are in a cluster, etc.?
Thanks in advance.
Here are two examples:
library (GGally)
library (ggnetwork)
library (ggplot2)
library (sna)
library (network)
#make a random network with some vertex attributes
abc<-as.network(rgraph(20,1))
abc %v% "kinds" <- letters[1:3]
abc %v% "model" <- LETTERS[12:18]
#plot the network using ggnet2 in library (GGally)
#I want to somehow group the nodes together by a vertex attribute.
#Here I have tried to group nodes by "kinds." How to do this??
ggnet2(abc,
size="degree", size.cut=3,
color = "kinds",
group = "kinds")
#and here is an example using library (ggnetwork)
#set degree as an attribute to call in ggnetwork.
#I could not figure out another way to set size = degree without first
#passing it as a vertex attribute.
abc %v% "deg_4ggnet"<-degree(abc)
abc2<-ggnetwork(abc)
ggplot(abc2, aes(x = x, y = y, xend = xend, yend = yend))+
geom_edges(color = "black") +
geom_nodes(aes(color = kinds, size = deg_4ggnet)) +
theme_blank()
#How to group by vertex attribute "kinds"???
Upvotes: 0
Views: 1475
Reputation: 11
Hey I just started using ggnet2 (I haven't used ggnetwork yet). So far I haven't found a quick and easy way to get the nodes to group the way you are trying to group them. However, I have a few suggestions on things you can do to improve the structure of your graphs.
First, install the RColorBrewer package. Then run the following code:
library(igraph)
library(ggplot2)
library(GGally)
library(sna)
library(network)
library(RColorBrewer)
abc<-as.network(rgraph(20,1))
abc %v% "kinds" <- sample(letters[1:3], 10, replace = TRUE)
ggnet2(abc, color = "kinds", size="degree", size.cut=3, palette="Set3")
ggnet2(abc, color = "kinds", size="degree", size.cut=3, palette="Set3", mode = "circle")
ggnet2(abc, color = "kinds", size="degree", size.cut=3, palette="Set3", mode = "spring")
In the 1st ggnet2 function call I have added a palette parameter. This parameter takes on color palette values that are predefined in the RColorBrewer package. In the 2nd and 3rd ggnet2 calls, I just added the mode paremeter which specifies the way that vertices will be placed in the graph visualization. I know this doesn't completely answer your question but I hope it helps a little bit.
Upvotes: 1