plotly / plotly/plotly.R

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

Open
#1,671 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
2.7k
Forks
641
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided R examples using ggplotly(), geom_sf(), the continuous and discrete fill scales, and tooltip = "text". Compare the generated tooltips for p1 and p2, including cases using hoveron = "text"; done means each polygon consistently displays its NAME and AREA rather than "a" or "Trace XX".

Written by the indexing model from the issue text.

Assessment

Tech stack
plotly, r
Domain
data-visualization
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.