I was color coding the vertices in the graph and wanted to generate a legend as simply , elegantly and as quickly as I can.
The fastest way to do this I've come to believe is generate the legend separately using ggplot2 before "pasting" the legend into the same plot as igraph using viewport
and layout()
In this method there is no need to call the rescale
or asp
arguements in the plot.igraph()
function.
Using the g_legend function on a data.frame, leg
, with 2 columns, x being the appropriate vertex attribute and y being the hex colour code used in my igraph plot, I've done the following.
My igraph object is t8g
legend <- g_legend(leg)
vpleg <- viewport(width = 0.1, height = 0.1, x=0.85,y=0.5)
layout(matrix(c(1,2),1,2,byrow=T),widths=c(3,1))
plot(t8g,edge.width=1,edge.arrow.size=0.1,vertex.label.cex=0.2,main="b2_top10")
pushViewport(vpleg)
grid.draw(legend)