在R中的圆图上自由放置顶点标签 [英] Freely placing vertex labels outside of vertices for a circle graph in R

查看:221
本文介绍了在R中的圆图上自由放置顶点标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试在R中显示一个圆形图,以便将每个节点的标签放置在节点本身的旁边,但要在节点本身之外.

I'm currently trying to display a circle graph in R with the ability to place the labels of each node right next to, but outside of, the node itself.

我看了几个答案,然后尝试了一个答案,建议我通过以下方式为每个节点本身指定以弧度表示的位置:

I looked at a few answers, and tried one which suggested that I specify locations given in radians for each node itself via:

radian.rescale <- function(x, start=0, direction=1) {
  c.rotate <- function(x) (x + start) %% (2 * pi) * direction
  c.rotate(scales::rescale(x, c(0, 2 * pi), range(x)))
}
lab.locs <- radian.rescale(x=1:n, direction=-1, start=0)
plot(sev, layout=la, vertex.size=25, vertex.label.dist=5,
     vertex.label.degree=lab.locs, vertex.label.color="black")

大多数情况下都有效,但是标签并没有完全按照需要放置(不是一个大问题),但是我无法使用cex调整字体的大小(这最终是一个很大的问题,我决定搜索其他方法).

And this mostly worked, but the labels weren't precisely placed as desire (not a big issue), but I could not adjust the size of the font with cex (which ultimately was a big enough issue that I decided to search for other methods).

寻找更多答案后,我发现存在以下命令: text("label",locator(1)) 这应该允许使用鼠标指针交互地放置文本.但是,当我运行该命令时,出现以下错误:

After looking for a few more answers I was able to find that there exists the following command: text("label", locator(1)) which is supposed to allow interactively placing the text with a mouse pointer. However, when I run that, I get the following error:

In xy.coords(x, y, recycle = TRUE) : NAs introduced by coercion

我只是想对分别具有七个和八个节点的圆形图执行此操作,因此我正在用七个节点对其进行测试:

I'm just trying to do this for a circle graph with seven and eight nodes respectively, so here is what I'm running to test it with the seven nodes:

##testing graph labeling
library(igraph)
library(ggplot2)
library(scales)

##making a 7-node circle graph
sev=make_graph(c(1,2, 2,3, 3,4, 4,5, 5,6, 6,7, 7,1))
sev=as.undirected(sev)
#relabel specific nodes blue
j=1;#index of vertex to start coloring
 V(sev)$color="white"; #Need to default to white, otherwise will color all  blue
V(sev)$color[(j)%%7]="dodgerblue";
V(sev)$color[(j+1)%%7]="dodgerblue";
V(sev)$color[(j+2)%%7]="dodgerblue";
la<-layout.circle(sev)
plot(sev)
text("label",locator(1))

对于任何格式设置方面的困难,我深表歉意,我可能会编辑问题以进行调整.

I apologize in advance for any formatting difficulties, I'll probably edit the question to adjust those.

推荐答案

查看您的两个版本.

第二个版本,text(locator(1),"label")应该允许您手动放置标签.

Second version, text(locator(1),"label") should allow you to place a label by hand.

但是您的第一个版本看起来并不差.由于您的第二个版本将标签放置在节点内,因此我将标签移到了节点上,并使字体变成原来的两倍,以演示如何做(vertex.label.cex而不是cex).我不确定您想要什么尺寸,但是您应该可以从这里进行调整.

But your first version did not look so bad. Since your second version put the labels inside the nodes, I moved the labels there and made the font twice as big just to show how to do it ( vertex.label.cex instead of cex ). I am not sure what size you wanted, but you should be able to adjust from here.

radian.rescale <- function(x, start=0, direction=1) {
  c.rotate <- function(x) (x + start) %% (2 * pi) * direction
  c.rotate(scales::rescale(x, c(0, 2 * pi), range(x)))
}
lab.locs <- radian.rescale(x=1:n, direction=-1, start=0)
plot(sev, layout=la, vertex.size=25, vertex.label.dist=0,
     vertex.label.degree=lab.locs, vertex.label.color="black", 
     vertex.label.cex=2)

这篇关于在R中的圆图上自由放置顶点标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆