gpt4 book ai didi

R ggiraph with ggmap for interactive/reactive longitude and latitude points - scale or axis issue?

转载 作者:行者123 更新时间:2023-12-04 12:07:28 26 4
gpt4 key购买 nike

新的 ggiraph 包非常适合让 ggplot2 响应用户的鼠标,没问题。

但是,我在使用 ggmap() 处理交互式/响应式(Reactive)经度和纬度点时遇到了问题

交互性很好,当鼠标悬停在一个点上时我可以得到工具提示响应,但是当与 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")

现在我可以使用下面的代码创建静态 map 了:

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() 使 map 中的点对用户的鼠标悬停使用react时,比例或轴出现问题:

#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

我已经尝试将 maprange、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

关于R ggiraph with ggmap for interactive/reactive longitude and latitude points - scale or axis issue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40166761/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com