1

I need to create a bipartite graph for consumer-brand relationships.

This is my example data:

datf <- data.frame(Consumers = c("A", "B", "C", "D", "E"),
                   Brands = c("Costa", "Starbucks", "Cafe2U", "Costa", "Costa"))

The following code gives me a network. But I am not sure how I can add a node type attribute to label consumers and brands:

    library(igraph) 
    dat=read.csv(file.choose(),header=TRUE) 
    el=as.matrix(dat) 
    el[,1]=as.character(el[,1])
    el[,2]=as.character(el[,2])
    g=graph.edgelist(el,directed=FALSE) 

I would like to create a bipartite graph with edges that connect each consumer with the brand they like. Ideally, the nodes will be labeled with text.

Could you show me how to do this using library(igraph)?

  • what `library(igraph)` code have you tried? – Nate Jan 22 '18 at 23:15
  • Hi Nate, I read the file in gephi (with two columns- consumers and brands) and exported a graphml file read it igraph. However, that doesn't add the vertext type. I'm trying to visualize brands and consumers in different colors and calculate the interaction between brands and conumers. – Chamil Rathnayake Jan 22 '18 at 23:19
  • that sounds like you are really close to your desired output, it's always good to include the code you have with your questions. That way people are more likely to help and can provide feedback tailored to your current situation – Nate Jan 22 '18 at 23:26
  • This is a code I used to create a similar network.############## library(igraph) dat=read.csv(file.choose(),header=TRUE) #################### (data looks like this Consumer Brand 1 John Costa 2 Jack Costa 3 Wendy Starbucks 4 Alfred Cafe2U 5 Noah Stabucks) ############################## el=as.matrix(dat) el[,1]=as.character(el[,1]) el[,2]=as.character(el[,2]) g=graph.edgelist(el,directed=FALSE)########### This works, but I can't figure out a way to assign attributes/labels (consumers and brands) – Chamil Rathnayake Jan 22 '18 at 23:39
  • 1
    `mi_graph <- graph_from_data_frame(datf); plot(mi_graph)` gets you a labeled graph. I think you can search SO and find [how to add colors](https://stackoverflow.com/questions/15999877/correctly-color-vertices-in-r-igraph) – Nate Jan 22 '18 at 23:42
  • Thanks very much @Nate. I really appreciate your quick reply. This works fine. And also, is there a way to create a extra node type attribute so that all the people are given the category of "consumers" and all coffee brands and given the category "coffee"? – Chamil Rathnayake Jan 22 '18 at 23:49
  • if you want the graph to be bipartite, you new to assign a boolean vector to an attribute called “type”—eg `V(g)$type <- V(g)$name %in% el[,1]`. If you call the graph it should show that it is bipartite—you will see a B after “IGRAPH” – paqmo Jan 23 '18 at 00:54
  • see [here](https://stackoverflow.com/questions/15367779/how-to-create-a-bipartite-network-in-r-with-igraph-or-tnet) – paqmo Jan 23 '18 at 00:55
  • Thanks @paqmo. That works. – Chamil Rathnayake Jan 23 '18 at 16:49

1 Answers1

3

This resource at Shizuka Lab is really useful for exploring bipartite networks in R with igraph. In short:

library(igraph)

# Your matrix containing consumer choice by brands
m = matrix(data = sample(0:1, 25, replace = TRUE), nrow = 5, ncol = 5)
colnames(m) = c("A", "B", "C", "D", "E")
rownames(m) = c("Costa", "Starbucks", "Cafe2U", "Petes", "Philz")

# Convert it to a bipartitie network
bg = igraph::graph.incidence(m)
bg

# See the vertex attributes 
V(bg)$type 
V(bg)$name

# Plot the network
shape = ifelse(V(bg)$type, "circle", "square") # assign shape by node type
col = ifelse(V(bg)$type, "red", "yellow") # assign color by node type

plot(bg, vertex.shape = shape, vertex.color = col)

Gives:

Nistara
  • 141
  • 1
  • 5