'bipartite graph for the yelp dataset rstudio

I am trying to produce a bipartite graph for a yelp sample dataset. If the user reviews one restaurant, one edge will connect this user and the restaurant, that is, reviewed == 1, otherwise, reviewed == 0. How can I draw a bipartite graph based on the fact of whether users review the restaurants or not? x represents users; y represents restaurants. There are edges for the pairs (u40, i7), (u40, i9), (u8, i12).

x <- c('u44', 'u44', 'u44', 'u40', 'u40', 'u8')
y <- c('i13', 'i1', 'i9', 'i7', 'i9', 'i12')
reviewed <- c(0, 0, 0, 1, 1, 1)

data.frame(cbind(x, y, reviewed))
    x   y reviewed
1 u44 i13        0
2 u44  i1        0
3 u44  i9        0
4 u40  i7        1
5 u40  i9        1
6  u8 i12        1

Update

Thanks for the help from G5W but I have another question now. The graph has some overlaps and how can I improve my code to make it more readable? Here is my entire dataset: https://drive.google.com/file/d/1rcURZBCOANrKQ4Kl_BmYW26LehrE0wye/view?usp=sharing

My code is shown below.

# Draw a bipartite graph
library(igraph)

g = graph_from_data_frame(data, directed=F)
g = delete_edges(g, which(data$reviewed == 0))

## set up vertex types to support layout as bipartite
V(g)$type = T
V(g)[unique(data$y)]$type  = F
V(g)$type

plot(g, layout=layout_as_bipartite)

enter image description here

The lastest Update

# Draw a bipartite graph
library(igraph)

data <- dput(data)

g = graph_from_data_frame(data, directed=F)
g = delete_edges(g, which(data$reviewed == 0))

## set up vertex types to support layout as bipartite
V(g)$type = T
V(g)[unique(data$y)]$type  = F
V(g)$type

plot(g, layout=layout_as_bipartite)

enter image description here



Solution 1:[1]

I will do this with igraph. It is fairly easy to make the graph connecting all of your user-restaurant pairs, then delete the edges for which reviewed = 0. To get a nice plot, you will want to lay out the vertices as a bipartite graph.

library(igraph)

DF = data.frame(cbind(x, y, reviewed))
g = graph_from_data_frame(DF, directed=F)
g = delete_edges(g, which(DF$reviewed == 0))

## set up vertex types to support layout as bipartite
V(g)$type = T
V(g)[unique(DF$y)]$type  = F
V(g)$type

plot(g, layout=layout_as_bipartite)

Graph laid out as bipartite

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 G5W