简体   繁体   中英

Plotting density points on a map in R

I've been trying to plot the number of tourists from different regions in Canada on a map in R using ggplot2 and openstreet map, but I seem to be missing a step as my points all end up in the bottom right corner of the map and my map shrinks in size.

Here are some of the data I used in the dataset 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

And here is the code I have until now. The data with the coordinates is in the Statistic Canada file that I download in the code below.

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")

I'd post the image that I get, but I don't have enough reputation yet !

I get two legends, the map is concentrated in the top left corner and all the points seem to be in the bottom right corner...

What can I do?

Thank you!!

I went through your code and did my best to see what is happening here. In short, I would recommend you to use the ggmap package. I am not an expert in GIS, but it seems to me that the map you got (ie, mp) is not something ggplot likes.

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"

Here I do not see lon and lat like 40,50, and 60. This somehow makes me think that ggplot may not like the values.

在此处输入图片说明

Here is another map using ggmap When I printed out the image, lon and lat are in numbers I expected.

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

在此处输入图片说明

So, I am guessing that coords.x1 and coords.x2 in canada_map are probably not matching with the numbers in the object, mp . At least, there is something screwy happening due to the difference in lon and lat values between mp and canada_map . In order to make lon and lat values consistent in your data (canada_map) and a map, I used the ggmap object (ca.map2) and drew a graphic. If you want to have colour and size in one legend this is the way you do it. In summary, you may want to stick to ggmap and ggplot in order to avoid similar problems in the future.

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())

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM