R 绘制颜色编码栅格和多边形的交点

R 绘制颜色编码栅格和多边形的交点,r,map,gis,shapefile,R,Map,Gis,Shapefile,我有一个多边形(科罗拉多州的轮廓)和一个覆盖该多边形的色码经纬度网格。我希望仅在多边形本身内显示颜色编码的网格。我该怎么做 下面是绘制多边形和栅格的代码。如图所示,我已将多边形放置在下图中的网格上,否则您将无法看到多边形 我尝试使用PBSmapping包来获得两个多边形的交点。但是,我只能用一种颜色绘制相交区域,而不是保留一个多边形的原始颜色方案。(网格是多色的。) 我还考虑过尝试让多边形的内部不填充,而多边形外部的区域为白色。也许这是可能的。然后我可以把多边形放在网格上,网格只在多边形内可见。

我有一个多边形(科罗拉多州的轮廓)和一个覆盖该多边形的色码经纬度网格。我希望仅在多边形本身内显示颜色编码的网格。我该怎么做

下面是绘制多边形和栅格的代码。如图所示,我已将多边形放置在下图中的网格上,否则您将无法看到多边形

我尝试使用
PBSmapping
包来获得两个多边形的交点。但是,我只能用一种颜色绘制相交区域,而不是保留一个多边形的原始颜色方案。(网格是多色的。)

我还考虑过尝试让多边形的内部不填充,而多边形外部的区域为白色。也许这是可能的。然后我可以把多边形放在网格上,网格只在多边形内可见。然而,如果我有多个多边形和一个网格,这种方法可能不起作用

谢谢你的建议。以下是迄今为止的代码:

library(rgdal)
library(maptools)
library(RColorBrewer)
library(classInt)
library(raster)

setwd('c:/users/mark w miller/gis_in_R')

## Specify a geographic extent for the map
## by defining the top-left and bottom-right geographic coordinates
mapExtent <- rbind(c(-115, 43), c( -97, 35))

# assign projection
newProj <- CRS("+proj=longlat +datum=NAD83 +no_defs +ellps=GRS80 +towgs84=0,0,0")

## Project the map extent (first need to specify that it is longlat) 
mapExtentPr <- spTransform(SpatialPoints(mapExtent, 
                  proj4string=CRS("+proj=longlat")),
                  newProj)

# get Colorado polygon
us1 <- getData('GADM', country="USA", level=1)
colorado <- us1[(us1$NAME_1 %in% c('Colorado')),]

## Project Colorada layer
coloradoPr  <- spTransform( colorado, newProj) 

# create grid of polygons
grd <- GridTopology(c(-110.5, 35.5), c(1,1), c(11,8))
polys <- as(grd, "SpatialPolygons")

# assign projection to grid
proj4string(polys) = CRS("+proj=longlat +datum=NAD83 +no_defs +ellps=GRS80 +towgs84=0,0,0")

# create fake atttribute data for each grid cell
poly.data = data.frame(f=runif(length(row.names(polys)), 0, 14))
row.names(poly.data) <- paste0('g', 1:length(row.names(polys)))

# convert grid to a SpatialPolygonsDataFrame:
poly.df = SpatialPolygonsDataFrame(polys, poly.data)

# assign colors to grid cells
plotvar <- poly.df$f
nclr    <- 8
plotclr <- brewer.pal(nclr,"BuPu")

colcode <- ifelse((                plotvar <=   2), plotclr[1],
           ifelse((plotvar >   2 & plotvar <=   4), plotclr[2],
           ifelse((plotvar >   4 & plotvar <=   6), plotclr[3],
           ifelse((plotvar >   6 & plotvar <=   8), plotclr[4],
           ifelse((plotvar >   8 & plotvar <=  10), plotclr[5],
           ifelse((plotvar >  10 & plotvar <=  12), plotclr[6],
           ifelse((plotvar >  12 & plotvar <=  14), plotclr[7],
                                                    plotclr[8])))))))

jpeg(filename = "colorado.with.grid2.jpeg")

## Plot each projected layer, beginning with the projected extent
plot(mapExtentPr, pch=NA)
plot(poly.df, , col=colcode, add=TRUE)
plot(coloradoPr , border="white", col="lightgrey", add=TRUE)

dev.off()
库(rgdal)
图书馆(地图工具)
图书馆(RColorBrewer)
图书馆(classInt)
图书馆(光栅)
setwd('c:/users/mark w miller/gis_in_R')
##指定地图的地理范围
##通过定义左上角和右下角的地理坐标

mapExtent以下是我使用
PBSmapping
软件包到目前为止得出的结论:

library(rgdal)
library(maptools)
library(RColorBrewer)
library(classInt)
library(raster)

setwd('c:/users/mark w miller/gis_in_R')

## Specify a geographic extent for the map
my.map.extent <- matrix(c(-113, 46,
                          -113, 33,
                           -93, 33,
                           -93, 46,
                          -113, 46), nrow=5, byrow=TRUE)

my.map.extent.p   = Polygon(my.map.extent)
my.map.extent.ps  = Polygons(list(my.map.extent.p),1)
my.map.extent.sps = SpatialPolygons(list(my.map.extent.ps))
proj4string(my.map.extent.sps) <- CRS("+proj=longlat +datum=NAD83 +no_defs +ellps=GRS80 +towgs84=0,0,0")

# assign projection
newProj <- CRS("+proj=longlat +datum=NAD83 +no_defs +ellps=GRS80 +towgs84=0,0,0")

# get my.states polygon
us1 <- getData('GADM', country="USA", level=1)
my.states <- us1[(us1$NAME_1 %in% c('Wyoming', 'Kansas')),]

## Project state layer
my.statesPr  <- spTransform(my.states, newProj) 

# create grid of polygons
#                      lower left,  cell size, number of cells on x axis and on y axis
grd <- GridTopology(c(-112.5, 33.5), c(1,1), c(20,15))
polys <- as(grd, "SpatialPolygons")

# assign projection to grid
proj4string(polys) = CRS("+proj=longlat +datum=NAD83 +no_defs +ellps=GRS80 +towgs84=0,0,0")

# create fake attribute data for each grid cell
poly.data = data.frame(f=runif(length(row.names(polys)), 0, 14))
row.names(poly.data) <- paste0('g', 1:length(row.names(polys)))

# convert grid to a SpatialPolygonsDataFrame:
poly.df = SpatialPolygonsDataFrame(polys, poly.data)

# assign colors to grid cells
plotvar <- poly.df$f
nclr    <- 8
plotclr <- brewer.pal(nclr,"BuPu")

colcode <- ifelse((                plotvar <=   2), plotclr[1],
           ifelse((plotvar >   2 & plotvar <=   4), plotclr[2],
           ifelse((plotvar >   4 & plotvar <=   6), plotclr[3],
           ifelse((plotvar >   6 & plotvar <=   8), plotclr[4],
           ifelse((plotvar >   8 & plotvar <=  10), plotclr[5],
           ifelse((plotvar >  10 & plotvar <=  12), plotclr[6],
           ifelse((plotvar >  12 & plotvar <=  14), plotclr[7],
                                                    plotclr[8])))))))

library(PBSmapping) 

extent.ps <- combinePolys(SpatialPolygons2PolySet(my.map.extent.sps))
grid.ps   <- combinePolys(SpatialPolygons2PolySet(poly.df))
states.ps <- combinePolys(SpatialPolygons2PolySet(my.statesPr))

diff.grid.states <- combinePolys(joinPolys(extent.ps, states.ps, "DIFF"))

dev.new()

jpeg(filename = "my.states.with.grid.jpeg")

plotPolys(extent.ps, proj = TRUE, xlab="Easting", ylab="Northing")
plot(poly.df, col=colcode, lwd = 0.5, add=TRUE)
addPolys(diff.grid.states, col="white")

dev.off()
库(rgdal)
图书馆(地图工具)
图书馆(RColorBrewer)
图书馆(classInt)
图书馆(光栅)
setwd('c:/users/mark w miller/gis_in_R')
##指定地图的地理范围
我的地图