错误摘要: as.facor() 在应用于 D1 数据框
的 role 列时出错说明: 目标是根据 D1 数据框的列角色 [F, S, A] 在 V2 中设置颜色。 可视化 V2 由具有 3 列和 300 多行的 D2 数据框组成
没有颜色编码的可视化如下:
代码片段如下:
library(igraph)
# input data
edges <- read.csv(
file.choose(),header = TRUE
)
head(edges)
# making adjacency matrix
adj_matrix <- as.matrix(get.adjacency(graph.data.frame(edges)))
head(adj_matrix)
class(adj_matrix)
# converting to igraph object
g=graph.adjacency(adj_matrix, mode="undirected")
V(g)
E(g)
# Generating Centrality measures
degree(g) #degree centrality
betweenness(g) #betweeness centrality
closeness(g) #closeness centrality
evcent(g)
#plotting the graph
color_graph <- read.csv( file.choose(), header = TRUE)
#head(color_graph)
# making adjacency matrix
color_graph_adj_matrix <- as.matrix(get.adjacency(graph.data.frame(color_graph)))
head(color_graph_adj_matrix)
# converting to igraph object
color_graph=graph.adjacency(color_graph_adj_matrix, mode="undirected")
#Creating a vector of color
library(RColorBrewer)
color_set <- brewer.pal(3,"Set1")
vertex_color <- color_set[as.numeric(as.factor(V(color_graph)$role))] #error_line
head(vertex_color)
plot(g, vertex.color)
graph.density(g) #get a density of graph
diameter(g, directed=FALSE,weights = NA)