如何在R中的OSM图块上叠加点密度(ggplot2)?

时间:2014-04-03 23:03:26

标签: r ggplot2 openstreetmap spatial

我对我的R脚本有疑问: 如何使用谷歌地图或osm tile作为背景在R中覆盖point_density? 1)这很好,但不是我想要的:

library(ggmap)
map <- get_map("Vilnius", zoom = 14, source = "osm", color = "bw")
mapPoints <- ggmap(map)
p<-ggplot(geotag@data, aes(Y, X))
p + stat_density2d(aes(fill= ..level..), alpha =0.5, geom="polygon") + geom_point(aes(Y, X), colour="red", data = geotag@data, alpha = .5)

2)如果我改为使用osm tile作为背景,则点密度会失真(遗憾的是我无法显示图像)。点密度函数似乎无法识别点分布并创建对称叠加:

library(ggmap)
map <- get_map("Vilnius", zoom = 14, source = "osm", color = "bw")
mapPoints <- ggmap(map)
mapPoints + stat_density2d(aes(fill= ..level..), alpha =0.5, geom="polygon") + geom_point(aes(Y, X), colour="red", data = geotag@data, alpha = .5)

以下是一些示例数据:

OID_    Y   X
0   25.29315500000  54.68288700000
0   25.29375600000  54.68260200000
0   25.28416600000  54.68472200000
0   25.29776900000  54.68051900000
0   25.29549400000  54.68064800000
0   25.25535200000  54.67742600000
0   25.29541800000  54.68429700000
0   25.29751100000  54.68445600000
0   25.29541800000  54.68429700000
0   25.29541800000  54.68429700000
0   25.29751100000  54.68445600000
0   25.29751100000  54.68445600000
0   25.29751100000  54.68445600000
0   25.29751100000  54.68445600000
0   25.28865900000  54.68074300000
0   25.28943200000  54.67457100000
0   25.29133200000  54.68690000000
0   25.29176000000  54.68873800000
0   25.28049400000  54.67952800000

我做错了什么?

感谢您的帮助

1 个答案:

答案 0 :(得分:3)

这是你要找的吗?致电您的样本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)

问题是,在第一个代码段中,默认数据集在geotag@data调用中设置为ggplot(...),而在第二个代码段中,默认数据集在调用{}内部定义{1}}。在这种情况下,您必须在ggmap(...)的调用中明确定义(本地)数据集以及X和Y映射。