R : Display Image at Exact Node in Igraph

标签 r igraph

我可以在图中的节点处放置图像,但图像未显示在确切位置。在这里,我附上了演示代码,该代码在精确节点显示图像时有问题。即使背后的逻辑是正确的,图像也会交换它们的位置。请改进代码,以便我可以进一步工作

注意:节点数量较少时,代码工作正常 .

################ R Code To implement dynamic image fetching  #######################
library(jpeg)
library(igraph) 

demo_adj <- data.frame(from=c(1,2,'B','X','Y',1,1,'A'),to=c('A','B','A','Y',1,'B','X',2))

rasters <- as.list(c(imgType1='',imgType2='',imgType3='',imgType4='',imgType5=''))

rasters$imgType1 <- readJPEG("path/Images/imgType1.jpg",native=TRUE)
rasters$imgType2 <- readJPEG("path/Images/imgType2.jpg",native=TRUE)
rasters$imgType3 <- readJPEG("path/Images/imgType3.jpg",native=TRUE)
rasters$imgType4 <- readJPEG("path/Images/imgType4.jpg",native=TRUE)
rasters$imgType5 <- readJPEG("path/Images/imgType5.jpg",native=TRUE)

lkp_mat <- data.frame(from=c(1,2,'A','B','X','Y'),type=c('imgType1','imgType2','imgType3','imgType4','imgType1','imgType3'))



## create the graph
gg <- graph.data.frame(demo_adj)
## set raster attribute
for(i in V(gg)$name){
                imgtype <- lkp_mat$type[lkp_mat["from"]==i]
                V(gg)[name==i]$raster <- rasters[imgtype]
}
plot(gg, layout=layout.star, vertex.shape="raster",
     vertex.label=V(gg)$name, margin=.2,
     vertex.size=50, vertex.size2=50,
     vertex.label.dist=2, vertex.label.degree=0)
###################### End of Above Code Segment ###########################################

或者

如果不想从磁盘获取 jpeg 图像,请使用以下代码解决上述问题:在这里您可以看到节点名称 5 和其他几个节点没有显示正确的图像。请区分图像,因为它不是很容易区分
###### Alternate Code To implement the above without jpeg images   #######

library(jpeg)
library(igraph) 

demo_adj <- data.frame(from=c(1,2,3,4,5,'F','G','H','I','J','J'),to=c('A','B','C','D','E',1,2,3,4,3,5))

rasters <- as.list(c(imgType1='',imgType2='',imgType3='',imgType4='',imgType5='',imgType6='',imgType7='',imgType8='',imgType9='',imgType10='',imgType11='',imgType12='',imgType13='',imgType14='',imgType15=''))
image1 <- as.raster(matrix(0:1, ncol=1, nrow=2))
image2 <- as.raster(matrix(0:1, ncol=2, nrow=4))
image3 <- as.raster(matrix(0:1, ncol=1, nrow=4))
image4 <- as.raster(matrix(0:1, ncol=4, nrow=4))
image5 <- as.raster(matrix(0:1, ncol=4, nrow=10))
image6 <- as.raster(matrix(0:1, ncol=2, nrow=2))
image7 <- as.raster(matrix(0:1, ncol=2, nrow=3))
image8 <- as.raster(matrix(0:1, ncol=2, nrow=4))
image9 <- as.raster(matrix(0:1, ncol=2, nrow=5))
image10 <- as.raster(matrix(0:1, ncol=2, nrow=6))
image11 <- as.raster(matrix(0:1, ncol=3, nrow=2))
image12 <- as.raster(matrix(0:1, ncol=4, nrow=8))
image13 <- as.raster(matrix(0:1, ncol=3, nrow=4))
image14 <- as.raster(matrix(0:1, ncol=4, nrow=2))
image15 <- as.raster(matrix(0:1, ncol=4, nrow=10))

rasters$imgType1 <- image1
rasters$imgType2 <- image2
rasters$imgType3 <- image3
rasters$imgType4 <- image4
rasters$imgType5 <- image5
rasters$imgType6 <- image6
rasters$imgType7 <- image7
rasters$imgType8 <- image8
rasters$imgType9 <- image9
rasters$imgType10 <- image10
rasters$imgType11 <- image11
rasters$imgType12 <- image12
rasters$imgType13 <- image13
rasters$imgType14 <- image14
rasters$imgType15 <- image15

lkp_mat <- data.frame(from=c(1,2,3,4,5,'A','B','C','D','E','F','G','H','I','J'),type=c('imgType1','imgType2','imgType3','imgType4','imgType5','imgType6','imgType7','imgType8','imgType9','imgType10','imgType11','imgType12','imgType13','imgType14','imgType15'))
#lkp_mat <- data.frame(from=c(1,2,3,4,5,'A','B','C','D','E','F','G','H','I','J'),type=c('imgType1','imgType2','imgType3','imgType4','imgType5','imgType2','imgType1','imgType2','imgType1','imgType2','imgType1','imgType2','imgType1','imgType2','imgType15'))

## create the graph
gg <- graph.data.frame(demo_adj)
## set raster attribute
for(i in V(gg)$name){
                imgtype <- lkp_mat$type[lkp_mat["from"]==i]
                V(gg)[name==i]$raster <- rasters[imgtype]
}
plot(gg, layout=layout.star, vertex.shape="raster",
     vertex.label=V(gg)$name, margin=.2,
     vertex.size=10, vertex.size2=20,
     vertex.label.dist=2, vertex.label.degree=0)

下面是问题图
enter image description here

最佳答案

这与 igraph 没有太大关系,但它是一个致命的 R 捕获,在 R inferno 中有特色。 , 第 8.2.6 节,不要用因子下标。

从您的代码中查看此部分,并添加了一些调试:

for(i in V(gg)$name){
  imgtype <- lkp_mat$type[lkp_mat["from"]==i]
  V(gg)[name==i]$raster <- rasters[imgtype]
  if (i=="J") { 
    print(rasters[imgtype]) ; 
    print(rasters[as.character(imgtype)]) 
  }
}

顺便提一句。如果您使用 vertices,整个事情可能会简单得多。 graph.data.frame() 的论据,例如像这样:
gg2 <- graph.data.frame(demo_adj, vertices=lkp_mat)
V(gg2)$raster <- rasters[V(gg2)$type]

plot(gg2, layout=layout.star, vertex.shape="raster",
     vertex.label=V(gg2)$name, margin=.2,
     vertex.size=10, vertex.size2=20,
     vertex.label.dist=2, vertex.label.degree=0)

关于R : Display Image at Exact Node in Igraph,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16932442/

相关文章:

Pandas:大对节点之间的最短路径长度

r - 从 R 中的 alphahull 中提取多个多边形

r - 格式化在 ggplot 的箱线图中显示为文本的数字。解释箱线图之外均值的定位

r - 从另一个列表(嵌套列表)的列表元素开始创建一个新列表

r - 绘制二分加折线图比较

python - r 网状 : rename duplicates from converted Python pandas dataframe

r - 计算网络中的周期

sql - 将 data.table 更新到 SQL 数据库?

linux - R 程序 - igraph 包错误

r - 如何使用 R + Igraph 获得基本周期