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/ -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 490 bytes Desc: OpenPGP digital signature URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20161102/27f199d5/attachment.bin>