I am using the R programming language. I am trying to follow the instructions here to make a "k nearest neighbor graph" of my data: https://igraph.org/r/doc/knn.html
Using the "igraph" library, I created some fake data and made a graph:
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)
From here, I was able to successfully convert this graph into a "k nearest neighbor graph" (note: I am not sure how to specify the "number of neighbors"):
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
But for some reason, I am not able to plot this graph:
plot(knn(graph))
Error in xy.coords(x, y, xlabel, ylabel, log) :
'x' is a list, but does not have components 'x' and 'y'
Does anyone know why this error is appearing? Can someone please show me what I am doing wrong?
Thanks
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.