R:创建选定的加拿大省份和美国各州的地图 [英] R: creating a map of selected Canadian provinces and U.S. states
问题描述
我正在尝试创建选定的加拿大各省/地区和选定的美国各州的地图.到目前为止,最好的地图似乎是使用GADM数据生成的地图: http://www.gadm.org/
I am attempting to create a map of selected Canadian provinces/territories and selected U.S. states. So far the nicest maps appear to be those generated with GADM data: http://www.gadm.org/
但是,我无法在同一张地图上绘制美国和加拿大,也无法仅绘制选定的省/地区和州.例如,我对阿拉斯加,育空地区,西北地区,不列颠哥伦比亚省,艾伯塔省和蒙大拿州等地区感兴趣.
However, I have not been able to plot the U.S. and Canada on the same map or plot only selected provinces/territories and states. For example, I am interested in Alaska, Yukon, NWT, British Columbia, Alberta, and Montana among others.
此外,美国地图似乎是沿着国际日期线分割的.
Also, the U.S. map appears to be split along the international dateline.
有人可以帮我吗?
- 在单个地图上绘制上述省/地区和州
- 避免让美国沿国际日期变更线
- 覆盖经纬度网格
- 选择一个特定的投影,可能是多圆锥投影.
也许spplot不允许用户指定投影.我没有在spplot帮助页面上看到选择投影的选项.我知道如何使用maps包中的map函数选择投影,但是这些地图看起来并不好看,我也无法使用该函数绘制所需的省/地区和州的子集.
Maybe spplot does not allow users to specify projections. I did not see an option to select a projection on the spplot help page. I know how to select projections with the map function in the maps package but those maps did not appear to look as nice and I could not plot the desired subset of provinces/territories and states with that function either.
我不知道如何开始添加经纬度网格.但是,文件"sp.pdf"的第3.2节似乎解决了这个问题.
I do not know how to begin adding a latitude-longitude grid. However, Section 3.2 of the file 'sp.pdf' seems to address the topic.
下面是我到目前为止提出的代码.我已经载入了我偶然发现的每个与地图相关的数据包,并注释掉了GADM数据(省/地区或州边界除外).
Below is the code I have come up with so far. I have loaded every map-related package I have stumbled upon and commented out GADM data except for provincial/territorial or state boundaries.
不幸的是,到目前为止,我仅能绘制加拿大或美国的地图.
Unfortunately, so far I have only managed to plot maps of Canada or the U.S.
library(maps)
library(mapproj)
library(mapdata)
library(rgeos)
library(maptools)
library(sp)
library(raster)
library(rgdal)
# can0<-getData('GADM', country="CAN", level=0) # Canada
can1<-getData('GADM', country="CAN", level=1) # provinces
# can2<-getData('GADM', country="CAN", level=2) # counties
plot(can1)
spplot(can1, "NAME_1") # colors the provinces and provides
# a color-coded legend for them
can1$NAME_1 # returns names of provinces/territories
# us0 <- getData('GADM', country="USA", level=0)
us1 <- getData('GADM', country="USA", level=1)
# us2 <- getData('GADM', country="USA", level=2)
plot(us1) # state boundaries split at
# the dateline
us1$NAME_1 # returns names of the states + DC
spplot(us1, "ID_1")
spplot(us1, "NAME_1") # color codes states and
# provides their names
#
# Here attempting unsuccessfully to combine U.S. and Canada on one map.
# Attempts at selecting given states or provinces have been unsuccessful.
#
plot(us1,can1)
us.can1 <- rbind(us1,can1)
感谢您的帮助.到目前为止,我在上面的步骤2-4上没有任何进展.也许我要求太多了.也许我应该简单地切换到ArcGIS并尝试使用该软件.
Thanks for any help. So far I have made no progress with Steps 2 - 4 above. Perhaps I am asking for too much. Perhaps I should simply switch to ArcGIS and try that software.
我已经阅读了这篇StackOverflow帖子:
I have read this StackOverflow post:
编辑
我现在借用了"R的应用空间数据分析"的电子副本. (2008),并从该书的网站下载(或找到)相关的R代码和数据:
I have now borrowed an electronic copy of 'Applied Spatial Data Analysis with R' Bevand et al. (2008) and downloaded (or located) associated R code and data from the book's website:
我还在这里找到了一些与GIS相关的R代码:
I also found some nice-looking GIS-related R code here:
https://sites.google.com/site/rodriguezsanchezf/news/usingrasagis
如果并且当我学习如何实现期望的目标时,我将在此处发布解决方案.尽管如果我无法完成R中的目标,我最终可能会转而使用ArcGIS.
If and when I learn how to accomplish the desired objectives I will post solutions here. Although I may eventually move to ArcGIS if I cannot accomplish the objectives in R.
推荐答案
要在同一设备上绘制多个SpatialPolygons
对象,一种方法是先指定要绘制的地理范围,然后再使用plot(..., add=TRUE)
.这样只会将感兴趣的点添加到地图上.
To plot multiple SpatialPolygons
objects on the same device, one approach is to specify the geographic extent you wish to plot first, and then using plot(..., add=TRUE)
. This will add to the map only those points that are of interest.
使用投影(例如,多圆锥投影)进行绘制需要首先使用rgdal
包中的spTransform()
函数,以确保所有图层都在同一投影中.
Plotting using a projection, (e.g. a polyconic projection) requires first using the spTransform()
function in the rgdal
package to make sure all the layers are in the same projection.
## Specify a geographic extent for the map
## by defining the top-left and bottom-right geographic coordinates
mapExtent <- rbind(c(-156, 80), c(-68, 19))
## Specify the required projection using a proj4 string
## Use http://www.spatialreference.org/ to find the required string
## Polyconic for North America
newProj <- CRS("+proj=poly +lat_0=0 +lon_0=-100 +x_0=0
+y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs")
## Project the map extent (first need to specify that it is longlat)
mapExtentPr <- spTransform(SpatialPoints(mapExtent,
proj4string=CRS("+proj=longlat")),
newProj)
## Project other layers
can1Pr <- spTransform(can1, newProj)
us1Pr <- spTransform(us1, newProj)
## Plot each projected layer, beginning with the projected extent
plot(mapExtentPr, pch=NA)
plot(can1Pr, border="white", col="lightgrey", add=TRUE)
plot(us1Pr, border="white", col="lightgrey", add=TRUE)
可以使用相同的方法轻松地向地图添加其他功能,例如突出显示感兴趣的司法管辖区:
Adding other features to the map, such as highlighting jurisdictions of interest, can easily be done using the same approach:
## Highlight provinces and states of interest
theseJurisdictions <- c("British Columbia",
"Yukon",
"Northwest Territories",
"Alberta",
"Montana",
"Alaska")
plot(can1Pr[can1Pr$NAME_1 %in% theseJurisdictions, ], border="white",
col="pink", add=TRUE)
plot(us1Pr[us1Pr$NAME_1 %in% theseJurisdictions, ], border="white",
col="pink", add=TRUE)
这是结果:
我认为使用投影时添加网格线非常复杂,以至于需要另一篇文章.看起来好像@Mark Miller在下面添加了它!
Add grid-lines when a projection is used is sufficiently complex that it requires another post, I think. Looks as if @Mark Miller as added it below!
这篇关于R:创建选定的加拿大省份和美国各州的地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!