[R-sig-Geo] Plotting point labels along with points using spplot sp.layout list
Felix Andrews
felix at nfrac.org
Mon Oct 1 07:46:03 CEST 2007
On 9/30/07, reeves at nceas.ucsb.edu <reeves at nceas.ucsb.edu> wrote:
> # Assume: ss[] is a char vector of point labels, one per Centroid element..
> # display list entries for first two points
> #
> pt1 = list("sp.text",c(LatLongs[1,1],LatLongs[1,2]),ss[1],col="white",pos=2)
> pt2 = list("sp.text",c(LatLongs[2,1],LatLongs[2,2]),ss[2],col="white",pos=2)
> #
> # I could create these for all 'n' list elements I suppose, but then the
> code is not general.
Look at sp.text, it is nothing but a constrained version of panel.text:
> sp.text
function (loc, txt, ...)
{
if (length(loc) != 2)
stop("loc should have length 2")
if (!is.numeric(loc))
stop("loc should be numeric")
panel.text(loc[1], loc[2], txt, ...)
}
<environment: namespace:sp>
So you can just do this:
ptLabels <- list("panel.text", LatLongs[,1], LatLongs[,2], labels=ss,
col="white", pos=2)
You might also be interested in maptools::pointLabel to place the
labels so they don't overlap.
Felix
On 9/30/07, reeves at nceas.ucsb.edu <reeves at nceas.ucsb.edu> wrote:
> Hello:
>
> Im working on a method to create multilayer raster/vector plots using
> spplot() and supporting functions. The question is: how do I efficiently
> add display list instructions for plotting the point labels that works
> for different sized point vectors?
>
> Here is a working 'brute force' approach (I have omitted the string parsing
> code that produces the point labels) that creates the plot and labels the
> first two points only:
>
> library(sp)
> library(rgdal)
> library(maptools)
> #
> # read files
> #
> Counties <- readShapePoly("PugetSoundCountiesClp2.shp")
> psImg <- readGDAL("PugetSoundSub1.img")
> Centroids <- readShapePoints("PSCentroidPointShape")
>
> len = length(Centroids at coords)
> LatLongs = Centroids at coords
> dim(LatLongs) = c(len/2,2)
> #
> # spplot 'sp.layout' list entries (demote polygons to spatial lines).
> #
> points <- list("sp.points", Centroids, pch = 21,col="green")
> Counties_lines <- as(Counties, "SpatialLines")
> polys <- list("sp.lines", Counties_lines, col="white")
> greys <- grey(0:256 / 256)
> #
> # Assume: ss[] is a char vector of point labels, one per Centroid element..
> # display list entries for first two points
> #
> pt1 = list("sp.text",c(LatLongs[1,1],LatLongs[1,2]),ss[1],col="white",pos=2)
> pt2 = list("sp.text",c(LatLongs[2,1],LatLongs[2,2]),ss[2],col="white",pos=2)
> #
> # I could create these for all 'n' list elements I suppose, but then the
> code is not general.
> #
> grob2 = spplot(psImg, "band1", col.regions=greys,
> sp.layout=list(points,pl1,pl2,polys),cuts=length(greys),
> colorkey=FALSE,scales=list(draw=TRUE))
> #
> plot(grob2)
>
> Something tells me Im doing this the hard way...
> I could construct the arguments to list() dynamically with string building
> code, and then call() or eval() the command, but isnt there a much simpler
> way to 1) tell sp.layout to add the point labels or 2) add the labels to
> the
> plot in a separate command? I have tried separate text() and
> update(trellis.last.object()) commands to no avail.
>
> Suggestions?
>
> Regards, Rick Reeves / NCEAS
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
--
Felix Andrews / 安福立
PhD candidate
Integrated Catchment Assessment and Management Centre
The Fenner School of Environment and Society
The Australian National University (Building 48A), ACT 0200
Beijing Bag, Locked Bag 40, Kingston ACT 2604
http://www.neurofractal.org/felix/
3358 543D AAC6 22C2 D336 80D9 360B 72DD 3E4C F5D8
More information about the R-sig-Geo
mailing list