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")
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")
EDIT: turns out even the continuous map sometimes shows weird tooltips. Some areas show the right one, as above, others show "Trace XX":