Is this what you're looking for? Calling your sample df
:
library(ggmap)
map <- get_map("Vilnius", zoom = 14, source = "osm", color = "bw")
mapPoints <- ggmap(map)
mapPoints +
stat_density2d(data=df,aes(Y,X, fill= ..level..), alpha =0.5, geom="polygon") +
geom_point(data=df, aes(Y, X), colour="red", alpha = .5)
The problem was that, in the first snippet, the default dataset is set to geotag@data
in the call to ggplot(...)
, whereas in the second snippet the default dataset is defined internally in the call to ggmap(...)
. In that case you have to explicitly define the (local) dataset, and the X and Y mappings, in the call to stat_density(...)
.