R基图多边形地图与指定的z范围



我想生成一组具有一致颜色坡道的地图,我有点卡住了。我知道如何做我想要的光栅,但不为矢量数据。

这是我想要的栅格的行为:

require(raster)
r1=raster(matrix(sample(1:50,16),4,4))
r2=raster(matrix(sample(1:100,16),4,4))
plot(r1,col=colorRampPalette(c("red","white","blue"))(10),zlim=c(0,100))
plot(r2,col=colorRampPalette(c("red","white","blue"))(10),zlim=c(0,100))

我如何用多边形制作类似的地图?

例如:

poly1=rasterToPolygons(r1)
poly2=rasterToPolygons(r2)
# Your previous code...
require(raster)
r1=raster(matrix(sample(1:50,16),4,4))
r2=raster(matrix(sample(1:100,16),4,4))
poly1=rasterToPolygons(r1)
poly2=rasterToPolygons(r2)
#################################################

# Color polygons with specific z range:
# Create a function to generate a continuous color palette to work with 
rbPal <- colorRampPalette(c('red','white','blue'))
# Make color scale for polygons 1 & 2 by cutting the continuous
# palette object (rbPal) into 50 discrete blocks.
# Each of these blocks will be defined by the polygon layer 
# values in a sequence ranging from 1 to 100 
head(poly1); summary(poly1)
poly1$Col <- rbPal(50)[as.numeric(cut(poly1$layer, breaks = c(seq(1, 100, by=2))))]
poly2$Col <- rbPal(50)[as.numeric(cut(poly2$layer, breaks = c(seq(1, 100, by=2))))]
head(poly1); head(poly2)
# Plot 
par(mfrow=c(1,2))
plot(poly1, col=poly1$Col)
plot(poly2, col=poly2$Col)

最新更新