2012-02-27 96 views
5

好日子大家,GGPLOT2和地圖:geom_point和annotation_raster的位置偏移

使用下面的代碼,我可以用ggmap成功檢索從谷歌的光柵,繪製一個annotation_raster使用ggplot2,和情節的網站地方如在上面的紅點的柵格圖層。在情節上,職位不太匹配(他們應該跟隨海岸線)。我知道我的網站的位置是正確的,因爲當我將數據作爲KML文件上傳到Google地球時,它們繪製了它們應該在的位置。

建議將不勝感激。

此代碼將按原樣運行...請注意,您需要開發版本爲ggplot2,該版本可在github上獲得。要安裝:

# install.packages("devtools") 
library(devtools) 
install_github("ggplot2") 

及代碼:

library(ggplot2) 
library(ggmap) 
library(grDevices) 
theme_set(theme_bw()) 

# Some coordinates of points to plot: 
siteLat = c(-22.94414, -22.67119, -29.25241, -30.31181, -32.80670, -33.01054, -32.75833, -  33.36068, -31.81708, -32.09185, -32.31667, -34.13667, -34.05016, -33.91847, -34.13525, -34.12811, -34.10399, -34.16342, -34.41459, -34.58786, -34.83353, -34.37150, -34.40278, -34.17091, -34.08565, -34.04896, -33.98066, -34.02448, -34.20667, -34.05889, -33.97362, -33.99125, -33.28611, -33.02407, -33.01798, -32.99316, -31.09704, -31.05000, -30.91622, -30.70735, -30.28722, -30.27389, -29.86476, -29.54501, -29.49660, -29.28056, -28.80467, -27.42472) 
siteLon = c(14.50175, 14.52134, 16.86710, 17.26951, 17.88522, 17.95063, 18.02778, 18.15731, 18.23065, 18.30262, 18.32222, 18.32674, 18.34971, 18.38217, 18.43592, 18.45077, 18.48364, 18.85908, 19.25493, 19.33971, 20.00439, 21.43518, 21.73972, 22.12749, 23.05532, 23.37925, 23.64567, 23.89933, 24.77944, 25.58889, 25.64724, 25.67788, 27.48889, 27.91626, 27.92182, 27.95036, 30.18395, 30.21666, 30.32982, 30.48474, 30.76026, 30.83556, 31.04479, 31.21662, 31.24665, 31.44403, 32.07567, 32.73333) 
siteName = c(seq(1:length(siteLon))) 
sites <- as.data.frame(cbind(siteLat, siteLon, siteName)) 

# specify raster's approximate coordinates: 
lats = c(-35, -20) 
lons = c(10, 35) 

SAMap <- GetMap.bbox(lons, lats, maptype = "satellite") 

# extract "real" coords of raster: 
lonr <- c(SAMap$BBOX$ll[2], SAMap$BBOX$ur[2]) 
latr <- c(SAMap$BBOX$ll[1], SAMap$BBOX$ur[1]) 

# extract raster fill data: 
h_raster <- as.raster(SAMap$myTile) 

# plot using annotation_raster: 
g <- ggplot(sites, aes(siteLon, siteLat)) 
g + annotation_raster(h_raster, lonr[1], lonr[2], latr[1], latr[2]) + 
    geom_point(aes(x = siteLon, y = siteLat), colour = "red", data = sites) + 
    scale_x_continuous(limits = lonr) + 
    scale_y_continuous(limits = latr) 

(對不起,我不能,因爲我是新來張貼圖片)。

+0

這並不在我的機器上運行。我懷疑你還需要包含'library(raster)'。即使如此,仍無法找到'annotation_raster'。這是哪個包(和版本)? – Andrie 2012-02-27 13:27:13

+0

我想你需要一個實驗性的'ggplot2'版本,請參閱https://github.com/hadley/ggplot2以獲取安裝信息。 – 2012-02-27 14:02:34

+0

我認爲'as.raster()'住在'library(grDevices)'中。 'annotation_raster'在'ggplot2'版本0.9.0中。將更新頂部的代碼。 – 2012-02-27 14:11:29

回答