Social network plot

I have a confidential version of my data in which I would like to make a social network using igraph. :

It is a "node list" that is paired with an "edge list" to make a social network using igraph. I however have been difficulty making my data appear weighted, ( I would assume that the individuals within my data set with the highest weights would be centered in a plot, but they have not been in any version I've made. Is there an easy fix to ensure that the igraph plot actually appears weighted instead of random?

Are you aware of igraph layouts ?

Also, what is the meaning of a weight 0 in your context? If it implies no connectivity then it may be sensible to filter it out of the data altogether ?

I am not very familiar with igraph layouts , yes you are correct, filtering all null values would be sensible. Do you have any suggestions as to what igraph layout to use?

Here is an example with fruchterman reingold

library(igraph) 
library(tidyverse)
set.seed(42)
spread_factor <- 20
node_size_factor <-2000

(edgelist <- data.frame(from=sample(letters,100,replace=TRUE),
                       to=sample(letters,100,replace=TRUE)) %>% filter(
                         from!=to
                       ) %>% arrange(from,to))

wgt_df <- enframe(sort(table(unlist(edgelist))^3),
        value="weight")

edgelist2 <- left_join(edgelist,
                       wgt_df,by=c("from"="name")) %>%  left_join(wgt_df,
                                                                  by=c("to"="name")) %>%
  mutate(weight=(weight.x*weight.y)/sum(weight.x*weight.y)/spread_factor)

g <- graph_from_data_frame(edgelist2)
is_weighted(g)

enframe(sort(strength(g))) %>% 
  mutate(name=forcats::as_factor(name)) %>% 
  ggplot(aes(x=as_factor(as.integer(name)),y=value)) + geom_text(aes(label=name))

plot(g,layout= layout_with_fr(g),rescale=TRUE,vertex.size=strength(g)*node_size_factor)


Q and E are the most connected.

Hi nirgrahamuk,

I tried to follow some of the similar code you provided with the example.
I've attached my code below and a figure from it.
There are two issues I keep encountering:

  1. Each node moves location in the plot every time the plot line is rerun
  2. The individuals with the lowest number (1:~10) I hypothesized would be central due to their weights being higher. Is there a modification that can be made in the plot to display this?

library(readr)

Import Data

link <- read_csv("~/Desktop/sting.edge.csv")
node <- read.csv("~/Desktop/sting.node.csv")

examine

head(node)
head(link)
nrow(node); length(unique(node$Name))
nrow(link); nrow(unique(link[,c("From", "To")]))

link <- link[order(link$From, link$To),]
colnames(link)[3] <- "weight"
rownames(link) <- NULL

library('igraph')

Weights

wgt_df2 <- enframe(sort(table(unlist(link))^3),
value="weight")

edgelist2b <- left_join(link,
wgt_df2,by=c("From"="name")) %>% left_join(wgt_df2,
by=c("To"="name")) %>%
mutate(weight=(weight.xweight.y)/sum(weight.xweight.y)/spread_factor)

plotting

g2 <- graph_from_data_frame(link)
is_weighted(g2)
??node_size_factor
enframe(sort(strength(g2))) %>%
mutate(name=forcats::as_factor(name)) %>%
ggplot(aes(x=as_factor(as.integer(name)),y=value)) + geom_text(aes(label=name))
node_size_factor <-.03
plot(g2,layout= layout_with_fr(g2),rescale=TRUE,vertex.size=strength(g2)*node_size_factor,
remove.multiple=F, remove.loops=T, edge.arrow.size=.001, vertex.color="lightblue")

The vertex size is scaled to the strength of the connections yet yours all seem pretty much the same to my eye, if 1 to 10 are stronger it cant be by very much ?

Yes they are, however, the vertices on my plot do not stay in the same location every time I run the same plot. Is this normal?

Yes because layout_with_fr() uses a iterative algorithm with a random initialization you should use set.seed() for reproducibilty

1 Like

Ah, that makes sense. Thank you nirgrahamuk, I appreciate all of your wonderful insight!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.