Adding tooltips to geom_map using ggiraph

364 Views Asked by At

I just read about the ggiraph package, which seems like it might be what I need to add tooltips to a map created through ggplot.

This means I have basically changed my ggplot code so that the geom_map line is geom_map_interactive, and I've added a tooltips argument to the aesthetics bit.

However I presumably am not using the geom_map_interactive or tooltips arguments correctly as although it doesn't throw up any errors, it doesn't produce an interactive map - just a static plot, as it did before.

Here is the full code:

install.packages("rio") 
install.packages("rgeos")
install.packages("maptools")
install.packages("mapproj")
require(rgdal)
install.packages("plyr") 
require(ggplot2)
install.packages("ggiraph")
library(ggiraph)

datafile  <- rio::import("location of datafile on computer", header=TRUE)

shapefile <- readOGR(dsn = "location of shapefile on computer", layer = "shapefile")

shapefile <- fortify(shapefile, region = "region")

mapfile <- ggplot()  +
  geom_map_interactive(data = datafile , aes(map_id = datafile$region, fill = datafile$data_required, tooltip = paste(datafile$data_required),  map = shapefile) +
  geom_polygon (data = shapefile, aes(x = long, y = lat, group = group), colour = "darkgray", fill = NA) +
  expand_limits(x = shapefile$long, y = shapefile$lat) +
  scale_fill_gradient (guide = "colourbar", low = ("antiquewhite1"), high = ("dodgerblue4"), limits = c(1250,1850), breaks = c(1250,1850))  +
  ggtitle("Data shown in this map")  +
  labs(fill = "") +
  coord_equal () +
  theme(
axis.text.x = element_blank(), axis.text.y = element_blank(), 
axis.ticks = element_blank(), axis.title.x = element_blank(), 
axis.title.y = element_blank(), 
panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
panel.border = element_blank(), panel.background = element_blank(), 
legend.title = element_text(face = "bold"),
plot.title = element_text(face = "bold", hjust = 0.5)) 
0

There are 0 best solutions below