plotly / plotly/plotly.R

ggplotly shows wrong tooltip with geom_sf() and discrete fill scale

Abierto
#1,671 1 comentario 0 reacciones 0 asignados Ver en GitHub

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")

map showing the correct tooltip

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")

map showing the wrong tooltip

EDIT: turns out even the continuous map sometimes shows weird tooltips. Some areas show the right one, as above, others show "Trace XX":

continuous map showing wrong tooltip in some areas

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:
image

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. 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

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.