ggmap icon indicating copy to clipboard operation
ggmap copied to clipboard

Issue with aesthetic mapping

Open restonslacker opened this issue 8 years ago • 2 comments

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

ggmap_density

# 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

restonslacker avatar Oct 12 '16 20:10 restonslacker

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?

dkahle avatar Oct 12 '16 21:10 dkahle

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.

md0u80c9 avatar Apr 03 '18 13:04 md0u80c9