Mario GS
Mario GS

Reputation: 879

How to fit text inside vertex and avoid overlapping?

I'm trying to plot a graph with characters as labels. The issue it that the text is a bit long and the vertices overlap between them. Is there a way to minimize the area of the circular vertex by fitting the text inside? and also, how to avoid overlapping between them? This is the edge list and the data frame of attributes that I'm using(The second column of the df has the labels that I'm trying to fit. Here is my code:

## READ THE EDGE LIST AS A GRAPH OBJECT ## 
library('igraph')
cn <- as.matrix(cn, header= FALSE)
g <- graph.edgelist(cn,directed= TRUE)
plot(g, layout = layout.reingold.tilford(g, root=1))

## GET ATTRIBUTES ## 
V(g)$name=as.character(att$Name)  
V(g)$desc=as.character(att$Description)

## GET LABELS ## 
V(g)$label <- V(g)$name

Here I tried to use the method proposed in this post.

## FIT THE LABELS INSIDE THE VERTEX ## 
ly <- layout.reingold.tilford(g, root=1) ly <- layout.auto(g)
plot(0, type="n", ann=FALSE, axes=FALSE, xlim=extendrange(ly[,1]), ylim=extendrange(ly[,2])) plot(g, layout=ly, rescale=FALSE, add=TRUE,
     vertex.size=(strwidth(V(g)$label) + strwidth("oo")) * 100)

It is working for fitting the text inside the vertexes, but it is not minimizing the area of the circle. For instance, the string of characters "Random Process" would fit better in two lines instead of one and so on.. Also, it has problems with vertex overlapping. I would like to keep the structure of a tree diagram. Here is my outcome so far:enter image description here

Upvotes: 1

Views: 1818

Answers (1)

Daniel
Daniel

Reputation: 395

Try with this:

plot(G, vertex.size=8, vertex.color = rainbow(10, .8, .8, alpha= .8),
vertex.label.color = "black", vertex.label.cex = 0.4, vertex.label.degree = -pi/2,
edge.arrow.size = 0.3, edge.arrow.width = 0.4, edge.color = "black")

With vertex.label.degree and vertex.label.cex you fit the text on the node. Best!

Upvotes: 1

Related Questions