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

ぐ巨炮叔叔 提交于 2019-12-11 05:53:02

问题


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

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

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.


回答1:


Looking at your two versions.

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

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)



来源:https://stackoverflow.com/questions/41307909/freely-placing-vertex-labels-outside-of-vertices-for-a-circle-graph-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!