R 用空间多边形在形状文件上绘制彩色网格

R 用空间多边形在形状文件上绘制彩色网格,r,map,plot,geospatial,shapefile,R,Map,Plot,Geospatial,Shapefile,我试图在底图上绘制一个彩色的属性网格。这里也有类似的问题,包括我最近问自己的一个问题,但早期的解决方案通常涉及ggplot2。我希望在不使用ggplot2的情况下找到一个理想的解决方案,因为我的最终实际地图将非常复杂,而且到目前为止,我使用plot已经取得了实质性的进展 下面是绘制Colorado并创建虚假属性数据网格的代码。我可以使用SpatialPoints将栅格转换为空间点,并绘制这些点。但是,我怀疑我需要转换为空间多边形,可能需要使用SpatialPolygons。当我尝试使用空间多边形

我试图在底图上绘制一个彩色的属性网格。这里也有类似的问题,包括我最近问自己的一个问题,但早期的解决方案通常涉及
ggplot2
。我希望在不使用
ggplot2
的情况下找到一个理想的解决方案,因为我的最终实际地图将非常复杂,而且到目前为止,我使用
plot
已经取得了实质性的进展

下面是绘制Colorado并创建虚假属性数据网格的代码。我可以使用
SpatialPoints
将栅格转换为空间点,并绘制这些点。但是,我怀疑我需要转换为空间多边形,可能需要使用
SpatialPolygons
。当我尝试使用
空间多边形时
会得到如下所示的错误

一旦将网格单元层添加到地图中,我希望裁剪网格,使其仅显示在科罗拉多州内(例如,如果我将另一个州添加到地图并使用更大的属性网格,则显示在科罗拉多州和怀俄明州内)。然而,这可能是一个后续问题

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

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

# create grid of polygons

grd <- GridTopology(c(1,1), c(1,1), c(10,10))
polys <- as(grd, "SpatialPolygons")

row.names(polys)

#  [1] "g1"   "g2"   "g3"   "g4"   "g5"   "g6"   "g7"   "g8"   "g9"   "g10"  "g11"  "g12"  "g13"  "g14"  "g15"  "g16"  "g17"  "g18"  "g19" 
# [20] "g20"  "g21"  "g22"  "g23"  "g24"  "g25"  "g26"  "g27"  "g28"  "g29"  "g30"  "g31"  "g32"  "g33"  "g34"  "g35"  "g36"  "g37"  "g38" 
# [39] "g39"  "g40"  "g41"  "g42"  "g43"  "g44"  "g45"  "g46"  "g47"  "g48"  "g49"  "g50"  "g51"  "g52"  "g53"  "g54"  "g55"  "g56"  "g57" 
# [58] "g58"  "g59"  "g60"  "g61"  "g62"  "g63"  "g64"  "g65"  "g66"  "g67"  "g68"  "g69"  "g70"  "g71"  "g72"  "g73"  "g74"  "g75"  "g76" 
# [77] "g77"  "g78"  "g79"  "g80"  "g81"  "g82"  "g83"  "g84"  "g85"  "g86"  "g87"  "g88"  "g89"  "g90"  "g91"  "g92"  "g93"  "g94"  "g95" 
# [96] "g96"  "g97"  "g98"  "g99"  "g100"

length(row.names(polys))

# [1] 100

class(polys)

# [1] "SpatialPolygons"
# attr(,"package")
# [1] "sp"

# 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 = "grid.plot.jpeg")

plot(poly.df, , col=colcode)
text(coordinates(poly.df), labels=row.names(poly.df))
dev.off()
以下是前面的一个问题,其答案使用
绘图
,但要求从海报中获取更多信息:

下面是我之前提出的一个类似问题,它只有一个ggplot2解决方案:

我下面的方法是基于我前面的另一个问题的解决方案:

谢谢你的帮助

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

#                  NW(long,lat)  SE(long,lat)
 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 map extent
mapExtentPr <- spTransform(SpatialPoints(mapExtent, 
                  proj4string=CRS("+proj=longlat")), newProj)

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

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

# Create grid cells containing fake attribute data 
# using approach found here: 
# http://www.numbertheory.nl/2011/11/08/drawing-polar-centered-spatial-maps-using-ggplot2/

set.seed(1234)

xlim = c(-113, -99)
ylim = c(  35,  45)

dat.grid = expand.grid(x = xlim[1]:xlim[2], y = ylim[1]:ylim[2])
dat.grid$z = runif(nrow(dat.grid))

## Project grid layer
dat.gridPr  <- spTransform(SpatialPoints(  dat.grid, proj4string=CRS("+proj=longlat")), newProj)
dat.gridPr2 <- spTransform(SpatialPolygons(dat.grid, proj4string=CRS("+proj=longlat")), newProj)

#Error in spTransform(SpatialPolygons(dat.grid, proj4string = CRS("+proj=longlat")),  : 
#  error in evaluating the argument 'x' in selecting a method for function 'spTransform': Error in SpatialPolygons(dat.grid, proj4string = CRS("+proj=longlat")) : 
#  cannot get a slot ("area") from an object of type "integer"

## Plot each projected layer, beginning with the projected extent
plot(mapExtentPr, pch=NA)
plot(coloradoPr , border="white", col="lightgrey", add=TRUE)
plot(dat.gridPr , border="white", col="lightgrey", add=TRUE)
plot(dat.gridPr , fill=dat.grid$z, add=TRUE)
plot(dat.gridPr , fill=dat.gridPr$z, add=TRUE)
库(rgdal)
图书馆(地图工具)
图书馆(GG2)
图书馆(plyr)
图书馆(RColorBrewer)
图书馆(classInt)
图书馆(光栅)
#西北(长,纬度)东南(长,纬度)

mapExtent下面是使用
sp
包创建彩色编码网格的代码。我没有包含创建科罗拉多地图的代码,因为上面已经给出了该代码。此外,我还没有尝试裁剪网格以填充科罗拉多州。这可能是另一个问题

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

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

# create grid of polygons

grd <- GridTopology(c(1,1), c(1,1), c(10,10))
polys <- as(grd, "SpatialPolygons")

row.names(polys)

#  [1] "g1"   "g2"   "g3"   "g4"   "g5"   "g6"   "g7"   "g8"   "g9"   "g10"  "g11"  "g12"  "g13"  "g14"  "g15"  "g16"  "g17"  "g18"  "g19" 
# [20] "g20"  "g21"  "g22"  "g23"  "g24"  "g25"  "g26"  "g27"  "g28"  "g29"  "g30"  "g31"  "g32"  "g33"  "g34"  "g35"  "g36"  "g37"  "g38" 
# [39] "g39"  "g40"  "g41"  "g42"  "g43"  "g44"  "g45"  "g46"  "g47"  "g48"  "g49"  "g50"  "g51"  "g52"  "g53"  "g54"  "g55"  "g56"  "g57" 
# [58] "g58"  "g59"  "g60"  "g61"  "g62"  "g63"  "g64"  "g65"  "g66"  "g67"  "g68"  "g69"  "g70"  "g71"  "g72"  "g73"  "g74"  "g75"  "g76" 
# [77] "g77"  "g78"  "g79"  "g80"  "g81"  "g82"  "g83"  "g84"  "g85"  "g86"  "g87"  "g88"  "g89"  "g90"  "g91"  "g92"  "g93"  "g94"  "g95" 
# [96] "g96"  "g97"  "g98"  "g99"  "g100"

length(row.names(polys))

# [1] 100

class(polys)

# [1] "SpatialPolygons"
# attr(,"package")
# [1] "sp"

# 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 = "grid.plot.jpeg")

plot(poly.df, , col=colcode)
text(coordinates(poly.df), labels=row.names(poly.df))
dev.off()
库(rgdal)
图书馆(地图工具)
图书馆(GG2)
图书馆(plyr)
图书馆(RColorBrewer)
图书馆(classInt)
图书馆(光栅)
setwd('c:/users/mark w miller/gis_in_R')
#创建多边形网格
grd