编辑悬停信息文本时,不稳定的绘图行为

时间:2019-08-29 14:43:35

标签: r plotly

我使用下面的数据框来对plotlyfactoextra进行欧几里得聚类

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)

然后我用以下命令编辑图例名称:

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

,而我正在尝试对悬停text执行相同操作。在编辑之前,这里是它的名字。

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"

然后我试图通过给出州和集群的名称来进行编辑。

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

当我检查时,一切似乎都很好。

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"       
> 

但是当我显示图时

p3

某些要点已正确编辑,而有些为什么不这样做?

1 个答案:

答案 0 :(得分:1)

尝试使用:

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)
}