Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 206942

how to properly plot igraph's graphs to make them comparable?

$
0
0

So, as in the title, I need to be able to plot some igraph's graphs and be able to compare them. To do so I believed that passing coordinates and then plotting them was enough. Then I found out that the graphs are rendered according to the coordinates that are provided but also according to the number of nodes (or the number of subnetworks or whatever other parameter which I am not able to understand). In order to get a grasp over the problem here is an example (the part related to euclidean distance is commented since it require a specific package, but I also posted the output):

library(igraph)
#library(TSdist)

smallNet <- graph(edges=c(1,2), n=2, directed=F) 

V(smallNet)$name <- c("mint", "pepper")

# first try
dev.new()

V(smallNet)$x <- c(10, 23)
V(smallNet)$y <- c(29, 36)

plot(smallNet, vertex.label.color="midnightblue", vertex.size=40, vertex.color="thistle1", layout=layout_nicely)
#print(paste("distance ", EuclideanDistance(V(smallNet)$x, V(smallNet)$y)))
#[1] "distance  23.0217288664427"


# second try
dev.new()

V(smallNet)$x <- c(1400, 1894)
V(smallNet)$y <- c(3700, 4140)

plot(smallNet, vertex.label.color="midnightblue", vertex.size=40, vertex.color="thistle1", layout=layout_nicely)
#print(paste("distance ", EuclideanDistance(V(smallNet)$x, V(smallNet)$y)))
#[1] "distance  3214.73420363177"

# third try
dev.new()

V(smallNet)$x <- c(10000, 26230)
V(smallNet)$y <- c(13800, 32150)

plot(smallNet, vertex.label.color="midnightblue", vertex.size=40, vertex.color="thistle1", layout=layout_nicely)
#print(paste("distance ", EuclideanDistance(V(smallNet)$x, V(smallNet)$y)))
#[1] "distance  7034.65706342534"

The point is that the (euclidean) distances are different but, if I look at the plots, nothing apparently changes. On the other hand, something must be different since the node's distance is increasing.

I noticed that adding few nodes improves the visualisation but still I believe the plots I am getting are not, somehow, respecting the actual distances. Here's another sample code with more nodes:

# first try
evenBigger <- graph(edges=c(1,2, 2,3, 3,1, 4,5), n=5, directed=F) 

V(evenBigger)$name <- c("pear", "mango", "blueberry", "coconut", "fig")

dev.new()

V(evenBigger)$x <- c(0, 25, 50, 70, 60)
V(evenBigger)$y <- c(0, 80, 20, 120, 40)

plot(evenBigger, vertex.label.color="midnightblue", vertex.size=40, vertex.color="thistle1", layout=layout_nicely)

# second try
evenBigger <- graph(edges=c(1,2, 2,3, 3,1, 4,5, 6,6), n=6, directed=F) 

V(evenBigger)$name <- c("pear", "mango", "blueberry", "coconut", "fig", "jujube")

dev.new()

V(evenBigger)$x <- c(0, 25, 50, 70, 120, 2000)
V(evenBigger)$y <- c(0, 80, 20, 120, 140, 2000)

plot(evenBigger, vertex.label.color="midnightblue", vertex.size=40, vertex.color="thistle1", layout=layout_nicely)

it looks like in these two new examples, something changes, since the jujube node is now far away, if compared to the other nodes. Are now the two nets comparable, from a graphical perspective? If not (which I believe to be the case)...what should I do in order to make them comparable?

I tried to set xlim and ylim as mentioned here but it looks like it is not working:

# first try
evenBigger <- graph(edges=c(1,2, 2,3, 3,1, 4,5), n=5, directed=F) 

V(evenBigger)$name <- c("pear", "mango", "blueberry", "coconut", "fig")

dev.new()

V(evenBigger)$x <- c(0, 25, 50, 70, 60)
V(evenBigger)$y <- c(0, 80, 20, 120, 40)

plot(evenBigger, vertex.label.color="midnightblue", vertex.size=40, vertex.color="thistle1", xlim=c(0, 2500), ylim=c(0, 2500), layout=layout_nicely)

# second try
evenBigger <- graph(edges=c(1,2, 2,3, 3,1, 4,5, 6,6), n=6, directed=F) 

V(evenBigger)$name <- c("pear", "mango", "blueberry", "coconut", "fig", "jujube")

dev.new()

V(evenBigger)$x <- c(0, 25, 50, 70, 120, 2000)
V(evenBigger)$y <- c(0, 80, 20, 120, 140, 2000)

plot(evenBigger, vertex.label.color="midnightblue", vertex.size=40, vertex.color="thistle1", xlim=c(0, 2500), ylim=c(0, 2500), layout=layout_nicely)

Suggestions are very welcome!


Viewing all articles
Browse latest Browse all 206942

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>