且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用R中的ggplot2绘制分箱类中的连续栅格数据

更新时间:2022-10-26 09:24:48

You should use the raster package to work with raster data. This package provides several function to work with categorical rasters. For example, with reclassify you can convert a continuous file into a discrete raster. The next example is adapted from this question:

library(raster)

f <- system.file("external/test.grd", package="raster")
r <- raster(f)
r <- reclassify(r, c(0, 500, 1,
                     500, 2000, 2))

On the other hand, if you want to use the ggplot2 functions, the rasterVis package provides a simple wrapper around ggplot that works with RasterLayer objects:

library(rasterVis)

gplot(r) +
    geom_raster(aes(fill = factor(value))) +
    coord_equal()

to define your own colors you can add then:

scale_fill_manual(values=c('red','green')))