2014-10-30 163 views
1

我一直試圖在R中使用ggplot2和openstreet地圖繪製加拿大不同地區的遊客人數,但我似乎錯過了一步,因爲我的觀點都是最終在地圖的右下角,我的地圖縮小了。在R的地圖上繪製密度點

下面是我在數據集map.tourists中使用的一些數據。

id Nb.Touristes Nb.Nuitees 
1001 939.9513 1879.903 
1004 1273.4336 2546.867 
1006 776.5203 3882.602 
1010 3118.4872 18598.194 
1102 921.7354 3971.677 
1103 622.8770 1245.754 

這是我到目前爲止的代碼。帶有座標的數據位於我下面代碼中下載的Statistic Canada文件中。

download.file("http://www12.statcan.gc.ca/census-recensement/2011/geo/bound-limit/files-fichiers/gcd_000b11a_e.zip", destfile="gcd_000b11a_e.zip") 
unzip("gcd_000b11a_e.zip") 
library(maptools) 
canada<-readShapeSpatial("gcd_000b11a_e") 

library(GISTools) 
CDCenters <- coordinates(canada) 
CDCenters <- SpatialPointsDataFrame(coords=canada, [email protected], 
           proj4string=CRS("+proj=longlat +ellps=clrk66")) 
CDCenters=data.frame(CDCenters, row.names=NULL , id=CDCenters$CDUID) 

canada_map <- merge(CDCenters, map.tourists, by="id") 

list <- ls() 
list <- list[-grep("canada_map", list)] 
rm(list=list) 
rm(list) 

Sys.setenv(NOAWT=1) 
library(OpenStreetMap) 
library(rgdal) 
library(stringr) 
library(ggplot2) 

mp <- openmap(c(71, -143), c(40, -50), zoom=4, type="osm",mergeTiles=TRUE) 
library(ggplot2) 

autoplot(mp) + 
geom_point(data=canada_map, alpha = I(8/10), aes(x=coords.x1,y=coords.x2, size=Nb.Touristes, color=Nb.Touristes)) + 
theme(axis.line=element_blank(), axis.text.x=element_blank(), axis.text.y=element_blank(), axis.ticks=element_blank(), axis.title.x=element_blank(), axis.title.y=element_blank()) + 
scale_size_continuous(range= c(1, 25)) + 
scale_colour_gradient(low="blue", high="red") + 
labs(title="Nombre de touristes à Montréal en 2010 selon la division de recensement d’origine") 

我會張貼我得到的圖像,但我沒有足夠的聲望呢!

我得到兩個傳說,地圖集中在左上角,所有的點似乎是在右下角......

我能做些什麼?

謝謝!

回答

1

我仔細檢查了你的代碼,並盡我所能去看看這裏發生了什麼。總之,我建議你使用ggmap包。我不是地理信息系統的專家,但在我看來,你得到的地圖(即mp)不是ggplot喜歡的東西。

library(maptools) 
library(GISTools) 
library(ggmap) 
library(ggplot2) 

### Following the OP here 
download.file("http://www12.statcan.gc.ca/census-recensement/2011/geo/bound-limit/files-fichiers/gcd_000b11a_e.zip", destfile="gcd_000b11a_e.zip") 

unzip("gcd_000b11a_e.zip") 

canada<-readShapeSpatial("gcd_000b11a_e") 

CDCenters <- coordinates(canada) 
CDCenters <- SpatialPointsDataFrame(coords=canada, [email protected], 
           proj4string=CRS("+proj=longlat +ellps=clrk66")) 
CDCenters <- data.frame(CDCenters, row.names=NULL , id=CDCenters$CDUID) 


### Tourist data 

dat <- structure(list(id = c(1001L, 1004L, 1006L, 1010L, 1102L, 1103L 
), Nb.Touristes = c(939.9513, 1273.4336, 776.5203, 3118.4872, 
921.7354, 622.877), Nb.Nuitees = c(1879.903, 2546.867, 3882.602, 
18598.194, 3971.677, 1245.754)), .Names = c("id", "Nb.Touristes", 
"Nb.Nuitees"), class = "data.frame", row.names = c(NA, -6L)) 


### Merge the map data and tourist data 

canada_map <- merge(CDCenters, dat, by="id") 

### OK, now I want to get maps in two different ways. 

### This is by the OP 
mp <- openmap(c(71, -143), c(40, -50), zoom=4, type="osm",mergeTiles=TRUE) 

#str(mp) 
#List of 2 
# $ tiles:List of 1 
# ..$ :List of 5 
# .. ..$ colorData : chr [1:701964] "#B5D0D0" "#B5D0D0" "#B5D0D0" "#B5D0D0" ... 
# .. ..$ bbox  :List of 2 
# .. .. ..$ p1: num [1:2] -15918687 11402272 
# .. .. ..$ p2: num [1:2] -5565975 4865942 
# .. ..$ projection:Formal class 'CRS' [package "sp"] with 1 slots 
# .. .. .. [email protected] projargs: chr "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m [email protected] +no_defs" 
# .. ..$ xres  : int 666 
# .. ..$ yres  : int 1054 
# .. ..- attr(*, "class")= chr "osmtile" 
# $ bbox :List of 2 
# ..$ p1: num [1:2] -15918687 11402272 
# ..$ p2: num [1:2] -5565975 4865942 
# - attr(*, "zoom")= int 4 
# - attr(*, "class")= chr "OpenStreetMap" 

在這裏我沒有看到LON和LAT像40,50和60這在某種程度上讓我覺得,ggplot可能不喜歡的值。

enter image description here

下面是使用ggmap當我打印出圖像,lon和緯度是在數字預計我另一地圖。

### Get openstreetmap using ggmap 
ca.map2 <- get_openstreetmap(bbox= c(left = -143, bottom = 40, right = -50, top = 71), 
            scale = 69885283, format = "png") 

#str(ca.map2) 
#chr [1:334, 1:529] "#B5D0D0" "#B5D0D0" "#B5D0D0" "#B5D0D0" "#B5D0D0" "#B5D0D0" "#B5D0D0" ... 
#- attr(*, "class")= chr [1:2] "ggmap" "raster" 
#- attr(*, "bb")='data.frame': 1 obs. of 4 variables: 
#..$ ll.lat: num 40 
#..$ ll.lon: num -143 
#..$ ur.lat: num 71 
#..$ ur.lon: num -50 

enter image description here

所以,我猜測,在canada_mapcoords.x1coords.x2很可能不與對象,mp數字匹配。至少,由於mpcanada_map之間的lon和lat值的差異,會出現一些棘手的情況。爲了使您的數據(canada_map)和地圖的lon和lat值保持一致,我使用了ggmap對象(ca.map2)並繪製了一個圖形。如果你想在一個傳說中擁有顏色和大小,這就是你做這件事的方式。總之,您可能想要堅持使用ggmapggplot以避免將來出現類似問題。

ggmap(ca.map2) + 
geom_point(data = canada_map, 
      aes(x=coords.x1,y=coords.x2, size = Nb.Touristes, color = Nb.Touristes)) + 
guides(colour = guide_legend()) 

enter image description here