maps - Clipping rasters in R -
i building map northeastern u.s.a. map background needs either altitude map or mean annual temperature map. have 2 rasters worldclim.org give me these variables need clip them extent of states interested in. suggestions on how this. have far:
#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 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 main map map.scale(x=-73,y=38, relwidth=0.15, metric=t, ratio=f) #create inset map # next, create new graphics space in lower-right hand corner. numbers proportional distances within graphics window (xmin,xmax,ymin,ymax) on scale of 0 1. # "plt" key parameter adjust par(plt = c(0.1, 0.53, 0.57, 0.90), new = true) # think key command http://www.stat.auckland.ac.nz/~paul/rgraphics/examples-map.r plot.window(xlim=c(-127, -66),ylim=c(23,53)) # fill box white polygon(c(0,360,360,0),c(0,0,90,90),col="white") # draw map map(database="state", interior=t, add=true, fill=false) map(database="state", regions=nestates, interior=true, add=true, fill=true, col="grey") the elevation , meantemp objects ones need clipped area extent of nestates object. input
the function crop in raster package allows use extent object or object extent can calculated cut(subset) object. package example:
r <- raster(nrow=45, ncol=90) r[] <- 1:ncell(r) e <- extent(-160, 10, 30, 60) rc <- crop(r, e) if wanted cut in more detailed manner maybe use shp of states , on function in sp package.
Comments
Post a Comment