我正在尝试建立一个加权的定向社交网络。我想通过将雄性设为蓝色而雌性设为粉色来将个人的性别纳入该网络。我该怎么做呢?
我的输入数据如下:
"name","id", "gender"
"ERIC",0, "Male"
"CHOICE",1, "Female"
"AMARILLO",2, "Male"
"YERMO",3, "Male"
"MOJITO",4, "Male"
"KESI",5, "Male"
"SUMBRIA",6, "Female"
"NOVIO",7, "Male"
"DIVA",8, "Female"
"CHICO",9, "Male"
"BELLE",10, "Female"
"PEPE",11, "Male"
"CARLOS",12, "Male"
"DIEGO",13, "Male"
"source","target","weight"
"ERIC","CHOICE",17
"ERIC","AMARILLO",3
"AMARILLO","CHOICE",19
"YERMO","CHOICE", 23
"YERMO","MOJITO",0
作为代码,我已经使用过:
install.packages("igraph")
library(igraph)
# Input data
edges <- read.csv(file.choose())
head(edges)
nodes <- read.csv(file.choose())
head(nodes)
library(igraph)
g <- graph.data.frame(edges, directed = T)
g
E(g)
# Make adjacency matrix
g[]
par(mar=c(0,0,0,0))
plot(g)
# Adjust the size and color of the individuals and relationships
plot(g, layout=layout.circle, main="Circle",
vertex.color = "orange",
vertex.label.color = "black",
vertex.size = 10,
edge.color = "orange",
edge.arrow.size= .4,
edge.size = 20,
main="Social network of Yellow-breasted capuchin")
betweenness(g, directed= TRUE)
degree (g, mode="all")
# Nodes aanpassen naar de betweenness
plot(g, layout=layout.fruchterman.reingold(g),
vertex.size=betweenness(g)*1.3,
vertex.color = "orange",
vertex.label.color = "black",
edge.color = "orange",
edge.arrow.size= .4,
edge.size = 20)
g2 = simplify(g)
E(g2)$weight = sapply(E(g2), function(e) {
length(all_shortest_paths(g, from=ends(g2, e)[1], to=ends(g2, e)[2])$res) } )
plot(g2, layout=layout.fruchterman.reingold(g2),
vertex.size=30,
vertex.color = "gold",
vertex.label.color = "black",
vertex.label.dist=0.5,
vertex.label.cex=0.8,
vertex.label.degree=-pi/2,
edge.width=E(g2)$weight*.8,
edge.color = "orange",
edge.arrow.size= 4,
edge.size = 10)
我对R很陌生,所以对这些错误感到抱歉。如果您需要其他信息来解决此问题,请这样说。谢谢!
答案 0 :(得分:1)
欢迎您!您可以尝试一下(您发布了很多代码,因此我创建了与您的代码相似的通用代码):
library(igraph)
# define the data for the graph
g <- graph_from_data_frame(edges, directed = TRUE, vertices =nodes)
# add the color: if Male, lightblue, else pink
V(g)$color <- ifelse(V(g)$gender == "Male", "lightblue", "pink")
现在您可以绘制图形了:
plot(g,
vertex.color = V(g)$color,
vertex.label.color = "black",
vertex.size = 10,
edge.color = "orange",
edge.arrow.size= .4,
edge.size = 0.4)
有数据:
nodes <- structure(list(name = structure(c(8L, 5L, 1L, 14L, 10L, 9L, 13L,
11L, 7L, 4L, 2L, 12L, 3L, 6L), .Label = c("AMARILLO", "BELLE",
"CARLOS", "CHICO", "CHOICE", "DIEGO", "DIVA", "ERIC", "KESI",
"MOJITO", "NOVIO", "PEPE", "SUMBRIA", "YERMO"), class = "factor"),
id = 0:13, gender = structure(c(2L, 1L, 2L, 2L, 2L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 2L, 2L), .Label = c("Female", "Male"), class = "factor")), class = "data.frame", row.names = c(NA,
-14L))
edges <-structure(list(source = structure(c(2L, 2L, 1L, 3L, 3L), .Label = c("AMARILLO",
"ERIC", "YERMO"), class = "factor"), target = structure(c(2L,
1L, 2L, 2L, 3L), .Label = c("AMARILLO", "CHOICE", "MOJITO"), class = "factor"),
weight = c(17L, 3L, 19L, 23L, 0L)), class = "data.frame", row.names = c(NA,
-5L))