2016-07-28 1 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 함수가 실제로 수행하는 것은 n 요소를 사용하여 "gray80"에서 "darkred"로 램핑하는 벡터 벡터를 제공합니다. 여기서 n은 인수를 당신이 그것을 전달합니다.

"as.numeric"대신 "as.character"를 사용하는 이유가 약간 명확하지 않지만 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

당신이 colorRampPalette 대신에 필드 패키지에서'two.colors'을하실 수 있습니다. – flies

+0

대단히 감사합니다. 그래, 나는 다른 사람의 예제 구문을 복사하는 것을 배우려고했기 때문에 실수로 텍스트가 아닌 숫자로 속성을 만들었다는 것을 깨달았다. – JRO

+0

허용하는 것을 잊지 마세요;) – flies