2016-07-28 41 views
1

任何想法我在做什麼錯誤在下面的語法?我試圖通過使用顏色漸變的連續屬性「EM」對我的節點着色。在最後一個命令後我得到錯誤:igraph:連續屬性向量的着色節點

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] 

回答

1

的錯誤意味着你想使用[]操作的對象,不承認它 - 因爲它沒有子集。在這種情況下,對象是palf,這是一項功能。 (R稱爲closure,在這種情況下,它基本上意味着「函數對象」)。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))] 
+0

你可能想從田間地頭包'two.colors'到位colorRampPalette的。 – flies

+0

非常感謝你,這工作!是的,我意識到我已經制作了屬性數字而不是錯誤的文本,因爲我試圖從複製別人的示例語法中學習 – JRO

+0

不要忘記標記爲已接受;) – flies