Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.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
R 手动设置悬停信息文本_R_Ggplot2_Ggplotly - Fatal编程技术网

R 手动设置悬停信息文本

R 手动设置悬停信息文本,r,ggplot2,ggplotly,R,Ggplot2,Ggplotly,我有一个数据框,在该数据框下我进行了适当处理,以创建一个具有以下内容的群集散点图: library(tidyverse) # data manipulation library(cluster) # clustering algorithms library(factoextra) # clustering algorithms & visualization library(plotly) df <- USArrests df <- na.omit(df) df

我有一个数据框,在该数据框下我进行了适当处理,以创建一个具有以下内容的群集散点图:

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')
library(tidyverse)#数据操作
库(群集)#群集算法
库(factoextra)#聚类算法与可视化
图书馆(绘本)
df