plot_knn: Plot k-Nearest Neighbors Graph

View source: R/Visualizations_Network.R

plot_knnR Documentation

Plot k-Nearest Neighbors Graph

Description

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.

Usage

plot_knn(df, k = 1, labels = NULL)

Arguments

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".

Value

A plot of the k-nearest neighbors graph using ggplot2.

Examples

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


eisascience/scCustFx documentation built on June 2, 2025, 3:59 a.m.