1

I am working on a network graph using a .csv database containing companies and projects. I can make network graphs showing how all these companies are connected through their projects (one company works on several projects). My .csv database looks like this:

ProjectName CompanyName Year
Project 1 Company A 2020
Project 1 Company B 2020
Project 1 Company C 2020
Project 2 Company A 2021
Project 2 Company C 2021
Project 2 Company D 2021

I am using a conversion to matrix and matrix multiplication to generate an adjacency graph, but using this conversion the year column is lost (I cut it off on import). How can I make a network graph connecting companies working together, with colored edges showing the project years (2 blocks of 4 years) in which they cooperate?

I currently use this for a 'normal' network plot containing all years:

twomode_edgelist<- read.csv("database.csv", header=T, sep = ";", colClasses=c(NA,NA,"NULL")
twomode_table <- table(twomode_edgelist) 
twomode_matrix <- as.matrix(twomode_table)
onemode <- as.matrix (t(twomode_matrix) %*% twomode_matrix)
diag(onemode) <- 0
g <- graph.adjacency(onemode1, mode=c("undirected"), weighted=TRUE)
plot(g)

Thanks a lot for thinking with me, I have never used R before and making the first graph took me way to much time!

T. Zuidema
  • 11
  • 1

2 Answers2

0

By default, an adjacency matrix will only allow you to store one value per cell. If you want to have multiple observations (i.e. multiple year-edges) graphing from a tidy/long data frame is an easier option. In this example, I also use {ggraph()} which is a handy package to graph networks through a grammar of graphics approach.

library(tidyverse)
library(igraph)
library(ggraph)


df <- data.frame(project = c("p1", "p2", "p3", "p2", "p3", "p4"), 
       company = c("A", "B", "C", "A", "C", "D"), 
       year = c(2020, 2020, 2020, 2021, 2021, 2021))

igraph::graph_from_data_frame(df, directed = FALSE) %>%
  ggraph() + 
  geom_edge_arc(aes(colour = as.factor(year))) +
  geom_node_label(aes(label = name)) + 
  theme_graph()

enter image description here

Nicolás Velasquez
  • 5,623
  • 11
  • 22
0

What about the output given by the code below?

graph_from_data_frame(twomode_edgelist) %>%
  set_edge_attr("color", value = as.factor(twomode_edgelist$Year)) %>%
  set_edge_attr("label", value = twomode_edgelist$Year) %>%
  plot()

enter image description here

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81