新的ggiraph软件包非常适合使ggplot2对用户的鼠标具有反应性,没有问题。

但是我很难让它与ggmap()一起正确地用于交互式/反应性经度和纬度点。

交互性很好,当鼠标悬停在某个点上时我可以得到工具提示的响应,但是将其与ggmap()一起使用时,比例尺或轴似乎存在问题。

下面的代码块提供了我要解决的问题的可重现示例,并且还链接了一些图像以说明我的意思。

首先安装所需的软件包,然后组成小的示例数据集,然后使用get_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")


here is the static map produced by the code above - no problem

但是,当我使用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)


here is a still image of my scale/axis problem produced by the code above. Note the the code above makes the tooltip reactivity works fine for mouse hover, it's just this scale problem that I need to resolve

我尝试将mapmap,extent和base_layer参数更改为ggmap()函数,例如:

ggmap(SOUTH_YARRA, maprange=TRUE, extent = "panel", base_layer = ggplot(data = df, aes(x =lon, y= lat)))


但是,这没有帮助。 ggiraph是一个很棒的软件包恕我直言,因为它是新来的,所以关于stackoverflow等还不多。任何帮助将非常感激!

最佳答案

这是ggiraph软件包中的一个错误,并且软件包维护人员已对其进行了修复,有关更多详细信息,请参见此处的github错误报告:https://github.com/davidgohel/ggiraph/issues/32#issuecomment-257869888

08-28 21:44