我刚刚阅读了有关 ggiraph 包的信息,这似乎是我需要向通过 ggplot 创建的地图添加工具提示的内容。
这意味着我基本上已经更改了我的 ggplot 代码,因此 geom_map 行是 geom_map_interactive,并且我在美学位中添加了一个工具提示参数。
但是,我大概没有正确使用 geom_map_interactive 或 tooltips 参数,因为虽然它没有抛出任何错误,但它不会产生交互式地图 - 只是一个静态图,就像以前一样。
这是完整的代码:
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))