I have an probably really simple problem. I have a matrix of distances MD (see example below) between a group of 6 people. They are sorted into three groups:
ClassA <- c("TOM","EVE","STEVE")
ClassB <- c("JANE","HANNAH")
ClassC <- c("ADAM")
matrix:
TOM JANE ADAM EVE STEVE HANNAH
TOM 0.0000000 0.8383965 1.1979821 1.186857 1.3500972 1.125554
JANE 0.8383965 0.0000000 1.2319391 1.274157 1.3964041 1.623064
ADAM 1.1979821 1.2319391 0.0000000 1.278604 0.8377628 1.432721
EVE 1.1868572 1.2741569 1.2786041 0.000000 1.4310217 1.328200
STEVE 1.3500972 1.3964041 0.8377628 1.431022 0.0000000 1.465991
HANNAH 1.1255540 1.6230643 1.4327214 1.328200 1.4659912 0.000000
Now I want to plot a minimal spanning tree:
g <- graph.adjacency(as.matrix(MD), weighted=TRUE)
V(g)$label <- V(g)$name
mst <- as.undirected(minimum.spanning.tree(g))
My question is: how to add speciefied colors to verticles belonging to the same group? It's quite simple in the example,
V(g)[1]$color <- "red"
V(g)[2]$color <- "blue"
V(g)[3]$color <- "green"
however, the complete matrix has over 100 pupils, therefore I need something more efficient. I tried to sort it, but my function crashed.
Thank you MrFlick! It's really simple yet effective. Do you perhaps know how to add legend in rglplot? I use it because mst graphs looks better in rgl than in plot, however its main purpose are 3D plots and unfortunately I haven't found any informations how to add legend to 2D plots.
> library(rgl)
> rgl.open()
> rgl.bg(sphere=FALSE, color=c("white"), lit=FALSE)
> rglplot(mst,vertex.size = 3, layout = myLayout, vertex.label.font=10, vertex.label.dist=0.2)
First, it would be nicer if you had your name/class assignments in a data.frame. (It also would have been nice if they were stored in a list or something). Here I gather all the ClassA/B/C/* varaibles into a list and stack them
indclass <- stack(mget(ls(pattern="Class*")))
indclass
# values ind
# 1 TOM ClassA
# 2 EVE ClassA
# 3 STEVE ClassA
# 4 JANE ClassB
# 5 HANNAH ClassB
# 6 ADAM ClassC
and i'll choose colors for each of the class names (the values from the "ind" column above)
cols <- c(ClassA="green", ClassB="lightblue", ClassC="purple")
Then, i'll use match()
to look up the class for each vertex and assign a color based on class name
V(g)$color <- cols[ indclass$ind[match( V(mst)$name, indclass$values ) ] ]
Then if you plot your minimal spanning tree, you get
mst <- as.undirected(minimum.spanning.tree(g))
plot(mst)