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

David Wang dw2116 at outlook.com
Tue Nov 1 21:54:24 CET 2016


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]]



More information about the R-sig-Geo mailing list