【问题标题】:R base plot polygon map with specified z range?具有指定z范围的R基础图多边形图?
【发布时间】:2015-08-16 20:44:34
【问题描述】:

我想生成一组颜色渐变一致的地图,但我有点卡住了。我知道如何为栅格做我想做的事,但对矢量数据却不知道。

这是我想要的栅格行为:

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)

【问题讨论】:

  • 我刚想出的一个选项是plot(poly2,col=(colorRampPalette(c("red","white","blue"))(10))[round(poly2$layer/10)]),但感觉就像是一团糟,所以我想知道是否有更简单的方法使用相同的工具。

标签: r vector colors polygon raster


【解决方案1】:
# 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-30
    • 2020-09-01
    • 1970-01-01
    相关资源
    最近更新 更多