为 ggmap 创建 base_layer 无法识别 data.frame [英] Creating base_layer for ggmap not recognizing data.frame

查看:21
本文介绍了为 ggmap 创建 base_layer 无法识别 data.frame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ggmap 在地图上绘制位置.因为我想使用分面,所以我必须向 ggmap 提供 base_layer 参数.我也在尝试将其包装在一个函数中.

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)

还有一个 data.frame 定义了我想要绘制的数据:

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))

我有一个函数可以出去抓取地图并将data.frame应用为base_layer:

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)
}

然后我使用

base.map <- CreateBaseMap(lat.range = lat.range, long.range = long.range, data.in = test.data)

我收到这个错误.

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)

然后它工作正常.

我还使用 print(data.in) 检查了 data.in 在它调用 base_layer 之前是否存在,我可以看到它在那里.

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.

base_layer 的调用似乎无法识别data.in.

It appears that the call to base_layer doesn't recognize data.in.

  1. 我怎样才能说服 base_layer 它真的想要接受 data.in?
  2. 这是 ggplot 的问题,还是我做错了什么?
  1. How can I persuade base_layer that it really wants to accept data.in?
  2. Is this a problem with ggplot, or am I doing something wrong?

推荐答案

解决方案似乎是在从创建的 ggplot 项上使用 %+%ggmap 调用,而不是在对 ggmap 的原始调用中包含 base_layer.这绕过了@baptiste 发现的代码问题.

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.

要实现此解决方案,请复制以下代码以代替我原始问题中的 #create the map object:

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)
  }

这篇关于为 ggmap 创建 base_layer 无法识别 data.frame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆