plotly / plotly/plotly.R

style(hoveron="fills") doesn't work with ggplotly() when there are multiple polygons with the same fill color

Open
#1,641 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

The following code based on the standard North Carolina shape-file fills counties by the values of BIR74, except that one county has been given an NA value for the variable.

library(ggplot2)
library(plotly)
library(sf)

nc_1 <-
  st_read(system.file("shape/nc.shp", package = "sf"), 
              quiet = TRUE) %>% 
  mutate(BIR74 = ifelse(NAME == "Ashe", NA, BIR74)) %>% 
  mutate(tooltip_text = ifelse(is.na(BIR74), 
                               paste0(NAME, " not reporting"), 
                               paste0(NAME, " reports ", BIR74)))
p_1 <-
  nc_1 %>% 
  ggplot() +
  geom_sf(aes(fill = BIR74, text = tooltip_text))

ggplotly(p_1, tooltip = "text") %>% 
  style(hoveron = "fills")

The custom tooltip indicates the missing value (hover over Ashe County) as expected.

The following code sets BIR74 to NA for two counties:

nc_2 <-
  st_read(system.file("shape/nc.shp", package = "sf"), 
          quiet = TRUE) %>% 
  mutate(BIR74 = ifelse(NAME %in% c("Ashe", "Alleghany"), NA, BIR74)) %>% 
  mutate(tooltip_text = ifelse(is.na(BIR74), 
                               paste0(NAME, " not reporting"), 
                               paste0(NAME, " reports ", BIR74)))
p_2 <-
  nc_2 %>% 
  ggplot() +
  geom_sf(aes(fill = BIR74, text = tooltip_text))

ggplotly(p_2, tooltip = "text") %>% 
  style(hoveron = "fills")

Hover over either Ashe or Alleghany and you get identical very long tooltips.

The output of plotly_json(p_2) indicates that Ashe and Alleghany have somehow been combined. See, e.g.:

json_2 <- plotly_json(p_2)
json_2$x$data %>%
  as.character() %>% 
  RJSONIO::fromJSON() %>% 
  .$data %>% 
  .[[100]] %>% 
  .[1:3]

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

Run the two North Carolina examples with ggplotly(), tooltip = "text", and style(hoveron = "fills"), then inspect plotly_json(p_2) as shown. Compare how the one-NA and two-NA cases represent polygon data; done means Ashe and Alleghany each display their own tooltip instead of one combined long tooltip.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.