I have data for some municipalities in Sweden, but not all of them. I would like to plot this data on a map, with tooltips indicating exact values and name of the municipality on hover.
The below code does this, but there is a serious problem -- when you hover over a municipality for which the data is NA, every municipality for which data is missing is highlighted in orange, not just the one that is being hovered. You can see the result of the below code here, if you don't want to run it on your own machine.
library(ggiraph)
library(ggplot2)
# devtools::install_github('reinholdsson/swemaps')
library(swemaps)
# Map data now in: map_kn
# For all municipalities except three, our data is missing:
allMunicipalities <- levels(map_kn$knnamn)
dat <- data.frame(knnamn = allMunicipalities, PlotVar = NA, ttip = NA)
dat$PlotVar[45] <- 8.3
dat$ttip[45] <- "Finsp?ng: 8.3"
dat$PlotVar[32] <- 7.2
dat$ttip[32] <- "Eda: 7.2"
dat$PlotVar[103] <- 11.9
dat$ttip[103] <- "Klippan: 11.9"
# Join our values to plot with the frame containing the map data:
plotData <- left_join(map_kn, dat, by="knnamn")
# Draw the map:
p <- ggplot(plotData, aes(ggplot_long, ggplot_lat)) +
geom_polygon_interactive(aes(fill = PlotVar, group = knkod,
tooltip = ttip, data_id = PlotVar, onclick = PlotVar)) +
coord_equal()
# Have a look at it:
girafe(ggobj = p)
Ideally the municipalities for which we have no data would be completely inert, and nothing would happen when hovering them, but I could be okay with just that one municipality being highlighted with a tooltip indicating there is no data, if the ideal version isn't possible.
EDIT:
Some more trial-and-error showed that in fact any two municipalities which have the same value, whether it is NA or some other value, will always be highlighted together. So it appears to be some problem with how the polygons are drawn?
question from:
https://stackoverflow.com/questions/65644153/turn-off-hover-effect-in-ggiraph-map-when-data-is-missing 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…