问题描述
我想使用ggmap绘制在地图上的位置。因为我想用小面,我必须提供 base_layer
参数 ggmap
。我也试图在一个函数来包装这个。
我有定义我的地图边框变量:
long.range< - C(-71.5,-67.5)
lat.range< - C(42.5,44.5)
和定义数据的data.frame我想绘制:
test.data< - data.frame(名= C(站点1,站点2,site3),
LAT= C(43.25,43.4,44)
LONG= C(-71.25,-69.5,-68.5))
我有出去和抓住地图并应用data.frame作为base_layer功能:
CreateBaseMap< - 功能(lat.range = C(NA,NA)
long.range = C(NA,NA)
data.in = NULL){
#下载地图图块
base.map.in< - get_map(位置= C(分(long.range)
分钟(lat.range)
MAX(long.range)
MAX(lat.range))
来源=OSM)
#创建地图对象
如果(is.null(data.in)){
base.map< - ggmap(base.map.in)
}其他{
base.map< - ggmap(base.map.in,
base_layer = ggplot(aes_string(X =LONG,
Y =LAT),
数据= data.in))
}
base.map< - base.map +
实验室(X =经度,
Y =纵横)+
coord_map()
打印(base.map)
回报(base.map)
}
然后我用我的调用函数
base.map< - CreateBaseMap(lat.range = lat.range,long.range = long.range,data.in = test.data)
和我得到这个错误。
错误ggplot(aes_string(X =LONG,Y =LAT),数据= data.in):
对象data.in'未找到
疑难解答至今
我知道,如果我直接调用该函数的胆量,像这样的:
base.map< - ggmap(get_map(位置= C(分(long.range)
分钟(lat.range)
MAX(long.range)
MAX(lat.range))
来源=OSM),
base_layer = ggplot(aes_string(X =LONG,
Y =LAT),
数据= test.data))+
geom_point()
打印(base.map)
然后正常工作。
我一直在使用打印(data.in)还检查
的data.in存在,它获取到调用之前 base_layer
,我可以看到它的存在。
问题
看来,在调用 base_layer
不承认 data.in
。
- 如何说服
base_layer
,它真的要接受data.in
? - 这是
ggplot
有问题,还是我做错了什么?
解决的办法似乎是使用%+%
在 ggplot
即从 ggmap
调用创建的,而不是包括 base_layer
原调用项目 ggmap
。这绕过@baptiste确定的code问题。
要实现该方案,在以下code复制到位的#创建地图对象
在我原来的问题:
#创建地图对象
如果(is.null(data.in)){
base.map< - ggmap(base.map.in)
}其他{
base.map< - ggmap(base.map.in)%+%data.in + AES(X = LONG,
Y = LAT)
}
I am trying to use ggmap to plot locations on a map. Because I want to use faceting, I have to supply the base_layer
argument to ggmap
. I am also trying to wrap this in a function.
I have variables that define the bounding box of my map:
long.range <- c(-71.5, -67.5)
lat.range <- c(42.5, 44.5)
And a data.frame that defines the data I want to plot:
test.data <- data.frame("Name" = c("site1","site2","site3"),
"LAT" = c(43.25,43.4,44),
"LONG" = c(-71.25,-69.5,-68.5))
I have a function that goes out and grabs the map and applies the data.frame as the base_layer:
CreateBaseMap <- function(lat.range = c(NA,NA),
long.range = c(NA,NA),
data.in = NULL){
# download the map tile
base.map.in <- get_map(location = c(min(long.range),
min(lat.range),
max(long.range),
max(lat.range)),
source = "osm")
# create the map object
if (is.null(data.in)){
base.map <- ggmap(base.map.in)
} else {
base.map <- ggmap(base.map.in,
base_layer = ggplot(aes_string(x = "LONG",
y = "LAT"),
data = data.in))
}
base.map <- base.map +
labs(x = "Longitude",
y = "Latitude") +
coord_map()
print(base.map)
return(base.map)
}
and then I call my function using
base.map <- CreateBaseMap(lat.range = lat.range, long.range = long.range, data.in = test.data)
and I get this error.
Error in ggplot(aes_string(x = "LONG", y = "LAT"), data = data.in) :
object 'data.in' not found
Troubleshooting so far
I know if I call the guts of the function directly, like this:
base.map <- ggmap(get_map(location = c(min(long.range),
min(lat.range),
max(long.range),
max(lat.range)),
source = "osm"),
base_layer = ggplot(aes_string(x = "LONG",
y = "LAT"),
data = test.data)) +
geom_point()
print(base.map)
then it works fine.
I have also checked using print(data.in)
that the data.in exists before it gets to the call to base_layer
, and I can see that it's there.
Question
It appears that the call to base_layer
doesn't recognize data.in
.
- How can I persuade
base_layer
that it really wants to acceptdata.in
? - Is this a problem with
ggplot
, or am I doing something wrong?
The solution seems to be to use %+%
on the ggplot
item that is created from the ggmap
call, rather than including base_layer
in the original call to ggmap
. This bypasses the code problem that @baptiste identified.
To implement this solution, copy in the following code in place of the #create the map object
in my original question:
# create the map object
if (is.null(data.in)){
base.map <- ggmap(base.map.in)
} else {
base.map <- ggmap(base.map.in ) %+% data.in + aes(x = LONG,
y = LAT)
}
这篇关于创建base_layer为ggmap不承认data.frame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!