新的 ggirgraph 包对于使ggplot2响应用户的鼠标非常有用,没有问题。
然而我在ggmap()的交互/反应经纬度点上遇到了麻烦。
交互性工作得很好,当鼠标悬停在一个点时,我可以得到工具提示响应,但是在使用ggmap()时,比例或轴似乎存在问题。
下面的代码块提供了我试图解决的问题的可重现示例,并且还有一些链接的图像来说明我的意思。
首先安装所需的软件包,然后组成小的示例数据集,然后使用get_map()函数下载所需的map:
#Install required_packages:
required_packages <- c("ggmap", "ggplot2", "ggiraph")
new.packages <- required_packages[!(required_packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
#Load required_packages:
lapply(required_packages, require, character.only = TRUE)
#Make small example data set:
suburb<-c("BURNLEY","COLLINGWOOD","MALVERN","PRAHRAN","RICHMOND","SOUTH YARRA","ST KILDA","ST KILDA WEST","TOORAK","WINDSOR")
lon<-c(145.0176466,144.98815,145.036,144.998,144.998,144.989,144.982,144.9732,145.018,144.988)
lat<-c(-37.8299258, -37.8019,-37.857,-37.852,-37.823,-37.84,-37.864,-37.8604,-37.841,-37.854)
`Change 2005-2015 (%)`<-c(112, 120, 136, 127, 122, 115, 110, 146, 120, 128)
df<-data.frame(suburb, lon, lat, `Change 2005-2015 (%)`)
#Download map from google maps
SOUTH_YARRA <- get_map(location = 'South Yarra, Australia', zoom = 13, maptype="terrain")
现在我使用下面的代码来创建静态映射没有问题:
ggmap(SOUTH_YARRA) +
geom_point(data = df,
aes(x =lon, y= lat, size=`Change 2005-2015 (%)`, colour = `Change 2005-2015 (%)`),
alpha=0.75) +
scale_colour_gradientn(colours=rainbow(5)) +
scale_radius (range = c(6, 12), trans = "identity", guide = "legend") +
ggtitle("Total change in Median n House Price (%) from 2005-2015 n")
这是上面代码生成的静态映射-没问题
然而,当我使用ggiraph的geom_point_interactive()使地图中的点对用户的鼠标悬停作出反应时,比例或轴出现问题:
#Try add reactivity using ggiraph's geom_point_interactive() instead of geom_point()
interactive_map<-ggmap(SOUTH_YARRA) +
geom_point_interactive(data = df,
aes(x =lon, y= lat, size=`Change 2005-2015 (%)`, colour = `Change 2005-2015 (%)`, tooltip=suburb, data_id = suburb),
alpha=0.75) +
scale_colour_gradientn(colours=rainbow(5)) +
scale_radius (range = c(6, 12), trans = "identity", guide = "legend") +
ggtitle("Total change in Median Melbourne n House Price (%) from 2005-2015 n")
ggiraph(code = {print(interactive_map)}, zoom_max = 5,
tooltip_offx = 20, tooltip_offy = -10,
hover_css = "fill:black;",
tooltip_opacity = 0.7)
是上面代码生成的比例/轴问题的静态图像。注意,上面的代码使工具提示反应性在鼠标悬停时工作得很好,这只是我需要解决的这个缩放问题
我已经尝试改变maprange, extent和base_layer参数到ggmap()函数,例如:
ggmap(SOUTH_YARRA, maprange=TRUE, extent = "panel", base_layer = ggplot(data = df, aes(x =lon, y= lat)))
然而,这并没有帮助。ggirgraph是一个很棒的包,因为它是新的,还没有太多的stackoverflow等。任何帮助都将非常感激!
这是github包中的一个错误,包维护者刚刚修复了它,查看这里的github错误报告了解更多详细信息:https://github.com/davidgohel/ggiraph/issues/32#issuecomment-257869888