简体   繁体   English

在igraph中添加图例以注释差异顶点大小

[英]Add legend in igraph to annotate difference vertices size

I have a graph in igraph with a vertex size that is coded for a value. 我在igraph有一个图形,其顶点大小是为值编码的。
I wish to add a legend with symbols (points in my case) with different sizes. 我希望添加一个带有不同大小的符号(在我的情况下为点)的图例。 I tried the following code 我尝试了以下代码

require(igraph)
er_graph <- erdos.renyi.game(100, 5/100)
value<-runif(100)
n<-6
size_vec<-seq_len(n)
sizeCut<-cut(value,n)
vertex.size<-size_vec[sizeCut]
plot(er_graph, vertex.label=NA, vertex.size=vertex.size)
legend('topleft',legend=levels(sizeCut),pt.cex=size_vec,col='black')

but end with legend without symbols see example 但以没有符号的图例结束, 请参阅示例

Any sugestions how I go about this? 我是怎么走的?

You should set pch to some value to indicate which character you want to use for the bullets (see ?points to check the possible values). 您应该将pch设置为某个值以指示要用于项目符号的字符(请参阅?points检查可能的值)。
Also, you should scale the pt.cex values in order to make the bullets not too big for the legend, and use pt.bg to set the background color of the bullets, eg 此外,您应该缩放pt.cex值以使子弹对于图例不太大,并使用pt.bg设置子弹的背景颜色,例如

# scaled between 1 and 2
scaled <- 1 + ((2-1) * (size_vec - min(size_vec) ) / (  max(size_vec) - min(size_vec) ) )
legend('topleft',legend=levels(sizeCut),pt.cex=scaled,col='black',pch=21, pt.bg='orange')

在此输入图像描述

EDIT : 编辑:

Unfortunately, calculating the right sizes of the bullets is not easy; 不幸的是,计算子弹的正确尺寸并不容易; a possible workaround is plotting white bullets then manually add the vertices to the legend in the same way they are plotted inside the plot.igraph function : 一种可能的解决方法是绘制白色项目符号,然后手动将顶点添加到图例中,方法与在plot.igraph函数中绘制它们的方式相同:

# N.B. /200 is taken from plot.igraph source code
a <- legend('topleft',legend=levels(sizeCut),pt.cex=size_vec/200,col='white',
            pch=21, pt.bg='white')
x <- (a$text$x + a$rect$left) / 2
y <- a$text$y
symbols(x,y,circles=size_vec/200,inches=FALSE,add=TRUE,bg='orange')

在此输入图像描述

Disclaimer : this code heavily relies on the source code of plot.igraph function that might be changed in a future version of igraph. 免责声明 :此代码严重依赖于plot.igraph函数的源代码,可能会在未来版本的igraph中更改。 Probably you should search for another plot function for igraph which natively allows to add a legend. 可能你应该搜索igraph的另一个绘图功能,它本身允许添加一个图例。

Anyone looking at this who wants a continuous scale for node sizes instead of a discrete scale then this is the code you need to do it: 任何想要连续缩放节点大小而不是离散比例的人都需要这样做,这就是你需要做的代码:

require(igraph)
er_graph <- erdos.renyi.game(100, 5/100)
value<-runif(100)
sizeCut<- c(0.2,0.4,0.6,0.8,1.0)
sizeCutScale <- sizeCut*10
vertex.size<-value*10
plot(er_graph, vertex.label=NA, vertex.size=vertex.size)
legend('topleft',legend=unique(sizeCut),pt.cex= sizeCutScale,col='black')
a <- legend('topleft',legend=unique(sizeCut),pt.cex=sizeCutScale/200,col='white',
            pch=21, pt.bg='white')
x <- (a$text$x + a$rect$left) / 2
y <- a$text$y
symbols(x,y,circles=sizeCutScale/200,inches=FALSE,add=TRUE,bg='orange')

在此输入图像描述

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

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