## ---- echo=FALSE, include=FALSE----------------------------------------------- library(knitr) opts_chunk$set(fig.width = 5, fig.height = 5, fig.cap = '', collapse = TRUE) ## ----------------------------------------------------------------------------- longitude <- c(-116.7, -120.4, -116.7, -113.5, -115.5, -120.8, -119.5, -113.7, -113.7, -110.7) latitude <- c(45.3, 42.6, 38.9, 42.1, 35.7, 38.9, 36.2, 39, 41.6, 36.9) lonlat <- cbind(longitude, latitude) ## ----------------------------------------------------------------------------- library(sp) pts <- SpatialPoints(lonlat) ## ----------------------------------------------------------------------------- class (pts) ## ----------------------------------------------------------------------------- showDefault(pts) ## ----------------------------------------------------------------------------- crdref <- CRS('+proj=longlat +datum=WGS84') pts <- SpatialPoints(lonlat, proj4string=crdref) ## ----------------------------------------------------------------------------- library(raster) pts ## ----------------------------------------------------------------------------- # Generate random precipitation values, same quantity as points precipvalue <- runif(nrow(lonlat), min=0, max=100) df <- data.frame(ID=1:nrow(lonlat), precip=precipvalue) ## ----------------------------------------------------------------------------- ptsdf <- SpatialPointsDataFrame(pts, data=df) ptsdf ## ----------------------------------------------------------------------------- str(ptsdf) ## ----------------------------------------------------------------------------- showDefault(ptsdf) ## ----------------------------------------------------------------------------- lon <- c(-116.8, -114.2, -112.9, -111.9, -114.2, -115.4, -117.7) lat <- c(41.3, 42.9, 42.4, 39.8, 37.6, 38.3, 37.6) lonlat <- cbind(lon, lat) lns <- spLines(lonlat, crs=crdref) lns ## ----------------------------------------------------------------------------- pols <- spPolygons(lonlat, crs=crdref) pols ## ----------------------------------------------------------------------------- str(pols) ## ---- vectordata-1------------------------------------------------------------ plot(pols, axes=TRUE, las=1) plot(pols, border='blue', col='yellow', lwd=3, add=TRUE) points(pts, col='red', pch=20, cex=3)