2014-04-03 97 views
2

我有關於我的R腳本的問題: 如何在R中使用google map或osm tile作爲背景疊加point_density? 1)這工作得很好,但不是我想要的東西:如何在R中的OSM圖塊上疊加點密度(ggplot2)?

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

2)如果我改用OSM瓷磚作爲背景的點密度是扭曲的(可惜我不能顯示說明的圖像)。看來,點密度函數不能識別點的分佈,並創建一個對稱的疊加:

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 = [email protected], 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 

什麼,我做錯了什麼?

感謝您的任何幫助

+0

你能提供數據集'geotag @ data'(或它來自的空間數據框)嗎?這只是一個猜測,但失真可能在第一個圖中。 'ggmap(...)'將縱橫比設置爲1:1以避免失真*。你不會在第二段中做到這一點。嘗試在最後一條語句中添加'+ coord_fixed()'。 – jlhoward

回答

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) 

的問題是,在第一個片段,默認的數據集在通話設置爲[email protected]ggplot(...),而在第二個片段默認的數據集定義內部致電ggmap(...)。在這種情況下,您必須在調用stat_density(...)時明確定義(本地)數據集以及X和Y映射。

+0

是的,感謝您的支持! – Alfred

+0

在新的ggmap庫下,似乎打開打開街道地圖的指令不再有效。我從上面的'get_map'得到以下錯誤: '在download.file(url,destfile = destfile,quiet =!messaging,mode =「wb」): 無法打開URL'http://tile.openstreetmap .org/cgi-bin/export?bbox = 25.2522284950317,54.6712512196609,25.3071601356567,54.7030039563636&scale = 31000&format = png':HTTP狀態爲'400 Bad Request' –