1

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.

BLP92
  • 145
  • 2
  • 10
  • 1
    It doesn't look like you have the parameters to `text()` in the right form or order. What about `with(locator(1), text(x,y,"label"))`. It would help to provide a [reproducible example](http://stackoverflow.com/questions/19226816/how-can-i-view-the-source-code-for-a-function) for your first attempt. Right now "lab.locs" is not defined if we use the data from – MrFlick Dec 23 '16 at 21:19
  • 2
    Or more in the spirit of your code `text(locator(1),"label")` You just had the arguments in the wrong order. – G5W Dec 23 '16 at 22:41
  • Thank you, both of these solutions are viable ways of addressing my issues! – BLP92 Dec 28 '16 at 01:32

1 Answers1

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)

Node graph

G5W
  • 36,531
  • 10
  • 47
  • 80
  • Perfect, thank you! I ended up using the locator approach but, as you pointed out, the vertex.label.cex is also the correct way to adjust the label sizing. – BLP92 Dec 28 '16 at 01:33