I am trying to use ggmap to place locations on a map. Since I want to use faceting, I have to provide the base_layer argument for 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 data.frame, which defines the data I want to build:
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 exits and grabs a map and applies data.frame as 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 that I call the gut function directly, for example:
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 great.

I also checked with print(data.in) that data.in exists before it reaches the base_layer call, and I see that it is there.
Question
It seems that calling base_layer does not recognize data.in
- How can I convince
base_layer that it really wants to accept data.in ? - Is this a problem with
ggplot , or am I doing something wrong?