Reputation: 17
I am trying to place the labels for my vertices outside a circular graph. I have read lots of posts on this including Placing vertex.label outside a circular layout in igraph and Freely placing vertex labels outside of vertices for a circle graph in R.
The issue is that my vertices are scaled to different sizes, so the placement of the labels directly outside of the vertex, no matter how big/small the vertex is, does not work as I want it to.
I am using the code in the top answer of this question (Placing vertex.label outside a circular layout in igraph) to generate the locations.
When I remove the varying sizes of the nodes, the labels move in the correct direction, but some of them are not completely outside the vertex, as some of the names are too long.
# set up dummy data
players <- c("Jack Taylor", "John", "Mike", "Jackson Hubbard", "Matt", "Casey", "Adam")
points <- c(20, 10, 5, 8, 15, 12, 30)
stats <- data.frame(name = players, points = points)
from <- c("Jack Taylor", "Jackson Hubbard", "Adam", "Matt", "Jack Taylor", "John")
to <- c("John", "Jack Taylor", "Jackson Hubbard", "Adam", "Adam", "Casey")
count <- c(2, 3, 5, 2, 1, 1)
assists <- data.frame(from = from, to = to, count = count)
home_weighted_net <- graph_from_data_frame(d=assists, vertices=players, directed=T)
V(home_weighted_net)$size <- points * 3
V(home_weighted_net)$label.cex <- 0.5
E(home_weighted_net)$width <- assists$count
# generate positions and graph
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:7, direction=-1, start=0)
lay <- layout.circle(home_weighted_net)
plot(home_weighted_net, layout= lay, vertex.label.degree=lab.locs, vertex.label.dist= 2)
Is there a way to account for the vertex size when setting the placement?
Also, is there a way for the label to be completely outside the vertex no matter how long the string is?
Upvotes: 0
Views: 459
Reputation: 37661
You specified vertex.label.dist
as a constant, but it can be a vector so that the offset is different for each vertex. I got a pretty good result with a simple function of the vertex size. You might be able to experiment and get a nicer result.
plot(home_weighted_net, layout= lay, vertex.label.degree=lab.locs,
vertex.label.dist= points/4+2.5)
Upvotes: 0