2015-06-02 50 views
0

我想生成一組具有一致顏色漸變的地圖,並且有點卡住了。我知道如何做我想要的柵格,但不是矢量數據。具有指定z範圍的R底圖多邊形地圖?

這是我想要的柵格的行爲:

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) 
+0

一個選項是'圖(POLY2,列=(colorRampPalette(C( 「紅」, 「白」, 「藍」))( 10))[round(poly2 $ layer/10)])'但它感覺像一個kluge,所以我想知道是否有更簡單的方法使用相同的工具。 – user1521655

回答

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)