简体   繁体   中英

Color bar legend for values on vertices of igraph in R

I am new in R and I am starting to work on graph visualization over there using igraph. The example below create a simple network of 10 vertices and color them according to color values (which in this case for simplicity I set up to be the same as ids of vertices).

library(igraph)
vertices <- 1:10
first <- 1:10
second <- c(2:10,1)
edges = cbind(first,second)
color = 1:10
net = graph_from_data_frame(edges,vertices=vertices ,directed=F )
V(net)$color = color
plot(net)

However from this plot it is not clear which colors correspond to which numbers:

在此处输入图片说明

To deal with this I have tried to create various legends I was able to find in the documentation and online. Take for instance the code below:

legend("bottom", legend=levels(as.factor(color)), bty = "n", cex =
1.5, pt.cex = 3, pch=20, col = color , horiz = FALSE , inset = c(0.1,
-0.3)

But in this case, the result is messy, obscure the picture, and do not provide a continuous color bar that would map the range of values on the nodes to color spectrum. Other options I was able to find are not better.

在此处输入图片说明

Do you know how to make a legend in a form of a continuous color bar placed below or to the right from the picture (so that it do not cover any part of it)? Ideally the color bar should show the whole continuous spectrum of colors and a few values corresponding to the colors (at least the extreme ones)? Do you happen to know how to achieve this?

Thank you for your help!

You should check out this answer by kokkenbaker,although it is a bit cumbersome, it might be just what you need. How to add colorbar with perspective plot in R

Thanks to ealbsho93 I was able to produce the following solution. It create a pallete, then map the values on the vertices on the graph to the pallete and displays it. It is not straightforward, but the result looks much better (see below)

rm(list=ls())
library(igraph)
library(fields)

vertices <- 1:10
first <- 1:10
second <- c(2:10,1)
edges = cbind(first,second)
net = graph_from_data_frame(edges,vertices=vertices ,directed=F )

#Here we create a sample function on the vertices of the graph
color_num = 10:1
#create a color palette of the same size as the number of vertices.
jet.colors <- colorRampPalette( rainbow( length( unique(color_num) ) ) )
color_spectrum <- jet.colors( length( unique(color_num ) ) )
#and over here we map the pallete to the order of values on vertices
ordered <- order(color_num)
color <- vector(length = length(ordered),mode="double")
for ( i in 1:length(ordered) )
{
    color[ ordered[i] ] <- color_spectrum [ i ]
}
V(net)$color = color

#Display the graph and the legend.
plot(net)
image.plot(legend.only=T, zlim=range(color_num), col=color_spectrum )

在此处输入图片说明

If there is a better solution, please let me know. Othervise, this one seems to be OK to use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM