[R-sig-Geo] How can I plot categorical raster (using levelplot?) with a proper legend?

Oscar Perpiñán Lamigueiro oscar.perpinan at gmail.com
Wed May 1 01:20:19 CEST 2013


Hello,

The levelplot method in the current version of rasterVis is not able to
work with RATs with more than one variable. I have just uploaded to
R-Forge a fix to solve this problem. Besides, levelplot has gained a new
argument (att) to choose which variable you need. Maybe tomorrow this
new version will be built and available to install. Meanwhile you can
build it on your own or use a RAT with only one attribute (for example,
drop the column "class").

Best,

Oscar.

Mikhail Titov <mlt at gmx.us> writes:

> Hi, all!
>
> I have a raster exported from ArcMap that I'm trying to plot (for now)
> with raster/rasterVis. But the problem is that levelplot fails to plot
> raster as is. When I deratify it first, it plots, but the legend is
> incorrect :( The manual for rasterVis says to use ratify for categorical
> data, but calling ratify makes no difference for me.
>
> I feel like I'm missing something... Here is the code I was using.
>
> ,----[ loading data ]
> | library(raster)
> | library(rasterVis)
> | library(foreign)
> | fname <- "too-big-to-attach.img"
> | benefits.rast <- raster(fname)
> | rat <- read.dbf(paste(fname, ".vat.dbf", sep=""))
> | names(rat)[1] <- "ID"                   # rename Value to ID
> | levels(benefits.rast) <- rat
> | levels(benefits.rast)
> `----
>
> This yields in my case
>
> ,----[ levels(benefits.rast) ]
> | [[1]]
> |   ID Count class
> | 1  0  3616     D
> | 2  1  2476     R
> | 3  2 11584     C
> | 4  3   305    NB
> | 5  4    60    D-
> | 6  5  1912     B
> | 7  6    57    SA
> | 8  7   151     A
> `----
>
> At this point if I call levelplot(benefits.rast), nothing is being
> plotted and I get the following error.
>
> ,----[ Error from levelplot(benefits.rast) ]
> | Error in from:to : result would be too long a vector
> | In addition: Warning messages:
> | 1: In min(x) : no non-missing arguments to min; returning Inf
> | 2: In max(x) : no non-missing arguments to max; returning -Inf
> | 3: In min(x) : no non-missing arguments to min; returning Inf
> | 4: In max(x) : no non-missing arguments to max; returning -Inf
> `----
>
> However the following code does produce a plot but with messed up legend
> items. Here is how it looks http://i.troll.ws/55d45652.png . Something
> yellowish that looks like a road should be 'R'.
>
> ,----[ The following results in a messed up legend ]
> | benefits.cat <- deratify(benefits.rast, "class")
> | levelplot(benefits.cat)                 # from rasterVis
> `----
>
> ,----[ The following results in a proper plot but with IDs ]
> | benefits.cat <- deratify(benefits.rast, "ID")
> | levelplot(benefits.cat)                 # from rasterVis
> `----
>
> An here is an experiment with ratify
>
> ,----[ try ratify ]
> | benefits.cat <- ratify(benefits.rast)
> | levelplot(benefits.cat)                 # from rasterVis
> `----
>
> That gives me
>
> ,----
> | Error in .checkLevels(levels(x)[[1]], value) : 
> |   the number of columns in the raster attributes (factors) data.frame should be > 1
> `----


-- 
Oscar Perpiñán Lamigueiro
Grupo de Sistemas Fotovoltaicos (IES-UPM)
Dpto. Ingeniería Eléctrica (EUITI-UPM)
URL: http://procomun.wordpress.com
Twitter: @oscarperpinan



More information about the R-sig-Geo mailing list