[R-sig-Geo] modify legend on levelplot

Oscar Perpiñan oscar.perpinan at upm.es
Thu Sep 18 12:57:03 CEST 2014


Hi,

This is not possible with the default colorkey mechanism of levelplot. You
have to disable it (colorkey=FALSE) and define a custom key using the "key"
argument. It expects a list whose components define the elements of the
legend. You should read the help page of lattice::xyplot for details about
"key".

Here you have a toy example that you should adapt to your needs:

library(rasterVis)

## Define a categorical raster
r <- raster(nrow=10, ncol=10)
r[] = 1
r[51:100] = 3
r[3:6, 1:5] = 5
r <- ratify(r)

## Levels
rat <- levels(r)[[1]]
rat$landcover <- c('Pine', 'Oak', 'Meadow')
levels(r) <- rat

## Key
myColors <- c('palegreen', 'midnightblue', 'indianred1')
myKey <- list(text=list(lab=rat$landcover),
              rectangles=list(col = myColors),
              space='inside',
              columns=3)

levelplot(r, col.regions = myColors, colorkey = FALSE, key = myKey)

Hope it helps.

Best,

Oscar.
-----------------------------------------------------------------
Oscar Perpiñán Lamigueiro
Dpto. Ingeniería Eléctrica (ETSIDI-UPM)
Grupo de Sistemas Fotovoltaicos (IES-UPM)
URL: http://oscarperpinan.github.io

2014-09-17 22:01 GMT+02:00 Tyler Frazier <tyler.j.frazier at icloud.com>:

> Hello,
>
> I would like to modify my legend on a rasterVis levelplot() but can’t
> figure it out.
>
> I use
>
> p <- levelplot(x_rat, main = “title", margin = FALSE, xlab = NULL, ylab =
> NULL, auto.key=list(space="top", columns=3),
> col.regions=c(‘gray','darkgreen','tomato4','yellow','purple','blue','azure2','orange','hotpink','peachpuff','salmon2'))
> p + layer(sp.lines(borders, lwd=1.2, col='gray60'))
>
> with the idea that the auto.key command will center the legend at the top
> of the plot, three items at a time (3 per column, 4 rows with the last row
> having 2).
>
> I would also like to locate the legend inside the borders over part of the
> main plot area where the map is less informative (with an opaque white
> background inside the legend box).
>
> Any suggestions would be appreciated.
>
> Thank you,
> Tyler
> -------------------------------------------------------------------------
> Tyler Frazier
> Center for Development Research (ZEF-C)
> University of Bonn
> 53113 Bonn, DE
> +49 (0) 228 73 4949 (office)
> +49 (0) 152 1018 2718 (handy)
> -------------------------------------------------------------------------
>
>
>
>
>
>
>
>         [[alternative HTML version deleted]]
>
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list