2

I have the dataframe below 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)
df <- USArrests
df <- na.omit(df)

df <- scale(df)
distance <- get_dist(df)
    x=4
k2 <- kmeans(df, centers = x, nstart = 25)
df %>%
  as_tibble() %>%
  mutate(cluster = k2$cluster,
         state = row.names(USArrests))

p2<-fviz_cluster(k2, data = df, geom="point")

p3 <- ggplotly(p2)
for (i in 1:x) {
  p3[["x"]][["data"]][[i]][["name"]] <- i
}
p3
p3%>%add_trace(
         text = rownames(df),
         hoverinfo = 'text')

When I use ggplotly() then the clusters names are displayed twice and besides that I want to display the rownames (states) of the dataset instead. Hoverinfo text and legend names change with the same logic? I do not think so.

firmo23
  • 7,490
  • 2
  • 38
  • 114

0 Answers0