[R-sig-Geo] spplot with two SpatialPolygonsDataFrame: 1) filling background polygon; 2) left justifying text

Lyndon Estes lestes at princeton.edu
Thu Oct 7 05:13:26 CEST 2010


Greetings,

I have two questions/problems related to spplot:

1. I am trying to plot one SpatialPolygonsDataFrame over another, and
fill the background polygon with grey. I can't seem to fill the
background polygon without having it cover over my supposed foreground
polygon, even if I specify "first = T" (which fails):

link1 <- "http://sites.google.com/site/ldemisc/spplot-question/demo.mdshp.RData"
link2 <- "http://sites.google.com/site/ldemisc/spplot-question/demo.sashp.RData"

load(url(link1)); load(url(link2))

# Set up plot background shape and additional text

#sa.lst = list("sp.polygons", sa.shp, fill = "grey", col = "black") #
Covers over md.shp
#sa.lst = list("sp.polygons", sa.shp, fill = "grey", first = F, col =
"black") # Covers over md.shp
#sa.lst = list("sp.polygons", sa.shp, fill = "grey", first = T, col =
"black") # sa.shp doesn't plot
sa.lst = list("sp.polygons", sa.shp, col = "black")  # Shape of South
Africa as background polygon -
                                                     #   works, but no fill

# Additional text - overall mean yield.
text1 <- list("sp.text", c(360000, -3210000),
             paste("Overall mean yield =",
round(mean(md.shp at data[-c(22,69), 4]), 0), "kg/ha"),
             col ="red", cex = 1)

# Scale bar limits and title
scmax  <- round(max(md.shp at data[, 4]) / 50, 0) * 50
scmin  <- round(min(md.shp at data[, 4]) / 50, 0) * 50
byval  <- 25
titlevec <- "Mean district maize yields (kg/ha) 1981-99: Cultivar PAN6528"

# Spplot
dev.new()
spplot(md.shp[-c(22,69),], z = 4,
       sp.layout = list(sa.lst, text1),
       col.regions = topo.colors(500),
       at = seq(scmin, scmax, by = byval),
       main = titlevec)

Any advice on how to correctly set "sa.lst" so that sa.shp fills
underneat md.shp will be much appreciated.  I haven't been able to
find a ready answer in the help files, except for this:

"The order of items in sp.layout matters; objects are drawn in the
order they appear. Plot order and prevalence of sp.layout items: for
points and lines, sp.layout items are drawn before the points (to
allow for grids and polygons); for grids and polygons sp.layout is
drawn afterwards (so the item will not be overdrawn by the grid and/or
polygon). Although a matter of taste, transparency may help when
combining things. "

But, beyond what I have tried so far, I can't figure out how to write
the code so that sa.shp is drawn first.

The second question:

2. Is there an option to pass to:

text1 <- list("sp.text", c(360000, -3210000),
             paste("Overall mean yield =",
round(mean(md.shp at data[-c(22,69), 4]), 0), "kg/ha"),
             col ="red", cex = 1)

so that the text is left-justified on the coordinates  c(360000,
-3210000)? At the moment it seems that the text is centered at this
location.

Thanks in advance for any help you can provide.

Best, Lyndon



More information about the R-sig-Geo mailing list