0
votes


I have a problem with visualising different nodes in a 2-mode network and hope, someone could help me find the mistake.
I checked the following related Q&A's, How to create a bipartite network in R with igraph or tnet or Converting data form for 2mode network in r but even though, I followed the instructions, it couldn't solve my problem.

So, here is what I did: I have a csv.file with two columns (position & justification). It looks like this:

title of the file: mydata.csv
id  position               justification
[1] contra solidarity  legal regulations
[2] pro solidarity     no justification    
[3] pro solidarity     political solidarity
[4] pro solidarity     political solidarity
[5] pro solidarity     legal regulations
... (in total, 620 observations/rows of 2 variables)

I work with and the packages & and want to create an with different colors for the nodes/vertices. R reads the file and the script works out fine (no errors):

EC_data <- read.csv("Mydata.csv", sep=";")
EC_data

library(statnet)
library(devtools)

euro_network <- network(EC_data, matrix.type="edgelist", sep=";")
euro_network

# overview about the network
summary(euro_network, print.adj=FALSE)

# for 2-mode networks
detach(package:statnet)
library(igraph)

# bipartite/2-mode network is created from edge lists
bn <- graph.data.frame(EC_data, directed=FALSE)
bn

# telling igraph that this is bipartite graph
V(bn)$type <- V(bn)$type %in% EC_data[,1]
bn
plot(bn)

# plotting the network
plot.igraph(bn,layout=layout.fruchterman.reingold)

# creating an vertex attribute to have different colors
V(bn)$type <- V(bn)$name %in% bn[,1]
bn
colors <- c("green", "red")
# green for 'contra solidarity', red for 'pro solidarity'
plot(bn, vertex.color=colors[V(bn)$type+1])

But it doesn't work out. All nodes have the same color and I don't know why. Moreover, I have the feeling that I miss something fundamentally or don't consider fully , because I am just getting started with R and igraph.

So, what is wrong with the data or script?

Any help or suggestions are very welcome!

1

1 Answers

2
votes

Try this instead

library(igraph)

col <- c("#a0d468", "#ffce54")
shape <- c("circle", "square")


d_edgelist <- read.csv("G:\\DATA.csv")

summary(d_edgelist)

library(igraph)
g <- graph.data.frame(d_edgelist, directed = FALSE)
g <- simplify(g)
V(g)$type <- FALSE
V(g)$type[V(g)$name %in% d_edgelist[, 1]] <- TRUE
g.proj <- bipartite.projection(g)
plot(g, layout = layout.bipartite,
     vertex.size = 40,
     vertex.shape = shape[as.numeric(V(g)$type) + 1],
     vertex.color = col[as.numeric(V(g)$type) + 1],
     edge.color = "#333333",
     edge.width = E(g)$weight * 2
)