ggplotly shows wrong tooltip with geom_sf() and discrete fill scale
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- R
- Estrellas
- 2.7k
- Forks
- 641
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Descripción
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:

Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza ejecutando los ejemplos de R proporcionados usando ggplotly(), geom_sf(), las escalas de relleno continuas y discretas, y tooltip = "text". Compara los tooltips generados para p1 y p2, incluidos los casos que usan hoveron = "text"; se considera terminado cuando cada polígono muestra de forma coherente su NAME y AREA en lugar de "a" o "Trace XX".
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- plotly, r
- Área
- data-visualization
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 38/100


