[R-sig-Geo] Plotting point labels along with points using spplot sp.layout list

Rick Reeves reeves at nceas.ucsb.edu
Mon Oct 1 19:09:48 CEST 2007


Thanks Felix, this is the answer that I was looking for!



points <- list("sp.points", Centroids, pch = 21,col="green")
polys <- list("sp.lines", Counties_lines, col="white")
ptLabels =
list("panel.text",LatLongs[,1],LatLongs[,2],labels=ss,col="white",pos=2)
print("plotting the grob")
browser()
grob2 = spplot(psImg, "band1", col.regions=greys,
sp.layout=list(points,ptLabels,polys),cuts=length(greys),
colorkey=FALSE,scales=list(draw=TRUE))
plot(grob2)

the complete example will soon be posted on my Web site:

http://nceas.ucsb.edu/scicomp/GISSeminar/UseCases/MapProdWithRGraphics/OneMapProdWithRGraphics.html

Regards, Rick R

Felix Andrews wrote:
> 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
>>
>>     
>
>
>   
-------------- next part --------------
A non-text attachment was scrubbed...
Name: reeves.vcf
Type: text/x-vcard
Size: 350 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20071001/5877be5f/attachment.vcf>


More information about the R-sig-Geo mailing list