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)

k2 <- kmeans(df, centers = 2, nstart = 25)
df %>%
  as_tibble() %>%
  mutate(cluster = k2$cluster,
         state = row.names(USArrests))
p2<-fviz_cluster(k2, data = df, geom="point")
#+ scale_fill_discrete(name = "Cluster", labels = c("1", "2", "3","4"))
p2
ggplotly(p2)
library(tidyverse)#数据操作
库(群集)#群集算法
库(factoextra)#聚类算法与可视化
图书馆(绘本)

df我得到十字架的最简单方法是重命名对象内的标签

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

p3 <- ggplotly(p2)

p3[["x"]][["data"]][[2]][["name"]] <- "2"
p3

p2在
p2中是否有输入错误?有趣的是,这怎么可能与4个簇(中心=4)而不是2个簇一起工作?我到处用4替换2,这又错了。很抱歉,这更多的是一个修复方案,而不是解决方案。[他们似乎也有类似的问题。如果我理解他们的话,请纠正它,因为
fviz_cluster()
-产生多个刻度,而
ggplolty()
无法处理它们。[这似乎很相似,他建议直接使用
plotly
构建int。嗯,我明白了。问题是,我无法直接使用plotly找到相同的绘图。如果可以使用4个群集,您的回答会很好。我使用了您的示例数据和代码。或者您具体的意思是什么?可能是这样。'p3[“x”][“data”]][[1] ][[“text”]]”在这里,您可以看到悬停文本中包含的内容,您可以将其替换为州名称。
# Example
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)

# added center variable for number of centers in kmeans
# this will also be used to select elemnets from ggplot or ggplotly later

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

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

p2
p3 <- ggplotly(p2)

# Solution
# First Problem: Changing legend labels 
# Because the transition from ggplot to ggplotly
#   messes up multiple scales like here (color and shape)
# Why it looks like intended when only changing the point layer, 
#   I don't know

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

# Second Problem: interactive points
# ggplot saves the data in one list and ggplotly splits the data 
#    depending on layer and cluster
# for the labels it is enough to change the point layers 
#    (the first x depending on num. of centers)
# to add more inforamtion to labels 
#   manipulate the variable names_states with html
for (i in 1:centers) {
  name_states <- p2[["data"]]%>%
    filter(cluster==i)%>%
    select(name)

  p3[["x"]][["data"]][[i]][["text"]] <- as.vector(name_states$name)
}

# Changing order of layers because polygon-layer is on top and 
#    makes it impossible to hover over points beneeth
p3[["x"]][["data"]] <- p3[["x"]][["data"]][(centers*3):1]

# Now you can hover over every point and can see the state name
p3