[R-sig-Geo] Harmonizing the sizes of side-by-side spplots

Edzer Pebesma edzer.pebesma at uni-muenster.de
Sat May 5 21:17:48 CEST 2012


With the help of Deepayan Sarkar, lattice_0.20-7 (on r-forge) lets you
control the key widths; the following two examples (different sized key
right, key bottom) come out with identical map extents (cartographic
scales). Thanks Deepayan!

require(gstat)
data(jura)
coordinates(jura.grid) <- ~ Xloc + Yloc
gridded(jura.grid) <- T
fullgrid(jura.grid) <- T
p1 <- spplot(jura.grid, zcol="Landuse", col.regions=terrain.colors(4))
p2 <- spplot(jura.grid, zcol="Rock", col.regions=bpy.colors(5))
require(lattice)
print(p1, split=c(1,1,2,1), more=T)
print(p2, split=c(2,1,2,1), more=F)

oopt <- lattice.options()
lattice.options(layout.widths =
        list(key.right = list(x = 3,
                 units = "cm", data = NULL)))
print(p1, split=c(1,1,2,1), more=T)
print(p2, split=c(2,1,2,1), more=F)
lattice.options(oopt)

loadMeuse()
p3 = spplot(meuse, "zinc", col.regions = bpy.colors())
p4 = spplot(meuse, "zinc", col.regions = bpy.colors(), cuts = 7)

print(p3, split=c(1,1,2,1), more=T)
print(p4, split=c(2,1,2,1), more=F)

oopt <- lattice.options()
lattice.options(layout.heights =
        list(key.bottom = list(x = 4,
                 units = "cm", data = NULL)))
print(p3, split=c(1,1,2,1), more=T)
print(p4, split=c(2,1,2,1), more=F)
lattice.options(oopt)


On 05/03/2012 05:21 PM, David Rossiter wrote:
> I often want to display two or more maps of the same area, prepared with spplot, side-by-side, but the sizes of the map do not match if (1) a legend is printed and (2) the width of the text in the legend is different. For example:
> 
>> require(gstat)
>> data(jura)
>> coordinates(jura.grid) <- ~ Xloc + Yloc
>> gridded(jura.grid) <- T
>>  fullgrid(jura.grid) <- T
>> print(, split=c(1,1,2,1), more=T)
>> p1 <- spplot(jura.grid, zcol="Landuse", col.regions=terrain.colors(4))
>> p2 <- spplot(jura.grid, zcol="Rock", col.regions=bpy.colors(5))
>> require(lattice)
>> print(p1, split=c(1,1,2,1), more=T)
>> print(p2, split=c(2,1,2,1), more=F)
> 
> Here, since the longest name of the rock types is longer than that of the landuse, the rock type map is smaller, to make room within the box for the legend.
> 
> (The same hapens if plotting kriging predictions next to their variances, if the numeric precision is not the same.)
> 
> I can get around the problem by adjusting the position within the box for the larger map:
> 
> print(p1, position=c(0.04,0.04,.96,.96), …)
> 
> but this (1) requires a manual assessment (maybe I could do it automatically by looking at the legend categories?) and (2) seems a clumsy way to do this. I have waded through the
> 
> I've tried to use the panel.height and panel.width arguments to print.trellis, but without success:
> 
>> str(p.width <- lattice.getOption("layout.widths")$panel)
> List of 3
>  $ x    : num 1
>  $ units: chr "null"
>  $ data : NULL
>> p.width <- list(0.9,"null",NULL)
> 
> But this (1) does not seem to affect the plots, (2) would probably only affect the whole panel, not just the map. I need to set the size of the map and legend separately.
> 
> I tried to present these as side-by-side levelplots, but since the levels are not the same this fails also:
> 
>> p3 <- spplot(jura.grid[c("Landuse","Rock")])
> Error in stack.SpatialPointsDataFrame(as(data, "SpatialPointsDataFrame"),  :
>   all factors should have identical levels
> 
> Thanks for any help,
> 
> D G Rossiter
> Senior University Lecturer
> Faculty of Geoinformation and Earth   Observation Science
> Universiteit Twente.
> Enschede (NL)
> 
> 
> Faculty of Geo-Information Science and Earth Observation (ITC)
> University of Twente
> Chamber of Commerce: 501305360000
> 
> E-mail disclaimer
> The information in this e-mail, including any attachments, is intended for the addressee only. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution or action in relation to the content of this information is strictly prohibited. If you have received this e-mail by mistake, please delete the message and any attachment and inform the sender by return e-mail. ITC accepts no liability for any error or omission in the message content or for damage of any kind that may arise as a result of e-mail transmission.
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

-- 
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster
Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
8333081, Fax: +49 251 8339763  http://ifgi.uni-muenster.de
http://www.52north.org/geostatistics      e.pebesma at wwu.de



More information about the R-sig-Geo mailing list