【问题标题】:How to construct bipartite graphs using igraph? [duplicate]如何使用 igraph 构建二部图? [复制]
【发布时间】:2018-01-23 10:04:52
【问题描述】:

我需要为消费者-品牌关系创建一个二分图。

这是我的示例数据:

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

下面的代码给了我一个网络。但我不确定如何添加节点类型属性来标记消费者和品牌:

    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) 

我想创建一个带有边的二分图,将每个消费者与他们喜欢的品牌联系起来。理想情况下,节点将用文本标记。

您能告诉我如何使用library(igraph) 来执行此操作吗?

【问题讨论】:

  • 你试过什么library(igraph)代码?
  • 嗨 Nate,我在 gephi 中读取了文件(有两列 - 消费者和品牌)并导出了一个 graphml 文件读取它 igraph。但是,这不会添加顶点类型。我试图用不同的颜色来可视化品牌和消费者,并计算品牌和消费者之间的互动。
  • 听起来你真的很接近你想要的输出,在你的问题中包含你的代码总是好的。这样人们就更有可能提供帮助,并且可以根据您当前的情况提供反馈
  • 这是我用来创建类似网络的代码。############# library(igraph) dat=read.csv(file.choose(), header=TRUE) ###################(数据看起来像这样的消费品牌 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)########### 这行得通,但我想不通一种分配属性/标签(消费者和品牌)的方法
  • mi_graph &lt;- graph_from_data_frame(datf); plot(mi_graph) 为您提供带标签的图表。我认为您可以搜索 SO 并找到 how to add colors

标签: r igraph bipartite


【解决方案1】:

Shizuka Lab 的此资源对于使用igraph 探索 R 中的二分网络非常有用。简而言之:

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)

提供:

【讨论】:

  • 谢谢尼斯塔拉。这很棒。
  • 不客气;如果它对你有用,请接受这个答案!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-31
  • 1970-01-01
  • 2021-12-09
  • 2018-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多