R:如何将噪声簇添加到DBSCAN图中

时间:2015-08-09 10:32:25

标签: r plot cluster-analysis dbscan

我正在尝试绘制DBSCAN结果。这就是我到目前为止所做的。我的距离矩阵为here

dbs55_CR_EUCL = dbscan(writeCRToMatrix,eps=0.006, MinPts = 4, method = "dist")

plot(writeCRToMatrix[dbs55_CR_EUCL$cluster>0,], 
     col=dbs55_CR_EUCL$cluster[dbs55_CR_EUCL$cluster>0],
     main="DBSCAN Clustering K = 4 \n (EPS=0.006, MinPts=4) without noise",
     pch = 20)

这是情节: enter image description here

当我尝试绘制包括噪声聚类在内的所有聚类时,我的情节中只能看到2个点。 enter image description here

我正在寻找的是

  1. 要将噪声群集中的点添加到绘图中,但使用不同的符号。类似于下图的内容
  2. enter image description here

    1. 为群集区域着色,如下图所示
    2. enter image description here

1 个答案:

答案 0 :(得分:2)

噪声簇的id为0. R图通常忽略0的颜色,因此如果要显示噪点(黑色),则需要执行以下操作:

plot(writeCRToMatrix, 
  col=dbs55_CR_EUCL$cluster+1L,
  main="DBSCAN Clustering K = 4 \n (EPS=0.006, MinPts=4) with noise",
  pch = 20)

如果您想要一个不同的噪音符号,那么您可以执行以下操作(改编自手册页):

library(dbscan)
n <- 100
x <- cbind(
     x = runif(10, 0, 10) + rnorm(n, sd = 0.2),
     y = runif(10, 0, 10) + rnorm(n, sd = 0.2)
)

res <- dbscan::dbscan(x, eps = .2, minPts = 4)
plot(x, col=res$cluster, pch = 20)
points(x[res$cluster == 0L], col = "grey", pch = "+")

以下是为每个群集创建阴影凸包的代码

library(ggplot2)
library(data.table)
library(dbscan)


dt <- data.table(x, level=as.factor(res$cluster), key = "level")
hulls <- dt[, .SD[chull(x, y)], by = level]

### get rid of hull for noise
hulls <- hulls[level != "0",]

cols <- c("0" = "grey", "1" = "red", "2" = "blue")

ggplot(dt, aes(x=x, y=y, color=level)) +
  geom_point() +
  geom_polygon(data = hulls, aes(fill = level, group = level),
    alpha = 0.2, color = NA) +
  scale_color_manual(values = cols) +
  scale_fill_manual(values = cols)

希望这有帮助。