2016-09-22 52 views
-1

我有一个城市数据样本,我将它们聚类为一些参数。但我无法直观地表示它们,首先使用了clusplot,但我不明白为什么尺度会发生变化,因为即使只绘制两个分量,数据范围从-1到1,范围也是从-4到4, 2至2,如示例1中所示。查看聚簇对象的名称

[clusplot[1]

所以我用hullplot DBSCAN包,但情节并不在您的输出城市的名称显示,作为clusplot,看到2。有人能给我一个如何将这些名称添加到图表的建议吗?

hullplot

+0

请添加创建剧情的代码 – rawr

回答

0

我会尝试使用GGPLOT2和ggrepel包这一点。我借用代码从this question制作凸包。

set.seed(175) 
library(ggplot2) 
library(ggrepel) # Or first install.packages("ggrepel") 

# Make the cluster 
mtcars$cluster <- as.factor(kmeans(mtcars, 3)$cluster) 

# Get the convex hull for the axes you want to plot 
hull_df <- plyr::ddply(mtcars, "cluster", function(dta) { 
    hull <- chull(dta$mpg, dta$disp) 
    dta[c(hull, hull[1]), ] 
}) 

ggplot(mtcars, aes(mpg, disp, color = cluster, fill = cluster)) + 
    geom_point() + 
    geom_polygon(data = hull_df, alpha = 0.5) + 
    geom_text_repel(aes(label = row.names(mtcars))) 

结果: enter image description here

+0

谢谢,您真的帮了我很多! 如果你知道任何参考资料来解释clusplot是如何工作的,我会非常感激,因为对于我所寻找的,他是唯一一个可以绘制两个以上参数的集群,或者它只使用2个最重要的参数,运行像选择变量的东西? PCA?! – user2905427

0

下面是一些例子如何与DBSCAN做到这一点:

library(dbscan) 
set.seed(2) 
n <- 400 

x <- cbind(
    x = runif(4, 0, 1) + rnorm(n, sd=0.1), 
    y = runif(4, 0, 1) + rnorm(n, sd=0.1), 
    z = runif(4, 0, 1) + rnorm(n, sd=0.1) 
) 
cl <- rep(1:4, time = 100) 

### show some points (first 10) inside the hulls with text 
hullplot(x, cl, main = "True clusters", pch = NA) 
points(x[1:10,]) 
text(x[1:10,], labels = paste("Obs.", 1:10), pos = 3) 

### look at dimensions x and z 
hullplot(x[, c("x", "z")], cl, main = "True clusters") 

### use a PCA projection 
hullplot(prcomp(x)$x, cl, main = "True clusters") 

你可以看一下包wordcloud更好字布局。请参阅here.