Reputation: 1875
I have the following example-network:
library(igraph)
library(ggplot2)
library(GGally)
# sample dataframe
df <- data.frame(from = sample(LETTERS, size = 20, replace = TRUE),
to = sample(LETTERS, size = 20, replace = TRUE))
df <- df %>% filter(from != to) # no self-loops
# Create graph
g <- graph_from_data_frame(df, directed = TRUE)
# plot network
ggnet2(g)
Sometimes I still get the error message that ggnet2 cannot plot multiplex graphs. But why exactly is my example a multiplex graph? Can someone please explain this to me?
Furthermore, I have another question: I would like to adjust the size of the nodes according to inbound-centrality (aka: how many connections are going to a node)
library(igraph)
library(ggplot2)
library(GGally)
# sample dataframe
df <- data.frame(from = sample(LETTERS, size = 20, replace = TRUE),
to = sample(LETTERS, size = 20, replace = TRUE))
df <- df %>% filter(from != to) %>% # no self-loops
group_by(to) %>%
mutate(inboundCentrality = n())
# Create graph
g <- graph_from_data_frame(df, directed = FALSE)
# plot network
ggnet2(g, node.color = inboundCentrality)
When I look at the g
object, it tells me that there's an attribute inboundCentrality
. However, ggnet2(g, node.size = inboundCentrality)
doesnt work either.
Thanks for help. I was trying to get it to work quite some time now :(
Upvotes: 1
Views: 217
Reputation: 6990
Responding to your first issue, the reason for the "ggnet2 cannot plot multiplex graphs" error is because your graph has multiple edges of different kinds between some of the nodes. To fix that, wrap your graph in simplify()
:
ggnet2(simplify(g))
A reason why the code works sometimes and not others would be if you are rerunning df <- data.frame(...)
: running sample()
without defining a set.seed()
value means your df will be different every time. So sometimes the random values in your df will result in a multiplex graph, sometimes not. By example:
set.seed(1)
df <- data.frame(from = sample(LETTERS, size = 20, replace = TRUE),
to = sample(LETTERS, size = 20, replace = TRUE))
will result in a non-multiplex graph, wheras:
set.seed.(123)
df <- data.frame(from = sample(LETTERS, size = 20, replace = TRUE),
to = sample(LETTERS, size = 20, replace = TRUE))
does and requires simplification.
Upvotes: 1