2

Any idea what I'm doing wrong in the below syntax? I'm trying to colour my nodes by the continuous attribute "EM" using a colour gradient. After the last command i get the error:

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

I don't know what this means.

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]
Psidom
  • 209,562
  • 33
  • 339
  • 356
JRO
  • 57
  • 1
  • 7

1 Answers1

1

The error means you're trying to use the [] operator on an object that doesn't recognize it - because it has no subsets. In this case, the object is palf, which is a function. (R calls it a closure, which, in this case, basically means "function object".) What the palf function actually does is give a vector of colors ramping from "gray80" to "darkred", with n elements, where n is the argument you pass it.

I'm a bit unclear why you're using "as.character" instead of "as.numeric" or something, but, supposing EM is a real number, as your question title implies, you could do something like this: (see 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))]
Community
  • 1
  • 1
flies
  • 2,017
  • 2
  • 24
  • 37
  • you may like `two.colors` from the fields package in place of colorRampPalette. – flies Jul 28 '16 at 19:04
  • thank you very much, this has worked! Yes i realised I had made the attribute numeric rather than text by mistake, as I was trying to learn from copying someone else's example syntax – JRO Jul 28 '16 at 19:33