rggplot2ggplotlyfactoextra

Delete or hide the data points' labels


I have the dataframe below :

Val1<-c(0.5,0.7,0.8,0.9)
Val2<-c(0.5,0.7,0.8,0.9)
Val3<-c(0.5,0.7,0.8,0.9)
Val4<-c(0.5,0.7,0.8,0.9)
vales<-data.frame(Val1,Val2,Val3,Val4)
row.names(vales)<-c("asd","dasd","dfsdf","fdff")

which I process properly in order to create a cluster scatter plot with:

library(tidyverse)  # data manipulation
library(cluster)    # clustering algorithms
library(factoextra) # clustering algorithms & visualization
library(plotly)

cl<-scale(vales)
dist <- get_dist(cl)
k2 <- kmeans(cl, centers = 2, nstart = 25)

cl %>%
  as_tibble() %>%
  mutate(cluster = k2$cluster,
         state = row.names(vales))

p2<-fviz_cluster(k2, data = cl)
p2+geom_text(aes(label=""))

#or
ggplotly(p2+geom_text(aes(label="")))

I want to delete the points' labels and I do not understand why they appear while in the case below they do not.

df <- USArrests
df <- na.omit(df)

df <- scale(df)
distance <- get_dist(df)

k2 <- kmeans(df, centers = 2, nstart = 25)
df %>%
  as_tibble() %>%
  mutate(cluster = k2$cluster,
         state = row.names(USArrests))

p1 <- fviz_cluster(k2, geom = "point", data = df) + ggtitle("k = 2")
p1+geom_text(aes(label=""))


#or
ggplotly(p1+geom_text(aes(label="")))

Solution

  • By default, the geom argument of fviz_cluster is geom=c("point","text"). By specifying geom="point", labels are not displayed (geom="text" to only display labels).

    fviz_cluster(k2, data = cl, geom="point")