在R中绘制地图上的密度点

时间:2014-10-30 22:38:09

标签: r map openstreetmap

我一直在尝试使用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, data=canada@data, 
                                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 个答案:

答案 0 :(得分:1)

我查看了您的代码并尽力了解这里发生了什么。简而言之,我建议您使用ggmap包。我不是GIS方面的专家,但在我看来,你得到的地图(即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, data=canada@data, 
                                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
#  .. .. .. ..@ 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 +nadgrids=@null +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和lat是我预期的数字。

### 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

所以,我猜测coords.x1中的coords.x2canada_map可能与对象中的数字mp不匹配。至少,由于mpcanada_map之间的lon和lat值的差异,有些事情发生了变化。为了使lon和lat值在数据(canada_map)和地图中保持一致,我使用了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