使用R中的晶格水平图的地理热图

时间:2014-09-24 14:56:28

标签: r map heatmap lattice

我正在尝试构建一个地理空间连续热图,它不受使用晶格的县或人工边界的限制,非常类似于Katz的Dialect Maps,与Choropleth Challenge略有不同。

到目前为止,我已经非常接近了,但我一直试图弄清楚如何避免在地理区域之外的区域显示热图颜色。请参阅以下示例:

library(maps)
library(lattice)

# get region border coordinates for the contiguous USA
m <- map("usa")

# make a grid of latitude and longitude, and supply z-values
lons <- seq(min(m$x, na.rm=T), max(m$x, na.rm=T), length.out=30)
lats <- seq(min(m$y, na.rm=T), max(m$y, na.rm=T), length.out=30)
pts <- expand.grid(lons, lats)
names(pts) <- c("lon", "lat")
pts$z <- sin(pts$lat*pi/180) + cos(pts$lon*pi/180)

## (A) eliminate z-values outside of the USA region
# pts$z[ lat & lon outside of region ] <- NA # don't know how to do this
# this could work, but would leave jagged edges around the region border

levelplot(z~lon*lat, pts, aspect="xy",
           panel = function(...){
             panel.levelplot(...) 
             panel.xyplot(m$x, m$y, type="l", col="black") # adds USA border
             # (B) fill the area outside region with white 
             # panel.something() # not sure what to use here
          })

方法(B)可能效果很好,但我没有看到一种简单的方法。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

我不确定如何使用maplattice完成您的要求,但是我会使用rasterrgeos工具来解决您的问题:

library(raster)
library(rgeos)

## get SpatialPolygnsDataFrame map of the states
m <- getData("GADM", country="United States", level=1)
m <- m[!m$NAME_1 %in% c("Alaska","Hawaii"),] # sorry Alaska and Hawaii 

## here I modified your code to make a raster object
r <- raster(nrow=30, ncol=30, 
            xmn=bbox(m)["x","min"], xmx=bbox(m)["x","max"],
            ymn=bbox(m)["y","min"], ymx=bbox(m)["y","max"],
            crs=proj4string(m))
xyz <- rasterToPoints(r)
r[] <- sin(xyz[,"y"]*pi/180) + cos(xyz[,"x"]*pi/180)

## Option A) mask raster using polygon
newr <- mask(r, m)
plot(newr, col=cm.colors(60), axes=FALSE)
plot(m, add=TRUE)
box(col="white")
## leaves jagged edges...

enter image description here

## Option B) cover the outside area
b <- gUnaryUnion(rasterToPolygons(r)) # first create a polygon that covers the raster
b <- gDifference(b, m) # then get the difference between the polygons
plot(r, col=cm.colors(100), axes=FALSE)
plot(b, add=TRUE, col="white", border="white")
plot(m, add=TRUE)
box(col="white")

enter image description here

答案 1 :(得分:0)

您可以将'pts'和'm'分别转换为类Raster*Spatial*的对象,然后将它们用作spplot的输入(这是levelplot的包装{1}},请参阅?raster::spplot)。这样您就可以准确丢弃那些不应显示的像素(通过mask),同时确保与其他网格(或网格>的兼容性)基于业务。这是一些示例代码。

## transform 'map' object to 'SpatialPolygons'
library(maptools)
m <- map2SpatialPolygons(m, IDs = seq(m$names), 
                         proj4string = CRS("+init=epsg:4326"))

## rasterize pts and mask pixels outside map region
library(raster)
coordinates(pts) <- ~ lon + lat
proj4string(pts) <- "+init=epsg:4326"
pts <- as(pts, "SpatialPixelsDataFrame")
rst <- raster(pts)
rst <- mask(rst, m)

## display data
library(latticeExtra)
spplot(rst, scales = list(draw = TRUE), alpha.regions = .8) + 
  layer(sp.polygons(m, lwd = 2))

spplot

为了适当地解决边缘效应,我建议你坚持@PaulRegular提供的很好的解决方法。

相关问题