KNN可视化-如何使用R在连接到N个最近点的数据点周围绘制圆

时间:2018-12-05 16:38:00

标签: r

我有一个散布图,是使用下面的代码生成的

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

enter image description here

我想标识一个数据点并使用另一种颜色为其着色,这是我在下面的代码中所做的

plot(feature1, feature2, pch=4, col=ifelse((feature1 > 2.6 & feature1 < 2.7 ), "red", "black"))

enter image description here

现在,我想在此点周围画一个圆(用红色标记),并将最近的N个相邻点连接到该点(其中N应该是变量)

如何使用R做到这一点?

这是我打算在输出中得到的内容

enter image description here

2 个答案:

答案 0 :(得分:1)

这是一种使用基本绘图功能的方法,但是使用了spDistsN1()库中的sp,它可以快速运行很多点。

编辑:我删除了对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')

}

这是您将数据用于n = 10的结果。

致电:
draw_neighbors(dat = cbind(feature1, feature2), focal_pt_index = which(feature1 > 2.6 & feature1 < 2.7), n = 10)

enter image description here

答案 1 :(得分:1)

首先让您将数据放入矩阵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"))

然后我们找到那些k最近的邻居,并画一个圆(使用this answer中的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")

enter image description here