2015-06-17 50 views
1

我试图使用sp程序包中的函数spplot绘制多边形数据,但我的数据框中存在一些缺失值(NA)。当我绘制这个数据框时,缺失值具有透明颜色。我想用黑色绘制它们。我怎样才能做到这一点?设置颜色为NA值,并在s中绘制R

library(sp) 
spplot(TestData,12) 

这是我实现这一目标是使用latticeExtra::layer_绘制多边形的所有用你喜欢的颜色NA,标准spplotTestData object

回答

1

的一种方式。

library(latticeExtra) 
spplot(TestData, 12, col.regions=heat.colors(101), at=seq(0, 4, length=100)) + 
    layer_(sp.polygons(TestData, fill='black')) 

enter image description here

同样,如果您有栅格数据,你可以做同样的layer_grid.rect

library(raster) 
library(latticeExtra) 
r <- raster(matrix(runif(100), ncol=10)) 
r[sample(100, 10)] <- NA 

spplot(r, col.regions=grey.colors, at=seq(0, 1, length=100)) + 
    layer_(grid.rect(0, 0, 2, 2, gp=gpar(fill = 'lightblue'))) 

enter image description here