I created a raster object (package raster in R) using the function rasterize starting from a shapefile of polygons. I passed as field a shapefile attribute (CODE) containing 37 landuse categories.

This is what I did in code:

myrast <- rasterize(x = shapefile, y = another_raster, field = shapefile$CODE) 

The command converts the categorical variable into numbers from 1 to 37 and correctly create the raster.

Now I want to export the raster to see it in a GIS but I would like that a legend would be shown with the CODE and not with the number from 1 to 37.

How can I do this?

  • In what GIS? Raster values are always numerical. For representing attribute data, it is common to have a standalone file (eg., dbf) containing the attributes with the raster values as the linking value. You can do this by simply writing a csv flat file. Aug 20 '20 at 18:03
  • In QGIS. So when I write a GTiff with R how can I have the relative legend in QGIS?
    – user36756
    Aug 20 '20 at 18:58
  • You can use rasterVis::levelplot() as, e.g., here. Aug 21 '20 at 16:30
  • Thank you for this hint, this is good for doing what I want in R, but for the export in QGIS?
    – user36756
    Aug 24 '20 at 7:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.