[R-sig-Geo] Trellis with spplot

Tim Appelhans tim.appelhans at gmail.com
Wed Sep 3 17:40:16 CEST 2014


Dear Nebi,
find one solution below. What you can do is split your meuse data set 
according to the factor you like (in your example 'soil'). Then, after 
making sure you have the same bounding box for each subset (I just too 
the one from the overall meuse data), you can produce a plot for each 
subset and put them back together using 'Reduce()'. The only thing that 
is not straight-forward with this approach is the creation of the 
legend. For this I have included a little example using the grid package 
to focus on a certain region of your plot (in your example case the 
empty bottom right panel) and then draw a key from scratch using 
lattice::draw.key(). This is a bit more tedious, but it will give you 
complete control over your layout.

Hope that helps.

Best
Tim

On 09/03/2014 04:41 PM, N St wrote:
> Hello
>
> I would like to facet a spplot with the factors of one variable (like
> a xyplot(y~x|A)). I want to use spplot because I have to add others
> polygons layers. In my reproducible example below I would like to end
> up with three plots, one for each categorie (1,2,3) in a trellis
>
> --------------------------
>
> library(sp); library(lattice) ; library(RColorBrewer) ; library(latticeExtra)
> data(meuse)
> coordinates(meuse) <- c("x", "y")
> cc <- coordinates(meuse)
> m.sl <- SpatialLines(list(Lines(list(Line(cc)), "line1")))
> data(meuse.riv)
> meuse.lst <- list(Polygons(list(Polygon(meuse.riv)), "meuse.riv"))
> meuse.pol <- SpatialPolygonsDataFrame(SpatialPolygons(meuse.lst),
> data.frame(1), match.ID=FALSE)
>
> colfactor = RColorBrewer:::brewer.pal(3,"Accent")
> pts <- list("sp.points", meuse, pch = 16, col = colfactor, cex=2,  )
> meuse.layout <- list(pts)
library(gridExtra)

## split meuse data according to soil levels
meuse.splt <- split(meuse, f = meuse$soil)

## create spplot for each subset of the previous split
l.lst <- lapply(seq(meuse.splt), function(i) {
   dat <- meuse.splt[[i]]
   dat at bbox <- bbox(meuse)
   spplot(dat, c("soil"), scales=list(draw=T), asp="iso",
          col.regions=colfactor[[i]], cex=2,
          auto.key = FALSE,
          as.table = TRUE)
})

## function to combine the individual plots
outLayout <- function(x, y) {
   update(c(x, y,
            layout = c(2, 2)), x.same = TRUE, y.same = TRUE,
          between = list(y = 0.3, x = 0.3))
}

## combine the plots
l <- Reduce(outLayout, l.lst)

## draw the plot to a new page
grid.newpage()

print(l + p, newpage = FALSE)

## focus on plotting area
downViewport(trellis.vpname(name = "figure"))

## set up new viewport for drawing the legend
vp1 <- viewport(x = 1, y = 0,
                 height = 0.4, width = 0.2,
                 just = c("right", "bottom"),
                 name = "legend.vp")

## focus on new viewport
pushViewport(vp1)

## draw legend
draw.key(list(text=list(c("1", "2", "3"),
                         cex = 1),
               columns = 1, rows = 3,
               points = list(pch = 16,
                             cex = 2,
                             col = colfactor),
               padding.text = 2),
          draw = T,
          vp = viewport(x = unit(0.5, "npc"),
                        y = unit(0.5, "npc"),
                        just = "centre"))

## re-focus on entire drawing area (i.e. the plotting device)
upViewport(0)
>
> l <- spplot(meuse, c("soil"), scales=list(draw=T), asp="iso",
> col.regions=colfactor, cex=2)
> p <- spplot(meuse.pol, fill="blue")
> l+p
>
>
> -----------------------------
> It sounds really easy but I couldn't find an answer.
>
> Best Regards
> Nebi
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

-- 
#####################################
Tim Appelhans
Department of Geography
Environmental Informatics
Philipps Universität Marburg
Deutschhausstraße 12
35032 Marburg (Paketpost: 35037 Marburg)
Germany

Tel +49 (0) 6421 28-25957

http://environmentalinformatics-marburg.de/



More information about the R-sig-Geo mailing list