在R中映射特定的州和省 [英] Mapping specific States and Provinces in R

查看:98
本文介绍了在R中映射特定的州和省的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用R为FieldBio项目生成一个带有示例位置的图形.到目前为止,我已经能够绘制我想要的美国州,绘制加拿大(国界)地图,以覆盖这两张地图;不过,我想做的是具体映射以下州/省,但不包括其他州/省: 加利福尼亚州 内华达州 犹他州 科罗拉多州 怀俄明州 蒙大拿 爱达荷州 华盛顿州 俄勒冈州 不列颠哥伦比亚省 艾伯塔省

I'm trying to use R to generate a figure with sample localities for my FieldBio project. So far, I've been able to map the US states I want, to map Canada (national border), to overlay these two maps; what I'd like to do, though, is to map the following states/provinces specifically, excluding the others: California Nevada Utah Colorado Wyoming Montana Idaho Washington Oregon British Columbia Alberta

这是我到目前为止一直在使用的代码:

Here is the code I've been using so far:

>map('state', region = c('california', 'nevada', 'utah', 'colorado', 'wyoming', 'montana', 'idaho', 'oregon', 'washington'), xlim=c(-130,-90), ylim=c(30,60), fill=TRUE, col="gray95")

>map("worldHires","Canada", xlim=c(-130,-90), ylim=c(30,60), col="gray95", fill=TRUE, add=TRUE)

这会生成一张具有所需州的地图,但将加拿大作为一个国家(在我设置的定界符处截断).

This produces a map with the desired states, but canada as a country (cut off at the delimitors I set).

有办法只做我想要的省吗?我想我知道如何在此之后绘制点(我将它们作为具有.lat和长数据的.csv)绘制出来.

Is there a way to do only the provinces I want? I think I know how to plot the points (I have them as a .csv with lat and long data) on top of this afterward.

我意识到( R:正在创建选定的加拿大省份和美国各州的地图)非常相似,但是对于该特定示例,我在代码中有些迷茫,因此无需突出显示任何特定内容.

I realize that (R: creating a map of selected Canadian provinces and U.S. states) is pretty similar, but I'm getting a little lost in the code for that particular example, and I don't need to highlight anything specific.

谢谢

推荐答案

喜欢吗?

library(raster)
states    <- c('California', 'Nevada', 'Utah', 'Colorado', 'Wyoming', 'Montana', 'Idaho', 'Oregon', 'Washington')
provinces <- c("British Columbia", "Alberta")

us <- getData("GADM",country="USA",level=1)
canada <- getData("GADM",country="CAN",level=1)

us.states <- us[us$NAME_1 %in% states,]
ca.provinces <- canada[canada$NAME_1 %in% provinces,]

us.bbox <- bbox(us.states)
ca.bbox <- bbox(ca.provinces)
xlim <- c(min(us.bbox[1,1],ca.bbox[1,1]),max(us.bbox[1,2],ca.bbox[1,2]))
ylim <- c(min(us.bbox[2,1],ca.bbox[2,1]),max(us.bbox[2,2],ca.bbox[2,2]))
plot(us.states, xlim=xlim, ylim=ylim)
plot(ca.provinces, xlim=xlim, ylim=ylim, add=T)

因此,这使用包raster中的getData(...)函数从GADM站点获取美国各州和加拿大各省的SpatialPolygonDataFrame.然后,它仅提取所需的状态(请注意,相关的属性表字段为NAME_1,并且状态/省份需要正确地大写).然后,我们从两个(子集)地图的边界框中计算x和y极限.最后,我们渲染地图.请注意在第二个plot(...)调用中使用add=T.

So this uses the getData(...) function in package raster to grab the SpatialPolygonDataFrames for US states and Canadian provinces from the GADM site. Then it extracts only the states you want (notice that the relevant attribute table field is NAME_1 and the the states/provinces need to be capitalized properly). Then we calculate the x and y-limits from the bounding boxes of the two (subsetted) maps. Finally we render the maps. Note the use of add=T in the second call to plot(...).

这是ggplot解决方案.

library(ggplot2)
ggplot(us.states,aes(x=long,y=lat,group=group))+
  geom_path()+
  geom_path(data=ca.provinces)+
  coord_map()

此处的优点是ggplot为您管理图层,因此您不必显式计算xlimylim.此外,IMO在添加其他层方面具有更大的灵活性.缺点是,使用诸如此类的高分辨率地图(尤其是加拿大西海岸),它的速度要慢得多.

The advantage here is that ggplot manages the layers for you, so you don't have to explicitly calculate xlim and ylim. Also, IMO there is much more flexibility in adding additional layers. The disadvantage is that, with high resolution maps such as these (esp the west coast of Canada), it is much slower.

这篇关于在R中映射特定的州和省的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆