View source: R/Visualizations_Network.R
plot_knn | R Documentation |
This function takes a dataframe that contains x and y coordinates and a label that defines the classes and makes a plot of the k-nearest neighbors graph using Euclidean distance and the cover tree algorithm.
plot_knn(df, k = 1, labels = NULL)
df |
A dataframe with x and y coordinates and a label column defining the classes. |
k |
An integer that defines the number of nearest neighbors to consider. Default is 1. |
labels |
An optional character vector specifying the label column in the dataframe. If not specified, it is assumed that the label column is named "label". |
A plot of the k-nearest neighbors graph using ggplot2.
# create sample data
set.seed(123)
x <- rnorm(100)
y <- rnorm(100)
label <- sample(c("A", "B", "C"), 100, replace = TRUE)
df <- data.frame(x = x, y = y, label = label)
# plot with k = 2
plot_knn(df, k = 2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.