使用SpatialPolygons绘制shapefile上的彩色网格

时间:2014-03-11 22:05:41

标签: r map plot geospatial shapefile

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

下面是绘制科罗拉多州并创建假属性数据网格的代码。我可以使用SpatialPoints将网格转换为空间点并绘制这些点。但是,我怀疑我需要转换为空间多边形,可能使用SpatialPolygons。当我尝试使用SpatialPolygons时,我获得了下面显示的错误。

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

以下是使用plot的答案的早期问题,但要求海报提供其他信息:R Plot Filled Longitude-Latitude Grid Cells on Map

以前是一个类似于我自己的类似问题,它只有一个ggplot2解决方案:color grid cells in the United States and Canada

下面的方法基于我之前提出的另一个问题的解决方案:R: creating a map of selected Canadian provinces and U.S. states

感谢您的帮助。

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)

修改

这篇文章描述了如何一次添加一个网格单元格,如果需要,我可以这样做:

https://gis.stackexchange.com/questions/18311/instantiating-spatial-polygon-without-using-a-shape-file-in-r

1 个答案:

答案 0 :(得分:0)

以下是使用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()

enter image description here