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

David Wang dw2116 at outlook.com
Wed Nov 2 14:52:39 CET 2016


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



More information about the R-sig-Geo mailing list