简体   繁体   English

R中igraph顶点值的颜色条图例

[英]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.我是 R 的新手,我开始使用 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).下面的示例创建了一个由 10 个顶点组成的简单网络,并根据颜色值为它们着色(在这种情况下,为简单起见,我将其设置为与顶点的 id 相同)。

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.您应该查看kokkenbaker的这个答案,虽然它有点麻烦,但它可能正是您所需要的。 How to add colorbar with perspective plot in R 如何在 R 中添加带有透视图的颜色条

Thanks to ealbsho93 I was able to produce the following solution.感谢 ealbsho93 我能够产生以下解决方案。 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.否则,这个似乎可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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