[R-sig-Geo] Add layers to a lattice plot via a for loop

Florian Detsch florian.detsch at staff.uni-marburg.de
Wed Nov 2 16:17:44 CET 2016


David,

the problem is related to latticeExtra::layer() rather than spplot(). I 
faced this issue a while ago, and back then, Oscar Perpiñán Lamigueiro 
pointed me towards the help pages of ?latticeExtra::layer, which says:

       Note that the evaluation used in ‘layer’ is non-standard, and can
       be confusing at first: you typically refer to variables as if
       inside the panel function (‘x’, ‘y’, etc); you can usually refer
       to objects which exist in the global environment (workspace), but
       it is safer to pass them in by name in the ‘data’ argument to
       ‘layer’. (And this should not to be confused with the ‘data’
       argument to the original ‘xyplot’.)

To cut a long story short, you are simply required to explicitly add a 
'data' argument (including the loop variable) to layer():

p <- spplot(coast, col.regions = "darkgray", colorkey = FALSE)
for (k in seq_along(tracks))
   p <- p + layer(sp.points(tracks[[k]], pch = 19, col = k),
                  data = list(k = k))
print(p)

Hope that helps,
Florian


On 02.11.2016 15:54, David Wang wrote:
> Oops, I didn't run the example in a clean, new session. Here is that an update that should work:
>
>
> library(maps)
> library(maptools)
> library(sp)
> library(latticeExtra)
>
> coast <- map(fill = TRUE, plot = FALSE)
> coast <- map2SpatialLines(coast)
> coast <- SpatialLinesDataFrame(coast, data.frame(z = seq_len(length(coast))))
> tracks <- list(SpatialPoints(cbind(c(90, 90), c(-20, 20))),
>                 SpatialPoints(cbind(c(-90, -90), c(-20, 20))))
>
> p <- spplot(coast, col.regions = "darkgray", colorkey = FALSE)
> p <- p + layer(sp.points(tracks[[1]], pch = 19, col = 1))
> p <- p + layer(sp.points(tracks[[2]], pch = 19, col = 2))
> print(p) # shows both tracks
>
> p <- spplot(coast, col.regions = "darkgray", colorkey = FALSE)
> for (k in seq_along(tracks))
>    p <- p + layer(sp.points(tracks[[k]], pch = 19, col = k))
> print(p) # shows the second (red) track only
>
> The R environment is:
>
>> sessionInfo()
> R version 3.3.1 (2016-06-21)
> Platform: x86_64-w64-mingw32/x64 (64-bit)
> Running under: Windows 7 x64 (build 7601) Service Pack 1
>
> Thanks,
>
> D
>
> ________________________________
> From: R-sig-Geo <r-sig-geo-bounces at r-project.org> on behalf of Edzer Pebesma <edzer.pebesma at uni-muenster.de>
> Sent: Wednesday, November 2, 2016 10:33:31 AM
> To: r-sig-geo at r-project.org
> Subject: Re: [R-sig-Geo] Add layers to a lattice plot via a for loop
>
>
>
> On 02/11/16 14:52, David Wang wrote:
>> Hello �kos,
>>
>>
>> Thank you for your suggestion. I'm not sure that solves the problem. But here is a reproducible example:
>>
>>
>> library(maps)
>> library(maptools)
>> library(sp)
>> coast <- map(fill = TRUE, plot = FALSE)
>> coast <- map2SpatialLines(coast)
>> tracks <- list(SpatialPoints(cbind(c(90, 90), c(-20, 20))),
>>                 SpatialPoints(cbind(c(-90, -90), c(-20, 20))))
>>
>> p <- spplot(coast, col.regions = "darkgray", colorkey = FALSE)
> Trying to reproduce, here I get:
>> p <- spplot(coast, col.regions = "darkgray", colorkey = FALSE)
> Error in (function (classes, fdef, mtable)  :
>    unable to find an inherited method for function ‘spplot’ for signature
> ‘"SpatialLines"’
>
>
>> p <- p + layer(sp.points(tracks[[1]], pch = 19, col = 1))
>> p <- p + layer(sp.points(tracks[[2]], pch = 19, col = 2))
>> print(p) # this shows both tracks
>>
>> p <- spplot(coast, col.regions = "darkgray", colorkey = FALSE)
>> for (k in seq_along(tracks))
>>    p <- p + layer(sp.points(tracks[[k]], pch = 19, col = k))
>> print(p) # this shows the second (red) track only
>>
>> Thanks,
>>
>> David
>>
>> ________________________________
>> From: R-sig-Geo <r-sig-geo-bounces at r-project.org> on behalf of Bede-Fazekas �kos <bfalevlist at gmail.com>
>> Sent: Tuesday, November 1, 2016 6:02:59 PM
>> To: r-sig-geo at r-project.org
>> Subject: Re: [R-sig-Geo] Add layers to a lattice plot via a for loop
>>
>> Dear David,
>>
>> since you have not provided us a reproductable script+dataset, I can't
>> check whether my suggestion is connected to your problem. Maybe not...
>> Anyway, lattice plotter functions - such as spplot() and its relatives
>> -  inside for loops, functions, and tryCatch() blocks usually work in a
>> different way than their line-by-line versions. Using print(spplot())
>> instead of spplot() can solve the problem.
>>
>> HTH,
>> �kos Bede-Fazekas
>> Hungarian Academy of Sciences
>>
>> 2016.11.01. 21:54 keltez�ssel, David Wang �rta:
>>> Hello,
>>>
>>>
>>> I need to overlay a set of tracks (SpatialPoints objects) on a map and thought I would add them one by one to a lattice plot of coastline. However, when I did
>>>
>>>
>>> p <- spplot(coast, xlim = xlim, ylim = ylim, col.regions = "darkgray", colorkey = FALSE)
>>> for (k in seq_along(tracks))
>>>     p <- p + layer(sp.points(tracks[[k]], pch = 20, col = k))
>>> p
>>>
>>> (where coast is the coastline as a SpatialLines object, and tracks is a list of SpatialPoints objects), p only shows the coastline and the last track. What's perplexing to me is that when I explicitly added layers without using a for loop, the result trellis object p does contain all four tracks in their specific colors:
>>>
>>>
>>>
>>>
>>> p <- spplot(coast, xlim = xlim, ylim = ylim, col.regions = "darkgray", colorkey = FALSE)
>>> p <- p + layer(sp.points(tracks[[1]], pch = 20, col = 1))
>>> p <- p + layer(sp.points(tracks[[2]], pch = 20, col = 2))
>>> p <- p + layer(sp.points(tracks[[3]], pch = 20, col = 3))
>>> p <- p + layer(sp.points(tracks[[4]], pch = 20, col = 4))
>>> p
>>>
>>> Does anyone happen to have a clue why the for loop failed to overlay layers?
>>>
>>> Thanks,
>>> David
>>>
>>>         [[alternative HTML version deleted]]
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>>         [[alternative HTML version deleted]]
>>
>>
>>
>> _______________________________________________
>> 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
> Heisenbergstraße 2, 48149 Münster, Germany; +49 251 83 33081
> Journal of Statistical Software:   http://www.jstatsoft.org/
> Computers & Geosciences:   http://elsevier.com/locate/cageo/
>
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
Florian Detsch (M.Sc. Physical Geography)
Environmental Informatics
Department of Geography
Philipps-Universität Marburg
Deutschhausstraße 12
35032 (parcel post: 35037) Marburg, Germany

Phone: +49 (0) 6421 28-25323
Web: http://umweltinformatik-marburg.de/en/staff/florian-detsch/


	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list