2017-09-21 89 views
0

我有看起來像名爲(inputdata_transaction_cluster)的數據幀:如何使用繪圖R突出顯示圖中特定聚類的中心(K-均值聚類)?

data frame

我成功地繪製了使用聚類分析plotly讀該數據幀中的曲線圖(K均值聚類)。 我的代碼如下:

nClust <- 3 
kmeans_output <- kmeans(inputdata_transaction_cluster, centers = nClust) 
inputdata_transaction_cluster$cluster = kmeans_output$cluster 


cols <- brewer.pal(nClust, "Set1") 
for(i in 1:nClust){ 
    inputdata_transaction_cluster$color[inputdata_transaction_cluster$cluster == i] <- cols[i] 
} 



plot_ly(inputdata_transaction_cluster, x =~timeStamp, y =~elapsed,type="scatter", mode = "markers", showlegend = FALSE, 
     hoverinfo = "x+y+text", text =~paste("Cluster:", cluster), 
     marker = list(opacity = 0.6, 
         color =~color, 
         size = 15, 
         line = list(color = "#262626", width = 3)))%>% 
    layout(      
    title = "CLUSTER", 
    xaxis = list(  
     title = ""),  
    yaxis = list(  
     title = "RESPONSE TIME") 
)%>% 
    layout(xaxis=ax,yaxis = ay) %>% 
    layout(hovermode = "closest", 
     showlegend = F, 
     title = paste("CLUSTER DIAGRAM :",unique(inputdata_transaction1$label)), 
     titlefont = list(color = "rgb(30,144,255)", size = 18)) 

我得到一個看起來像圖: Rplot

但正如你所看到的情節並不突出每個特定clusters.So的中心,有什麼辦法我可以使用情節R來實現這一點。

當我運行kmeans_output $中心,我得到每個羣集的值,但我怎麼能在圖中顯示。 謝謝提前尋求幫助。

回答

0

我已經在下面對您的問題做了一個模型。您可以使用點或註釋(或兩者)突出顯示中心。我已經使用模擬數據,因爲你的不可用。

library("plotly") 
library("RColorBrewer") 
nClust <- 3 
inputdata_transaction_cluster <- as.data.frame(matrix(rnorm(100), ncol=4)) 
colnames(inputdata_transaction_cluster) <- c("timeStamp", "elapsed", "color", 
    "label") 
kmeans_output <- kmeans(inputdata_transaction_cluster, centers = nClust) 
inputdata_transaction_cluster$cluster = kmeans_output$cluster 


cols <- brewer.pal(nClust, "Set1") 
for(i in 1:nClust){ 
    inputdata_transaction_cluster$color[inputdata_transaction_cluster$cluster == i] <- cols[i] 
} 



plot_ly(inputdata_transaction_cluster, 
    x =~timeStamp, 
    y =~elapsed, 
    type="scatter", mode = "markers", showlegend = FALSE, 
     hoverinfo = "x+y+text", text =~paste("Cluster:", cluster), 
     marker = list(opacity = 0.6, 
         color =~color, 
         size = 15, 
         line = list(color = "#262626", width = 3)))%>% 
    layout(      
    title = "CLUSTER", 
    xaxis = list(  
     title = ""),  
    yaxis = list(  
     title = "RESPONSE TIME") 
)%>% 
    layout(hovermode = "closest", 
     showlegend = F, 
     title = paste("CLUSTER DIAGRAM :", 
      unique(inputdata_transaction_cluster$label)), 
     titlefont = list(color = "rgb(30,144,255)", size = 18)) %>% 
## Relevant part: 
    add_markers(data=as.data.frame(kmeans_output$centers), 
    x=~timeStamp, 
    y=~elapsed, 
    inherit = FALSE, 
    color = c("red", "green", "blue"), 
    size = 15) %>% 
    add_annotations(data=as.data.frame(kmeans_output$centers), 
    x=~timeStamp, y=~elapsed, color = c("red", "green", "blue"), 
    text = paste("Cluster: ", rownames(kmeans_output$centers)))