0

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?

  • I believe I saw an issue relating to this on the ggiraph github page when Googling this yesterday, but now I can't find it again. – Vilhelm Agdur Jan 09 '21 at 15:13
  • 1
    I think there are misunderstandings about the effect of some aes, for example, you are using simple numbers in onclick, this will have no effect, this is not a valid JS function. Then you are associating hover events on all data that contains NA. If you don't want to animate these, don't associate animations with them (i.e. don't use `data_id`) – David Gohel Jan 10 '21 at 07:30
  • Yes, it seems I included too much in a copy-paste. Changing to data_id=knkod fixed part of the problem. Is there any way to get a hover effect for non-NA data only? I can only figure out how to use data_id for either all data or none of it. – Vilhelm Agdur Jan 13 '21 at 21:35

1 Answers1

0

Appreciate this is an old question, but solved for it today.

Solution is to set your tooltip value to an empty string ('') for all NA values. When you do this, they don't show anything on hover.

If you want to avoid the values being highlighted together, just do as @David Gohel's comment and drop data_id from the call.

Tried to recreate your data to post a full solution but ran into problems installing `swemaps' that I didn't have time to fix.

An example of how to handle is posted below:

#load packages
library('maps') 
library('tidyverse')
library('ggiraph')

#generate map data
world_map <- map_data('world')

#Generate data with NA values
data <- data.frame('region' = unique(world_map$region)) %>%
  .[seq(2, nrow(.), 2), ] %>%
  data.frame() %>%
  add_column(value = runif(nrow(.))) %>%
  setNames(c('region', 'value')) %>%
  right_join(world_map, by = 'region')

#We want to show the names of countries on hover
#So set the value of the region name as '' in cases where value data is NA
data_map <- data %>%
  mutate(region = ifelse(is.na(value), '', region))

#Create the plot setting tooltip = region
#Note no call to data_id
plot = ggplot(data_map, 
       aes(x = long, y = lat, fill = value, group = group, tooltip = region)) +
  geom_polygon_interactive(col = 'black')

#Render the ggiraph object
ggiraph(ggobj = plot)
MorrisseyJ
  • 1,191
  • 12
  • 19