1

I am trying to work with discrete data on land use in Australia. I load up the file as follows:

library(raster)

dat<-raster("C:\\Users\\Douglas\\Desktop\\GIS Project\\LandUse\\Land use of Australia 2005-2006 - National Map - ESRI GRID - integer - Geographics\\luav4g9abll07811a02egigeo___\\lu05v4ag\\hdr.adf")

Here is more information about the raster file:

class       : RasterLayer 
dimensions  : 3401, 4150, 14114150  (nrow, ncol, ncell)
resolution  : 0.01, 0.01  (x, y)
extent      : 112.505, 154.005, -44.005, -9.995  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs 
data source : C:\Users\Douglas\Desktop\GIS Project\LandUse\Land use of Australia 2005-2006 - National Map - ESRI GRID - integer - Geographics\luav4g9abll07811a02egigeo___\lu05v4ag\hdr.adf 
names       : hdr 
values      : 1, 2329  (min, max)
attributes  :
         ID COUNT COMMODITIES                 COMMODITIES_DESC IRRIGATION                         IRRIGATION_DESC LU_CODE
 from:    1  3199          -1 Non-agricultural land or no data          0 Dryland agriculture, not ag. or no data     125
 to  : 2329     1          -1 Non-agricultural land or no data          0 Dryland agriculture, not ag. or no data     114
                               LU_DESC                    LU_DESC2                    LU_DESC3 T.CODE
 CONSERVATION AND NATURAL ENVIRONMENTS Managed resource protection Traditional indigenous uses  1.2.5
 CONSERVATION AND NATURAL ENVIRONMENTS         Nature conservation  Natural feature protection  1.1.4

and when I simply plot this map, it seems that R does not see the values as discrete, but rather continuous values.

plot(dat,"T.CODE")

Land use map of Australia

How to get R to recognize the values as discrete and not continuous?

  • Deleting questions is a perilous game. The SE framework can identify this form of abuse and defend itself. In the future, Edit the question instead of creating a new one and deleting the old. – Vince Jun 21 '18 at 11:14
  • Okay, did not know that. I just figured it would be easier to delete rather than amend. Will not do it again. – Douglas Jun 21 '18 at 11:17
2

You can use levelplot from the rasterVis package to plot with categorical labels:

> r = raster(matrix(sample(1:3,25,TRUE),5,5))
> levels(r)=data.frame(ID=1:3, code=LETTERS[1:3])
> rasterVis::levelplot(r)

enter image description here

R uses the presence of a "RAT" (Raster Attribute Data) to define a raster as categorical - see help(ratify) for functions around this (my call of levels(r) in my example is calling raster::levels as documented in help(ratify).

1

I am not sure that the raster package really makes the difference between discrete and continuous data. It is all just data. What I think is the issue here is the selection of continuous legend. The raster package provides only relatively simple function for plotting which comes with preselected style of legend (the one in your image).

I would suggest that you take a look at tmap package - available from CRAN. It provides almost endless possibilities for plotting spatial data (rasters are just one part). If you take a look at this example in the vignette, you will find an example containing tm_raster function, which is used to style rasters. The example in the text does almost exactly what you want to do.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.