Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
编辑hoverinfo文本时,绘图行为不稳定_R_Plotly - Fatal编程技术网

编辑hoverinfo文本时,绘图行为不稳定

编辑hoverinfo文本时,绘图行为不稳定,r,plotly,R,Plotly,我使用下面的数据框来执行带有plotly和factoextra library(tidyverse) # data manipulation library(cluster) # clustering algorithms library(factoextra) # clustering algorithms & visualization library(plotly) df <- USArrests df <- na.omit(df) dfa <- sca

我使用下面的数据框来执行带有
plotly
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)
然后,我试图通过给出状态和集群的名称来进行编辑

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

p3
有些点已正确编辑,有些点未正确编辑。为什么会发生这种情况?

尝试使用:

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