gpt4 book ai didi

R : Display Image at Exact Node in Igraph

转载 作者:行者123 更新时间:2023-12-01 01:08:23 28 4
gpt4 key购买 nike

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

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

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

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com