【问题标题】:Manually set the legend names手动设置图例名称
【发布时间】:2019-08-26 19:20:39
【问题描述】:

我有下面的数据框,我可以正确处理以下数据框,以便创建一个集群散点图:

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() 时,图例名称会发生​​变化,因此我正在寻找一种手动设置它们的方法,甚至根本隐藏图例。

【问题讨论】:

  • p2&lt;-fviz_cluster(k2, data = cl, geom="point") 中有错字吗?应该是data=df。

标签: r ggplot2 ggplotly


【解决方案1】:

最简单的方法是重命名对象内的标签。

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

【讨论】:

  • 很有趣,但是这如何与 4 个集群(中心 = 4)而不是 2 个一起工作?我到处都用 4 替换 2,结果又错了
  • 好吧,抱歉,这更像是一个修复而不是解决方案。 [github.com/ropensci/plotly/issues/1164]他们似乎有类似的问题。如果我理解它们,请纠正它,因为fviz_cluster() - 产生多个比例,ggplolty() 无法处理它们。 [stackoverflow.com/questions/49133395/…这个好像差不多,他建议直接用plotly构建int。
  • 嗯,我明白了。问题是我无法直接用 plotly 找到相同的情节。您上面的答案会很棒,它可以与 4 个集群一起使用
  • 当然,我使用了您的示例数据和代码。或者你具体是什么意思?
  • 这可能是可能的。 'p3[["x"]][["data"]][[1]][["text"]]' 在那里你可以看到悬停文本中的内容,你可以只用状态名称替换它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-28
  • 2014-07-05
  • 2016-10-26
  • 1970-01-01
  • 2016-11-24
  • 2021-05-03
相关资源
最近更新 更多