如何正确绘制igraph图以使其具有可比性?

时间:2019-10-30 13:30:40

标签: r plot igraph

因此,正如标题所示,我需要能够绘制一些igraph的图并进行比较。为此,我相信传递坐标然后绘制坐标就足够了。然后我发现,这些图是根据提供的坐标绘制的,而且还根据节点的数量(或子网的数量或我无法理解的其他任何参数)绘制的。为了了解这个问题,这里有一个例子(与欧几里德距离有关的部分已注释,因为它需要特定的包装,但我也发布了输出信息):

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"

问题在于,(欧几里得)距离不同,但是,如果我查看这些图,显然没有任何变化。另一方面,某些地方必须有所不同,因为节点的距离在增加。

我注意到增加几个节点可以改善可视化效果,但是我仍然相信我得到的绘图在某种程度上没有考虑实际距离。这是具有更多节点的另一个示例代码:

# 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)

在这两个新示例中,有所改变,因为与其他节点相比,节点现在距离很远。从图形的角度来看,现在两个网是否具有可比性?如果没有(我相信是这样)...为了使它们具有可比性,我应该怎么做?

我尝试按照提到的here设置xlim和ylim,但似乎无法使用:

# 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)

非常欢迎提出建议!

1 个答案:

答案 0 :(得分:1)

igraph绘图会进行一些缩放,从而导致这种非直观的输出。您可以通过打开轴来查看。在这里,我将为您进行“第二次尝试”

### Second try
V(smallNet)$x <- c(1400, 1894)
V(smallNet)$y <- c(3700, 4140)
plot(smallNet, vertex.label.color="midnightblue", 
    vertex.color="thistle1", layout=layout_nicely,
    axes=TRUE, main="Second Try")

Second try with axes

请注意,即使您将点放置在(x,y)=(1400,3700)和(1894,4140),它们也会绘制在(-1,-1)和(1,1)。 igraph会自动将x和y缩放为间隔[-1,1]。您可以使用rescale参数将其关闭。但是,这样做还需要其他更改。即使关闭重新缩放,igraph也会假定x和y的绘图间隔均为[-1,1]。您需要更改xlim和ylim。此外,节点大小以图形单位为单位,因此必须与数据范围成正比。我把所有这些都放进去了:

par(mfrow=c(1,3))

# first try
V(smallNet)$x <- c(10, 23)
V(smallNet)$y <- c(29, 36)
plot(smallNet, vertex.label.color="midnightblue", 
    vertex.size=8*diff(range(V(smallNet)$x)), 
    vertex.color="thistle1", layout=layout_nicely, 
    xlim=range( V(smallNet)$x), ylim=range( V(smallNet)$y),
    rescale=FALSE, axes=TRUE, main="First Try")

# second try
V(smallNet)$x <- c(1400, 1894)
V(smallNet)$y <- c(3700, 4140)
plot(smallNet, vertex.label.color="midnightblue", 
    vertex.size=8*diff(range(V(smallNet)$x)), 
    vertex.color="thistle1", layout=layout_nicely,
    xlim=range( V(smallNet)$x), ylim=range( V(smallNet)$y),
    rescale=FALSE, axes=TRUE, main="Second Try")

# third try
V(smallNet)$x <- c(10000, 26230)
V(smallNet)$y <- c(13800, 32150)
plot(smallNet, vertex.label.color="midnightblue", 
    vertex.size=8*diff(range(V(smallNet)$x)), 
    vertex.color="thistle1", layout=layout_nicely, 
    xlim=range( V(smallNet)$x), ylim=range( V(smallNet)$y),
    rescale=FALSE, axes=TRUE, main="Third Try")

All three graphs without rescaling

现在您可以看到,即使形状相似,它们的比例也不同。