具有指定z范围的R基础图多边形图?

时间:2015-06-02 21:04:42

标签: r vector colors polygon raster

我想生成一组具有一致颜色渐变的地图,并且有点卡住。我知道如何做我想要的栅格,但不是矢量数据。

这是我想要的栅格行为:

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)

1 个答案:

答案 0 :(得分:0)

# 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)