ggplotly shows wrong tooltip with geom_sf() and discrete fill scale
还没有人认领这个 Issue。
- 主要语言
- R
- 星标
- 2.7k
- 派生
- 641
- PR 合并指标
- 30 天内没有已合并 PR
描述
This is a copy of my question on Stack Overflow, because I suspect it's not intended behaviour.
I'm trying to create maps that'll show tooltips on hover with plotly. In the maps below, I've tried to make it show the name and the area of each place.
The first map, which has a continuous fill scale, shows the tooltip I'm asking for. But the second map, doesn't. The only difference is that the fill scale is discrete.
What's going on, and how can I fix it?
library(pacman)
p_load(sf, ggplot2, dplyr, plotly)
nc <- st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) %>%
mutate(x = factor(c(rep("a", 50), rep("b", 50))),
TEXT = paste(NAME, AREA))
# It works when I've got a continuous fill scale
p1 <- nc %>%
ggplot() +
geom_sf(aes(fill = AREA, text = TEXT))
#> Warning: Ignoring unknown aesthetics: text
ggplotly(p1, tooltip = "text") %>%
style(hoverlabel = list(bgcolor = "white"), hoveron = "text")
Why does this map say "a"?
# But it doesn't work with a discrete fill scale
p2 <- nc %>%
ggplot() +
geom_sf(aes(fill = x, text = TEXT))
#> Warning: Ignoring unknown aesthetics: text
ggplotly(p2, tooltip = "text") %>%
style(hoverlabel = list(bgcolor = "white"), hoveron = "text")
EDIT: turns out even the continuous map sometimes shows weird tooltips. Some areas show the right one, as above, others show "Trace XX":
From Stack Overflow answer: someone suggested removing hoveron = "text", which partially works. It shows the right tooltips, but only on the outlines of the shapes, so you don't know which shape it's referring to. Some shapes you can't get the tooltip at all, such as this one:

贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先使用 ggplotly()、geom_sf()、连续和离散填充比例尺以及 tooltip = "text" 运行所提供的 R 示例。比较 p1 和 p2 生成的工具提示,包括使用 hoveron = "text" 的情况;当每个多边形都一致地显示其 NAME 和 AREA,而不是显示 "a" 或 "Trace XX" 时,即表示完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- plotly, r
- 领域
- data-visualization
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 38/100


