简体   繁体   中英

How to transfer specific colors to a raster in r

I wonder if it is possible to assign specific colors to the cells of a raster in r.

I know that “rasterize” in the package "raster" allows transferring values to a raster given the coordinates of the given value. However, I wonder if is possible to transfer just a color to the specific position of a cell?

In short, I have a given raster and an external database with three columns (x, y, and color). I would like to transfer the color in the third column to the cell in the raster at the xy position.

This is the code that I have:

library(raster)

BaseRaster<-raster(xmn=1, xmx=4, ymn=1, ymx=4,resolution=1) #create an empty raster

Database<-data.frame(Xcols=c(1.5, 2.5,3.5,   1.5, 2.5,3.5,  1.5, 2.5,3.5) ,
Ycols= c(1.5, 1.5,1.5,   2.5, 2.5,2.5,  3.5, 3.5,3.5),
Cols=c("#FFF423","#FFFF14","#FFE20F","#FFF80A","#FFCB07","#FFE100","#FFEE0B","#FDEE0A","#FFE209"))

The question is how do I transfer the colors in the third column of the database to the raster, given the coordinates provided in columns 1 and 2 in the database? Unfortunately, rasterize does not do the job

ColorRaster= rasterize(c(Database$Xcols, Database$Ycols), BaseRaster, Database $Cols)

Assuming BaseRaster is your raster object and database has the color the following should work:

plot(BaseRaster, col=colorRampPalette(database$color))(255)

for more information refer plot{raster}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM