ggmap
ggmap copied to clipboard
Issue with aesthetic mapping
Please note that in the following example geom_density
is being used as a stand-in for a custom stat/geom that I wrote for internal projects at my office.
library(ggmap)
bbox <- c(left=-.1, bottom=-.1, right=1.1,top=1.3 )
zm <- calc_zoom(bbox)
map <- get_openstreetmap(bbox, OSM_scale_lookup(zm))
dat <- data.frame(x=runif(100))
# this works
ggmap(map, base_layer = ggplot(dat))+geom_density(aes(x=x), fill="green")
# this produces an error
ggmap(map)+geom_density(aes(x=x),data=dat, fill="green")
Error in eval(expr, envir, enclos) : object 'lat' not found
# a different error
ggmap(map)+geom_density(aes(x=runif(100)), fill="green")
Error: Aesthetics must be either length 1 or the same as the data (4): x, y
Tested under R 3.3.1
with ggmap 2.6.1
on Windows
I think this is because geom_density()
, through stat_density()
, is creating variables other than lat
that are being mapped to the y
aesthetic. For example, this works:
ggmap(map) +
geom_density(aes(x = x, y = ..density..), data=dat, fill="green")
Have you tried referencing the variable created by the stat?
Is this fixed by the bug fix to base_layer? I certainly saw this in the stable version on CRAN but on the latest version I've had no such issues at all.