Search code examples
rggplot2data-visualizationcluster-analysis

Is there a way to single out data points in a jammed ggplot?


Cluster plot

This is a ggplot produced by fviz_cluster function. The problem is that I want to single out some data points or even a single one and label it by name. I can easily identify the data points that are on the edges, but what happens for those in the middle of the pack. I thought of approaching this by labelling every data point and then zoom in, but the plot is not interactive and I can't find a way to do that. Any idea on how to approach this would be much appreciated

Here is my code:


r=as.data.frame(colnames(df))

remov=c(1,2,5,7,11,14,16,21,23,24,25,26,89,29:54)

df=df[,-remov]

rem=c(2,3,6,7,8,9,12,13,14,15,16)
rownam=as.data.frame(unique(df$Name))
k=df[,-rem]
k= k[!duplicated(df$Name),]
k <- data.frame(k[,-1], row.names = k[,1])

k=na.omit(k)

kme=kmeans(k,centers = 6,nstart = 25)

str(kme)


fviz_cluster(kme, data = k,geom="point")




Solution

  • If you want an easy workaround to make your graph interactive use the ggplotly() function.

    install plotly:

    install.packages("plotly")
    library(plotly)
    
    plot <- fviz_cluster(kme, data = k,geom="point")
    
    plot %>% ggplotly()
    
    # or
    
    ggplotly(plot)