2016-09-22 125 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.