自由地将顶点标签放置在 R 中圆图的顶点之外

Freely placing vertex labels outside of vertices for a circle graph in R

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

我查看了一些答案,并尝试了一个建议我通过以下方式为每个节点本身指定以弧度为单位的位置:

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 调整字体的大小(这最终是一个足够大的问题,我决定搜索其他方法)。

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

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

我只是尝试对分别具有七个和八个节点的圆图执行此操作,所以这就是我运行用七个节点测试它的方法:

##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))

对于任何格式问题,我提前表示歉意,我可能会编辑问题以调整这些问题。

查看您的两个版本。

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

但是你的第一个版本看起来还不错。由于您的第二个版本将标签放在节点内,因此我将标签移到那里并将字体放大两倍只是为了展示如何操作( vertex.label.cex 而不是 cex )。我不确定你想要什么尺寸,但你应该可以从这里调整。

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)