[R-sig-Geo] sp and latticeExtra: add colorbar for layer drawn 'under'

Tim Appelhans tim.appelhans at gmail.com
Sat Aug 29 10:55:12 CEST 2015


Ben,
as a follow up, it also works the other way round, which is more intuative:

pols <- spplot(spdfbb,
                colorkey = FALSE,
                sp.layout = list(b = list('sp.polygons', qa, col = 
'grey', first = FALSE)))

rst <- spplot(R)

combo <- rst + as.layer(pols)
combo

I am not sure why SpatialPolygons* objects get rendered via levelplot.
In general, the crux with lattice/latticeExtra is that all layers 
essentially get amalgamated into one list object and hence the style 
settings for the plot are taken from the first object (rst). So if you 
were to draw pols first, the relevant colorkey specification would be 
taken from pols:

pols2 <- spplot(spdfbb,
                 colorkey = list(col = heat.colors(101), width = 1,
                                 at = seq(0, 30, length.out = 100),
                                 space = "top"),
                 sp.layout = list(b = list('sp.polygons', qa, col = 
'grey', first = FALSE)))

pols2

combo2 <- pols2 + as.layer(rst, under = TRUE) #colorkey now taken from pols2
combo2

combo3 <- rst + pols2 #same as above as colorkey is taken from rst
combo3

Best
Tim




On 29.08.2015 02:32, Ben Tupper wrote:
> That works like a charm.
>
> It's sort of non-intuitive that I would use the colorkey argument for the plot of the polygons since color key is documented for levelplot.  In my naive view I would looked to xyplot when plotting polygons. I'm still in the grope and thrash stage with lattice/spplot (and occasionally enjoying it.)
>
> Thanks so much!
>
> Ben
>
> On Aug 28, 2015, at 10:51 AM, Tim Appelhans <tim.appelhans at gmail.com> wrote:
>
>> Ben,
>> you could specify the colorkey manually in the spplot call for spdfbb:
>>
>> sp::spplot(spdfbb,
>>            colorkey = list(col = bpy.colors(101), width = 1,
>>                            at = seq(0, 30, length.out = 100),
>>                            space = "top"),
>>            sp.layout = list(b = list('sp.polygons', qa, col = 'grey', first = FALSE))) +
>>   latticeExtra::as.layer(spplot(R, at = seq(0, 30, length.out = 100)), under = TRUE)
>>
>>
>> You simply need to make sure to pass the same specs to the spplot call to R.
>>
>> HTH
>> Tim
>>
>> On 28.08.2015 16:33, Ben Tupper wrote:
>>> Hello,
>>>
>>> I have drawn on the following posting to draw a raster 'under' a set of polygons as shown in the image link below.  The data used is available at the third link (about 650KB).
>>>
>>> Posting: http://stackoverflow.com/questions/27062768/how-to-plot-additional-raster-with-spplot
>>>
>>> Image: https://dl.dropboxusercontent.com/u/8433654/sp-over-plot.png
>>>
>>> Example data: https://dl.dropboxusercontent.com/u/8433654/example.RData
>>>
>>> Here is a brief description of each variable plotted.
>>>
>>> R is a single layer RasterStack of sea surface temperatures
>>> qa is a SpatialPolygonsDataFrame of fisheries management polygons
>>> spdfbb is also a SpatialPolygonsDataFrame that contains a single polygon (created from the extent of qa)
>>>
>>> sp::spplot(spdfbb,
>>>     colorkey = FALSE,
>>>     sp.layout = list(b = list('sp.polygons', qa, col = 'grey', first = FALSE))) +
>>>     latticeExtra::as.layer(spplot(R), under = TRUE)
>>>
>>> Is it possible add a colorbar for the raster, R, as it might appear if I called...
>>>
>>> spplot(R)
>>>
>>> Session info is below my signature.
>>>
>>> Thanks!
>>> Ben
>>>
>>> Ben Tupper
>>> Bigelow Laboratory for Ocean Sciences
>>> 60 Bigelow Drive, P.O. Box 380
>>> East Boothbay, Maine 04544
>>> http://www.bigelow.org
>>>
>>>> sessionInfo()
>>> R version 3.2.2 (2015-08-14)
>>> Platform: x86_64-apple-darwin13.4.0 (64-bit)
>>> Running under: OS X 10.9.5 (Mavericks)
>>>
>>> locale:
>>> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
>>>
>>> attached base packages:
>>> [1] stats     graphics  grDevices utils     datasets  methods   base
>>>
>>> other attached packages:
>>>   [1] latticeExtra_0.6-26 lattice_0.20-33     rgeos_0.3-8         obpgcrawler_0.1     ecocastmap_0.1
>>>   [6] RColorBrewer_1.1-2  ggplot2_1.0.1       rgdal_0.9-3         spnc_0.1            raster_2.4-15
>>> [11] sp_1.1-1
>>>
>>> loaded via a namespace (and not attached):
>>>   [1] Rcpp_0.12.0      magrittr_1.5     MASS_7.3-43      munsell_0.4.2    colorspace_1.2-6 R6_2.1.1
>>>   [7] stringr_1.0.0    httr_1.0.0       plyr_1.8.3       tools_3.2.2      grid_3.2.2       gtable_0.1.2
>>> [13] digest_0.6.8     reshape2_1.4.1   ncdf4_1.13       curl_0.9.3       stringi_0.5-5    scales_0.2.5
>>> [19] XML_3.98-1.3     proto_0.3-10
>>> _______________________________________________
>>> 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/
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>
> Ben Tupper
> Bigelow Laboratory for Ocean Sciences
> 60 Bigelow Drive, P.O. Box 380
> East Boothbay, Maine 04544
> http://www.bigelow.org
>
> _______________________________________________
> 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