Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How have labels inside the scaled nodes in visNetwork?

Tags:

graph

r

In visNetwork, by default text doesn't go inside nodes, instead it appears below it:

require(visNetwork, quietly = TRUE)
nodes <- data.frame(id = 1:3, label=2014:2016 ,value=1:3)
edges <- data.frame(from = c(1,2), to = c(1,3))
visNetwork(nodes, edges, width = "100%")

enter image description here

Seems like the only way to solve this issue is to use set shape property to circle:

require(visNetwork, quietly = TRUE)
nodes <- data.frame(id = 1:3, label=2014:2016 ,value=1:3,shape='circle')
edges <- data.frame(from = c(1,2), to = c(1,3))
visNetwork(nodes, edges, width = "100%")

enter image description here

The problem is that, as you can see in the figure above, now with labels inside circle scaling nodes using value property don't work.

So the question is how to have both options ("scale" and "text inside") at the same time?

PS: What a pity, there is no visNetwork tag!

like image 667
Woeitg Avatar asked Sep 24 '16 09:09

Woeitg


2 Answers

I found a tricky way to solve this bug. setting up font.size instead of value property works fine. You need to scale it for best visualization. For instance I scale it 10 times bigger:

require(visNetwork, quietly = TRUE)
nodes <- data.frame(id = 1:3, label=2014:2016 ,font.size =(1:3)*10,shape='circle')
edges <- data.frame(from = c(1,2), to = c(1,3))
visNetwork(nodes, edges, width = "100%")

enter image description here

like image 167
Woeitg Avatar answered Nov 03 '22 15:11

Woeitg


I've found another way, adding spaces on bot sides of the label.
That will keep all fonts the same size.

n <- 5L
nodes <- data.frame(id = 1:3, label=paste0(strrep(" ",n), 2014:2016,
strrep(" ",n)) ,value=1:3,shape='circle')
edges <- data.frame(from = c(1,2), to = c(1,3))
visNetwork(nodes, edges, width = "100%")

enter image description here

If the length of the labels is not the same you may want to try other criteria.

like image 28
skan Avatar answered Nov 03 '22 16:11

skan