2013-05-20 72 views
1

我正在制作美国东北部的地图 地图背景需要是高度图或年平均温度图。我有两个来自Worldclim.org的栅格,它们给了我这些变量,但我需要将它们限制在我感兴趣的状态范围内。关于如何执行此操作的任何建议。 这是我到目前为止有:在R中修剪光栅

#load libraries 
library (sp) 
library (rgdal) 
library (raster) 
library (maps) 
library (mapproj) 


#load data 
state<- data (stateMapEnv) 
elevation<-raster("alt.bil") 
meantemp<-raster ("bio_1.asc") 

#build the raw map 
nestates<- c("maine", "vermont", "massachusetts", "new hampshire" ,"connecticut", 
    "rhode island","new york","pennsylvania", "new jersey", 
    "maryland", "delaware", "virginia", "west virginia") 

map(database="state", regions = nestates, interior=T, lwd=2) 
map.axes() 

#add site localities 
sites<-read.csv("sites.csv", header=T) 
lat<-sites$Latitude 
lon<-sites$Longitude 

map(database="state", regions = nestates, interior=T, lwd=2) 
points (x=lon, y=lat, pch=17, cex=1.5, col="black") 
map.axes() 
library(maps)                 #Add axes to main map 
map.scale(x=-73,y=38, relwidth=0.15, metric=T, ratio=F) 

#create an inset map 

# Next, we create a new graphics space in the lower-right hand corner. The numbers are proportional distances within the graphics window (xmin,xmax,ymin,ymax) on a scale of 0 to 1. 
    # "plt" is the key parameter to adjust 
    par(plt = c(0.1, 0.53, 0.57, 0.90), new = TRUE) 

    # I think this is the key command from http://www.stat.auckland.ac.nz/~paul/RGraphics/examples-map.R 
    plot.window(xlim=c(-127, -66),ylim=c(23,53)) 

    # fill the box with white 
    polygon(c(0,360,360,0),c(0,0,90,90),col="white") 

    # draw the map 
    map(database="state", interior=T, add=TRUE, fill=FALSE) 
    map(database="state", regions=nestates, interior=TRUE, add=TRUE, fill=TRUE, col="grey") 

elevationmeantemp对象是需要被夹在nestates对象的区域范围内的人。任何输入都将有所帮助

+1

看看'over'或MapGAM'包。一个会帮助你自己建立它。另一个已经具有这样的功能,您可能能够进行逆向工程。 –

+0

有没有办法使用'地图'填写栅格?甚至在光栅中使用叠加功能? –

+0

@mnel:你有没有解决这个问题的任何意见? –

回答

3

栅格包中的函数crop允许您使用Extent对象或可计算Extent的对象来剪切(子集)另一个对象。该软件包例如:

r <- raster(nrow=45, ncol=90) 
r[] <- 1:ncell(r) 
e <- extent(-160, 10, 30, 60) 
rc <- crop(r, e) 

如果你想在一个更详细的方式削减,也许你可以使用状态的小水电和切换功能在SP包。