[R-sig-Geo] How to draw the same legend (one legend) for the multiple spatial figures?

Florian Detsch florian.detsch at staff.uni-marburg.de
Mon Aug 7 10:04:22 CEST 2017


Dear Wei,

probably the most straightforward way to combine multiple trellis plots 
into one is via latticeExtra::c.trellis(). Be aware, however, that the 
desired range of z-values (from which your legend will ultimately be 
created) needs to be assigned manually to each sub-plot using 'at'. Here 
is a minimal example based on the meuse.grid 'SpatialPixelsDataFrame' 
from sp, which works the same eg for 'Raster*' objects.

-----

## sample data
library(sp)
data("meuse.grid")
gridded(meuse.grid) = ~ x + y
meuse.grid$dist2 = meuse.grid$dist^2

p1 = spplot(meuse.grid, zcol = "dist", at = seq(0, 1, .01),
             sp.layout = list("sp.text", c(179000, 333250), "a) dist"),
             colorkey = list(height = .5), scales = list(draw = TRUE))
p2 = spplot(meuse.grid, zcol = "dist2", at = seq(0, 1, .01),
             sp.layout = list("sp.text", c(179000, 333250), "b) dist2"))

## combine plots using latticeExtra::c.trellis
update(c(p1, p2), layout = c(1, 2), as.table = TRUE) # 1 column, 2 rows
update(c(p1, p2)) # 1 row, 2 columns

-----

In order to combine numerous plots into one (eg stored in a 'list'), you 
can take inspiration from the Reduce()-based approach in 
Orcs::latticeCombineGrid() (see 
https://github.com/fdetsch/Orcs/blob/master/R/latticeCombineGrid.R; 
credit goes to Tim Appelhans), among others.

Best,
Florian


On 06.08.2017 14:13, Bede-Fazekas Ákos wrote:
> Dear Brandon and Wei,
> I don't know the answer to your question but a somewhat similar 
> solution is when you plot a separate legend using lattice::draw.key() 
> and disable sp::spplot()'s built-in legend using argument "auto.key = 
> FALSE".
> An example:
>
> # loading libraries
> library(lattice)
> library(sp)
> library(gridExtra)
> library(grid)
>
> # drawing legend
> no_of_categories <- 10
> cutpoints <- seq(from = 0 - 0.0001, to = 1 + 0.0001, length.out = 
> no_of_categories + 1)
> legend_text <- apply(X = cbind(round(cutpoints [1:no_of_categories], 
> 1), round(cutpoints [2:(no_of_categories + 1)], 1)), MARGIN = 1, FUN = 
> function(number) {return(paste(format(x = number, digits = 2), 
> collapse = " - "))})
> legend_colors <- colorRampPalette(c("red", "orange", "yellow", 
> "lightgreen", "darkgreen"))(no_of_categories)
> legend <- draw.key(key = list(reverse.rows = TRUE, space = "right", 
> rectangles = list(col = legend_colors, border = FALSE), text = 
> list(legend_text)), draw = FALSE)
> dev.off()
>
> # drawing maps
> maps <- list()
> for (column_name in colnames(sp_object at data)) {
>     map <- spplot(obj = sp_object, zcol = column_name, auto.key = 
> FALSE, col.regions = legend_colors, cuts = cutpoints)
>     maps <- append(maps , list(map))
> }
> maps <- append(maps , list(legend))
>
> # plotting the map-legend composite to a png file
> layout <- rbind(c(1,2), c(3,4), c(5,6)) # let's say we have 5 maps and 
> a legend
> composite <- do.call(arrangeGrob, c(maps, list(layout_matrix = 
> layout), list(widths = c(1, 1))))
> png(width = 1000, height = 1000, filename = "map.png"))
>     grid.newpage()
>     grid.draw(composite)
> dev.off()
>
> Hope this helps,
> Ákos Bede-Fazekas
> Hungarian Academy of Sciences
>
> 2017.08.06. 11:53 keltezéssel, Brandon Payne írta:
>> How to draw the same legend (one legend) for    the
>>        multiple spatial figures?
>>
>> I would put the legend next to the upper-most plot and
>> wrap the whole thing in a single
>>
>> /figure
>>
>> so that it would be more obvious that the same legend applied to all.
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
Dr. Florian Detsch
Environmental Informatics
Department of Geography
Philipps-Universität Marburg
Deutschhausstraße 12
35032 (parcel post: 35037) Marburg, Germany

Phone: +49 (0) 6421 28-25323
Web: http://www.uni-marburg.de/fb19/fachgebiete/umweltinformatik/detschf/index.html



More information about the R-sig-Geo mailing list