R: xy.coords(x, y, xlabel, ylabel, log) 中的错误:'x' 是一个列表,但没有组件 'x' 和 'y'

R: Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' is a list, but does not have components 'x' and 'y'

我正在使用 R 编程语言。我正在尝试按照此处的说明制作数据的“k 最近邻图”:https://igraph.org/r/doc/knn.html

使用“igraph”库,我创建了一些假数据并制作了图表:

library(igraph)
file <-data.frame(
    
    "source" = c(
        "John",
        "John",
        "Tim",
        "Tim",
        "Alex",
        "Andrew",
        "Andrew",
        "Andrew",
        "Oliver",
        "Oliver",
        "Oliver",
        "Matt",
        "Steven",
        "Steven",
        "Steven",
        "Matt",
        "Charles",
        "Charles",
        "Charles",
        "Sean",
        "Ted",
        "Ryan",
        "Ryan",
        "Ryan",
        "Ted",
        "Phil",
        "Phil",
        "Phil",
        "Sam",
        "Toby",
        "Toby",
        "Donald",
        "Donald",
        "Donald",
        "Mitch",
        "Mitch",
        "Mitch"),
    
    "target" = c("Sam",
                 "Tim",
                 "Alex",
                 "Matt",
                 "Andrew",
                 "Sean",
                 "Peter",
                 "Ben",
                 "Kevin",
                 "Thomas",
                 "Dave",
                 "Steven",
                 "Kenny",
                 "Derek",
                 "CJ",
                 "Charles",
                 "Ivan",
                 "Kyle",
                 "Andrew",
                 "Ted",
                 "Ryan",
                 "Daniel",
                 "Chris",
                 "Scott",
                 "Phil",
                 "Henry",
                 "George",
                 "Paul",
                 "Toby",
                 "Donald",
                 "Mitch",
                 "Jack",
                 "Luke",
                 "Myles",
                 "Elliot",
                 "Harvey",
                 "Owen")
    
)

graph <- graph.data.frame(file, directed=F)
graph <- simplify(graph)
plot(graph)

从这里开始,我能够成功地将此图转换为“k 最近邻图”(注意:我不确定如何指定“邻居数”):

 knn(graph)

$knn
    John      Tim     Alex   Andrew   Oliver     Matt   Steven  Charles     Sean      Ted     Ryan     Phil      Sam 
2.500000 2.333333 4.000000 2.000000 1.000000 3.666667 1.500000 2.500000 4.000000 3.333333 1.500000 1.500000 2.500000 
    Toby   Donald    Mitch    Peter      Ben    Kevin   Thomas     Dave    Kenny    Derek       CJ     Ivan     Kyle 
3.333333 1.500000 1.500000 5.000000 5.000000 3.000000 3.000000 3.000000 4.000000 4.000000 4.000000 4.000000 4.000000 
  Daniel    Chris    Scott    Henry   George     Paul     Jack     Luke    Myles   Elliot   Harvey     Owen 
4.000000 4.000000 4.000000 4.000000 4.000000 4.000000 4.000000 4.000000 4.000000 4.000000 4.000000 4.000000 

$knnk
[1] 3.954545 3.250000 2.733333 1.666667 2.000000

但出于某种原因,我无法绘制此图:

plot(knn(graph))

Error in xy.coords(x, y, xlabel, ylabel, log) : 
  'x' is a list, but does not have components 'x' and 'y'

有谁知道为什么会出现这个错误?有人可以告诉我我做错了什么吗?

谢谢

你应该知道 knn(graph) 是一个列表,即

> class(knn(graph))
[1] "list"

这就是为什么你不能在 knn(graph)

上使用 plot

我猜你想画的是knn(graph)$knn,例如

plot(rev(stack(knn(graph)$knn)))

plot(knn(graph)$knn)