NN is an abstract S3 superclass for the classes of the objects returned
by kNN(), frNN() and sNN(). Methods for sorting, plotting and getting an
adjacency list are defined.
Arguments
- x
a
NNobject- ...
further parameters are passed on to
graphics::plot.default().- decreasing
sort in decreasing order?
- data
that was used to create
x- main
title
- pch
plotting character.
- col
color used for the data points (nodes). Use
NULLto make the points invisible.- linecol
color used for edges.
- xlab, ylab
axis labels. The default is to use the column names of
dataor, if the data is projected, the principal components with the percentage of variance they explain.
Details
plot() draws the neighborhood graph in the first two dimensions of
data. If data has more than two columns, then the data is first
projected onto its first two principal components using stats::prcomp()
(centered, but not scaled, so Euclidean distances are preserved as well as
possible) and the graph is drawn in the projection.
Examples
data(iris)
x <- iris[, -5]
# finding kNN directly in data (using a k-d tree)
nn <- kNN(x, k=5)
nn
#> k-nearest neighbors for 150 objects (k=5).
#> Distance metric: euclidean
#>
#> Available fields: dist, id, k, sort, metric
# plot the kNN where NN are shown as line connecting points.
# Since the data has more than 2 dimensions, a PCA projection is used.
plot(nn, x)
# show the first few elements of the adjacency list
head(adjacencylist(nn))
#> [[1]]
#> 1 2 3 4 5
#> 18 5 40 28 29
#>
#> [[2]]
#> 1 2 3 4 5
#> 35 46 13 10 26
#>
#> [[3]]
#> 1 2 3 4 5
#> 48 4 7 13 46
#>
#> [[4]]
#> 1 2 3 4 5
#> 48 30 31 3 46
#>
#> [[5]]
#> 1 2 3 4 5
#> 38 1 18 41 8
#>
#> [[6]]
#> 1 2 3 4 5
#> 19 11 49 45 20
#>
if (FALSE) { # \dontrun{
# create a graph and find connected components (if igraph is installed)
library("igraph")
g <- graph_from_adj_list(adjacencylist(nn))
comp <- components(g)
plot(x, col = comp$membership)
# detect clusters (communities) with the label propagation algorithm
cl <- membership(cluster_label_prop(g))
plot(x, col = cl)
} # }