[英]KNN visualization - How to draw a circle around a data point connecting to N nearest points using R
I have a scatter plot which I generate using below code 我有一个散点图,我使用下面的代码生成
set.seed(10)
mydata <- data.frame(x1 = rnorm(1000), x2 = rnorm(1000))
ind <- replicate(3, sample(nrow(mydata), 500))
head(ind)
feature1 = mydata[ind[,1], "x1"]
feature2 = mydata[ind[,2], "x2"]
# start with a plot
plot(feature1, feature2, pch=4 , col="black")
I want to identify one data point and color it using a different color, which I do using below code 我想识别一个数据点并使用不同的颜色对其进行着色,我在下面的代码中使用它
plot(feature1, feature2, pch=4, col=ifelse((feature1 > 2.6 & feature1 < 2.7 ), "red", "black"))
Now, I would like to draw a circle around this point(which is marked in RED) and connect nearest neighboring N points to this point(where N should be a variable) 现在,我想围绕这一点绘制一个圆圈(用红色标记)并将最近的相邻N点连接到这一点(其中N应该是一个变量)
How can I do it using R? 我怎么能用R做呢?
Here is what I intend to get in my output 这是我打算在输出中得到的内容
Here's a way to do it with base plotting functions but using spDistsN1()
from the sp
library which should run quickly for very large numbers of points. 这是一种使用基本绘图函数的方法,但是使用sp
库中的spDistsN1()
,它应该可以快速运行非常多的点。
edit : I removed dependence on plotrix
library for circle drawing, which was giving an incorrect result. 编辑 :我删除了plotrix
库对圆绘图的依赖,这给出了错误的结果。
draw_neighbors <- function(dat, focal_pt_index, n) {
require(sp)
# Calculate distances to focal point.
dists <- spDistsN1(pts = dat, pt = dat[focal_pt_index,])
# Sort points by distance.
dat <- cbind(dat, dist = dists)
dat <- dat[order(dat[,'dist']), ]
# Plot points
plot(dat[,1], dat[,2], pch=4 , col=ifelse(dat[,'dist'] == 0, "red", "black"), asp = 1)
# Draw a line to each neighbor
neighbors <- dat[2:(n+1), ]
for (i in 1:nrow(neighbors)) {
lines(x = c(dat[1,1], neighbors[i,1]), y = c(dat[1,2], neighbors[i,2]), col = 'red')
}
# Draw a circle at the radius equal to the largest distance within the n nearest neighbors.
radius <- dat[n+1, 'dist']
angles <- seq(0,2*pi,length=1000)
coords <- cbind(dat[1,1] + sin(angles) * radius, dat[1,2] + cos(angles)* radius)
points(coords, type = 'l', lty = 2, col = 'red')
}
Here is what you get using your data for n = 10
. 以下是您使用n = 10
数据得到的结果。
Call: 呼叫:
draw_neighbors(dat = cbind(feature1, feature2), focal_pt_index = which(feature1 > 2.6 & feature1 < 2.7), n = 10)
Let's first put your data into a matrix p
, determine your point of interest p0
, and define the number of common neighbours of interest k
. 让我们首先将您的数据放入矩阵p
,确定您的兴趣点p0
,并定义感兴趣的共同邻居k
。
p <- cbind(feature1, feature2)
idx <- p[, 1] > 2.6 & p[, 1] < 2.7
p0 <- p[idx, ]
k <- 10
plot(feature1, feature2, pch = 4, col = ifelse(idx, "red", "black"))
Then we find those k
nearest neighbours and draw a circle (using circleFun
from this answer ) and segments: 然后我们找到那些k
最近邻居并绘制一个圆圈(使用此答案中的 circleFun
)和段:
kNN <- p[order(colMeans((t(p) - p0)^2))[1 + 1:k], ]
crc <- circleFun(p0, diameter = 2 * sqrt(sum((kNN[k, ] - p0)^2)))
lines(x = crc$x, y = crc$y, col = 'red', lty = 2)
segments(x0 = p0[1], y0 = p0[2], x1 = kNN[, 1], y1 = kNN[, 2], col = "red")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.