igraph:通过连续属性向量为节点着色

igraph: Colouring nodes by continous attribute vector

知道我在以下语法中做错了什么吗?我正在尝试使用颜色渐变通过连续属性 "EM" 为我的节点着色。在最后一个命令之后我得到错误:

Error in palf[V(g)$EM] : object of type 'closure' is not subsettable

我不知道这是什么意思。

library(igraph) # This loads the igraph package
dat=read.csv(file.choose(),header=TRUE,row.names=1,check.names=FALSE) # choose an adjacency matrix from a .csv file
m=as.matrix(dat) # coerces the data set as a matrix
g=graph.adjacency(m,mode="undirected",weighted=NULL) # this will create an 'igraph object'

a=read.csv(file.choose())
V(g)$EM=as.character(a$EM[match(V(g)$name,a$ID)]) # This code says to create a vertex attribute called "EM" by extracting the value of the column "EM" in the attributes file when the ID number matches the vertex name.
V(g)$EM # This will print the new vertex attribute, "EM"

palf <- colorRampPalette(c("gray80", "dark red"))

V(g)$color <- palf[V(g)$EM]

该错误意味着您正尝试在无法识别的 object 上使用 [] 运算符 - 因为它没有子集。在这种情况下,object 是 palf,这是一个函数。 (R 称之为 closure,在这种情况下,基本上意味着 "function object"。)palf 函数实际上做的是给出一个从 "gray80" 到"darkred",包含 n 个元素,其中 n 是您传递给它的参数。

我有点不清楚你为什么使用 "as.character" 而不是 "as.numeric" 之类的东西,但是,假设 EM 是一个实数,正如你的问题标题所暗示的那样,你可以这样做: (参见 Scale a series between two points

range1.100 <- function(x){1 + 99*(x-min(x))/(max(x)-min(x))}
colr <- palf(100);
V(g)$color <- colr[round(range1.100(V(g)$EM))]