0

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)) 
Jaccar
  • 1,720
  • 17
  • 46
  • 1
    Can you edit this a minimal reproducible example? Use the least amount of code and packages to reproduce your error or undesired behavior. Don't include extra code (like all of the ggplot theme options), unless it is causing your issue. SO happy to help – Nate Sep 01 '17 at 11:26
  • 1
    I found that creating maps via `ggplot` is not optimal; consider using the `tmap` package, which works nicely with `sp` objects like `spatialPolygonsDataFrame` and `spatialPixelsDataFrame`. It is also capable of producing an interactive HTML map (leaflet). – Jindra Lacko Sep 01 '17 at 14:27

0 Answers0