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

Bede-Fazekas Ákos bfalevlist at gmail.com
Tue Nov 1 23:02:59 CET 2016


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
>



More information about the R-sig-Geo mailing list