How can I create raster plots with the same colour scale in R

后端 未结 6 903
盖世英雄少女心
盖世英雄少女心 2021-01-31 10:36

I\'m creating some maps from raster files using the \"raster\" package in R. I\'d like to create comparison rasters, showing several maps side by side. It\'s important for this

6条回答
  •  轮回少年
    2021-01-31 11:12

    There is more work to be done here in 'raster' but here is a hack:

     library(raster)
     r1 <- r2 <- r3 <- raster(ncol=10, nrow=10)
     r1[] <- runif(ncell(r1))
     r2[] <- runif(ncell(r2)) / 2
     r3[] <- runif(ncell(r3)) * 1.5
     r3 <- min(r3, 1)
     s <- stack(r1, r2, r3)
    
    
     brk <- c(0, 0.25, 0.5, 0.75, 1)
     par(mfrow=c(1,3))
     plot(r1, breaks=brk, col=rainbow(4), legend=F)
     plot(r1, breaks=brk, col=rainbow(4), legend.only=T, box=F)
     plot(r2, breaks=brk, col=rainbow(4), legend=F)
     plot(r1, breaks=brk, col=rainbow(4), legend.only=T, box=F)
     plot(r3, breaks=brk, col=rainbow(4), legend=F)
     plot(r1, breaks=brk, col=rainbow(4), legend.only=T, box=F)
    

    You can also use the spplot function (sp package)

     s <- stack(r1, r2, r3) 
     sp <- as(s, 'SpatialGridDataFrame')
     spplot(sp)
    

    You can also send the values to ggplot (search the r-sig-geo archives for examples) If your RasterLayer links to a very large file, you might first do, before going to ggplot

    r <- sampleRegular(r, size=100000, asRaster=TRUE) 
    

    and then perhaps

    m <- as.matrix(r)
    

提交回复
热议问题