2

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

Oliver
  • 1,098
  • 1
  • 11
  • 16
  • I have had different results when I use a continuous fill too. It seems there's a separate "trace" layer for every color/fill. – Arthur Yip Oct 08 '20 at 06:12
  • This blog post with code shows what we want but I can't reproduce it https://blog.cpsievert.me/2018/01/30/learning-improving-ggplotly-geom-sf/ – Arthur Yip Oct 08 '20 at 06:13

1 Answers1

0

You have already defined the tooltip content using text = TEXT. Just remove hoveron = "text" and everything will be OK. See this post for more info.

p1 <- nc %>% 
  ggplot() +
  geom_sf(aes(fill = AREA, text = TEXT))
#> Warning: Ignoring unknown aesthetics: text

ggplotly(p1, tooltip = "text") %>% 
  style(hoverlabel = list(bgcolor = "white"))

enter image description here

p2 <- nc %>% 
  ggplot() +
  geom_sf(aes(fill = x, text = TEXT))
#> Warning: Ignoring unknown aesthetics: text

ggplotly(p2, tooltip = "text") %>% 
  style(hoverlabel = list(bgcolor = "white"))

enter image description here

Majid
  • 1,836
  • 9
  • 19
  • 1
    Thank you. That gets me very close, but now the tooltip only shows up when you hover over the lines, not the interior of the areas. And because the lines for neighbouring areas are drawn on top of one another, you can't get some of the tooltips to show, no matter where you hover. E.g. Cabarrus, the downward-facing triangle, five counties to the West of Harnett. – Oliver Dec 13 '19 at 10:37
  • Not sure about why is that happening. Looks like I can't get around that one! – Majid Dec 14 '19 at 02:39
  • You can get the hover behavior you want by adding a `style(hoveron = "fill")` – commscho Jun 30 '20 at 02:47
  • I've also gotten different behaviour when I use ggplotly() %>% style(hoveron = "fills") – Arthur Yip Oct 08 '20 at 06:11
  • The workaround is shared here: https://github.com/ropensci/plotly/issues/1641#issuecomment-550477069 – Jiddu Alexander Jun 02 '21 at 12:49