[R-sig-Geo] Strategy to crop margins in map by spplot()?

Matthieu Stigler matthieu.stigler at gmail.com
Wed Aug 24 14:16:03 CEST 2011


Thanks a lot Brian!

It seems however that spplot() creates the big above/bottom margins with 
another trick than layout.height$top.padding.... I tried removing 
everything, yet I still have huge margins...

Any idea which parameter to change to remove margins? See code below!

Thanks!!!

Matthieu

library(maptools)
library(lattice)

data(wrld_simpl) #sorry, this is so slow, but is the only polygon 
shapefile available by default I think?
x11(type="Xlib") #should be slightly faster
spplot(wrld_simpl, "SUBREGION")

theme.novpadding <-
    list(layout.heights =
         list(top.padding = 0,
              main.key.padding = 0,
              key.axis.padding = 0,
              axis.xlab.padding = 0,
              xlab.key.padding = 0,
              key.sub.padding = 0,
              bottom.padding = 0),
         layout.widths =
         list(left.padding = 0,
              key.ylab.padding = 0,
              ylab.axis.padding = 0,
              axis.key.padding = 0,
              right.padding = 0))
spplot(wrld_simpl, "SUBREGION",    par.settings = theme.novpadding)

Le 24/08/2011 13:12, Brian Oney a écrit :
> Hi Matthieu,
> As spplot is "just" a wrapper for the lattice package. It helps to 
> know/learn a thing about the lattice package.
> The control of graphical parameters in the lattice package is 
> extremely fine.
>
> Here is one working example changing the layout parameters:
> library(sp)
> library(lattice) # required for trellis.par.set():
> trellis.par.set(sp.theme()) # sets color ramp to bpy.colors()
>
> data(meuse)
> coordinates(meuse)=~x+y
> ## coloured points plot with legend in plotting area and scales:
> spplot(meuse, "zinc", do.log = TRUE,
>     key.space=list(x=0.2,y=0.9,corner=c(0,1)),
>     scales=list(draw=T))
>
> ### Do a bit of exploring in lattice
> trellis.pars.save <- trellis.par.get() # for good measure
> trellis.pars <- trellis.par.get()
> names(trellis.pars)
> names(trellis.pars$layout.heights)
> layout.height <- trellis.par.get("layout.heights")
> names(layout.height)
> layout.height$top.padding
> layout.height$top.padding <- 0 ### here is where margins etc. can be 
> changed
>
> trellis.par.set(layout.heights = layout.height)
> ### now try it again
> spplot(meuse, "zinc", do.log = TRUE,
>     key.space=list(x=0.2,y=0.9,corner=c(0,1)),
>     scales=list(draw=T))
> windows() ### open a new plotting device to compare with other plots
>
> ### Put it back to the defaults
> trellis.par.set(layout.heights = trellis.pars.save$layout.heights)
>
> I recall that sweaving with the lattice package presents some 
> problems, but can't remember exactly. If there are problems, I would 
> "manually" insert it i.e.:
>
>
> <<echo=FALSE,results=tex>>=
> ### this should work but not so sure.
> png(file="graph.png", width = 4, height = 5,units='in',res=900)
> spplot(something)
> dev.null <- dev.off()
> cat("\\includegraphics[width=0.95\\textwidth]{graph}\n\n", sep="")
> @
>
> hth,
> Brian
>



More information about the R-sig-Geo mailing list