手动设置图例名称

时间:2019-08-26 19:20:39

标签: 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 <- 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)

当我使用ggplotly()时,图例名称会更改,因此我正在寻找一种手动设置它们甚至完全隐藏图例的方法。

1 个答案:

答案 0 :(得分:2)

我遇到十字架的最简单方法是重命名对象中的标签。

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

p3 <- ggplotly(p2)

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

虽然不漂亮,但短期内会有所帮助。

编辑:所以有多个问题   第一:关于图例标签   第二:关于情节中的互动点     #给出了大多数示例代码,     #仅更改中心变量

# 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

result