firmo23
firmo23

Reputation: 8404

Unstable plotly behavior when editing the hoverinfo text

I use the dataframe below in order to perform Euclideian clustering with plotly and factoextra

library(tidyverse)  # data manipulation
library(cluster)    # clustering algorithms
library(factoextra) # clustering algorithms & visualization
library(plotly)
df <- USArrests
df <- na.omit(df)

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

p2<-fviz_cluster(k2, data = n[,-6], geom="point")

p3 <- ggplotly(p2)

Then I edit the legend names with:

for (i in 1:x) {
  p3[["x"]][["data"]][[i]][["name"]] <- i
}

and Im trying to do the same with the hover text. Before editing this here are its names.

p3[["x"]][["data"]][[1]][["text"]]
[1] "cluster: 1<br />cluster: 1<br />x: -1.7365521<br />y:  1.30054054<br />x: -1.7365521<br />y:  1.30054054"
[2] "cluster: 1<br />cluster: 1<br />x: -0.7496608<br />y:  1.68382066<br />x: -0.7496608<br />y:  1.68382066"
[3] "cluster: 1<br />cluster: 1<br />x: -2.3282754<br />y:  1.19213607<br />x: -2.3282754<br />y:  1.19213607"
[4] "cluster: 1<br />cluster: 1<br />x: -2.2087966<br />y:  0.88703475<br />x: -2.2087966<br />y:  0.88703475"
[5] "cluster: 1<br />cluster: 1<br />x: -1.9120555<br />y:  2.31540057<br />x: -1.9120555<br />y:  2.31540057"
[6] "cluster: 1<br />cluster: 1<br />x: -1.9984170<br />y:  2.13036421<br />x: -1.9984170<br />y:  2.13036421"
[7] "cluster: 1<br />cluster: 1<br />x: -2.1332496<br />y:  1.82577968<br />x: -2.1332496<br />y:  1.82577968"
[8] "cluster: 1<br />cluster: 1<br />x: -1.7134769<br />y:  1.07585032<br />x: -1.7134769<br />y:  1.07585032"

Then Im trying to edit by giving the name of the state and the cluster.

p3[["x"]][["data"]][[1]][["text"]]<-paste(rownames(df),"Cluster:",n$cluster)

And everything seems to be alright when I check.

p3[["x"]][["data"]][[1]][["text"]]
 [1] "Alabama Cluster: 1"        "Alaska Cluster: 2"         "Arizona Cluster: 2"       
 [4] "Arkansas Cluster: 1"       "California Cluster: 2"     "Colorado Cluster: 2"      
 [7] "Connecticut Cluster: 4"    "Delaware Cluster: 4"       "Florida Cluster: 2"       
[10] "Georgia Cluster: 1"        "Hawaii Cluster: 4"         "Idaho Cluster: 3"         
[13] "Illinois Cluster: 2"       "Indiana Cluster: 4"        "Iowa Cluster: 3"          
[16] "Kansas Cluster: 4"         "Kentucky Cluster: 3"       "Louisiana Cluster: 1"     
[19] "Maine Cluster: 3"          "Maryland Cluster: 2"       "Massachusetts Cluster: 4" 
[22] "Michigan Cluster: 2"       "Minnesota Cluster: 3"      "Mississippi Cluster: 1"   
[25] "Missouri Cluster: 2"       "Montana Cluster: 3"        "Nebraska Cluster: 3"      
[28] "Nevada Cluster: 2"         "New Hampshire Cluster: 3"  "New Jersey Cluster: 4"    
[31] "New Mexico Cluster: 2"     "New York Cluster: 2"       "North Carolina Cluster: 1"
[34] "North Dakota Cluster: 3"   "Ohio Cluster: 4"           "Oklahoma Cluster: 4"      
[37] "Oregon Cluster: 4"         "Pennsylvania Cluster: 4"   "Rhode Island Cluster: 4"  
[40] "South Carolina Cluster: 1" "South Dakota Cluster: 3"   "Tennessee Cluster: 1"     
[43] "Texas Cluster: 2"          "Utah Cluster: 4"           "Vermont Cluster: 3"       
[46] "Virginia Cluster: 4"       "Washington Cluster: 4"     "West Virginia Cluster: 3" 
[49] "Wisconsin Cluster: 3"      "Wyoming Cluster: 4"       
> 

but when I display the plot with

p3

some of the points are correctly edited and some not why does this happen?

Upvotes: 0

Views: 45

Answers (1)

Marco Sandri
Marco Sandri

Reputation: 24252

Try using:

p3 <- ggplotly(p2)
for (k in 1:x) {
  p3[["x"]][["data"]][[k]][["name"]] <- i
  dtk <- subset(n, cluster==k)
  p3[["x"]][["data"]][[k]][["text"]] <- paste(dtk$state,"Cluster:",dtk$cluster)
}

Upvotes: 1

Related Questions