Search code examples
rplotgislatticer-sp

How to set the default theme for spplot?


Let's say, I have some spatial data and I want to plot it using spplot function from sp package:

library('sp')
library('lattice')
demo(meuse, ask = FALSE, echo = FALSE)
spplot(meuse.grid[,'dist'])

enter image description here

It's easy to change the theme, for example col.regions, for only one plot:

spplot(meuse.grid[,'dist'], col.regions=rainbow(100))

enter image description here

However, what if I want to change the col.regions for all of my plots? I've tried a few functions, for example:

trellis.par.set(sp.theme(set = FALSE, regions = list(col = rainbow(100)))) #1
trellis.par.set(regions = list(col = rainbow(100))) #2
lattice.options(default.theme = sp.theme(set = FALSE, regions = list(col = rainbow(100)))) #3

But nothing seems to work. So my question is - how to properly set the default theme for spplot?


Solution

  • Have a look at the following code in order to manually re-define the default color scheme for spplot (see also ?spplot where set_col_regions is further described).

    ## re-define default color scheme
    old_theme <- get_col_regions()
    new_theme <- set_col_regions(rainbow(100))
    
    ## sample data
    data("meuse.grid")
    coordinates(meuse.grid) <- ~x+y
    proj4string(meuse.grid) <- CRS("+init=epsg:28992")
    gridded(meuse.grid) <- TRUE
    
    ## display data with c
    spplot(meuse.grid, zcol = "dist")                
    

    spplot

    The customized color scheme persists for any subsequently created 'spplot' object, e.g.

    library(mapview)
    spplot(poppendorf, "B007n")
    

    mapview