使用ggiraph将工具提示添加到geom_map

时间:2017-09-01 10:57:05

标签: r ggplot2 mapping tooltip ggiraph

我刚刚读到了ggiraph包,看起来我可能需要将工具提示添加到通过ggplot创建的地图中。

这意味着我基本上改变了我的ggplot代码,以便geom_map行是geom_map_interactive,并且我在美学位中添加了一个工具提示参数。

然而,我可能没有正确使用geom_map_interactive或工具提示参数,因为虽然它不会产生任何错误,但它不会产生交互式地图 - 只是一个静态的情节,就像之前一样。

以下是完整代码:

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 个答案:

没有答案