plotly / plotly/plotly.R

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

Aperta
#1,671 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
R
Stelle
2.7k
Fork
641
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo gli esempi R forniti usando ggplotly(), geom_sf(), le scale di riempimento continue e discrete e tooltip = "text". Confronta i tooltip generati per p1 e p2, inclusi i casi che usano hoveron = "text"; il lavoro è completato quando ogni poligono visualizza costantemente il proprio NAME e AREA invece di "a" o "Trace XX".

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
plotly, r
Ambito
data-visualization
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.