[R-sig-Geo] spplot: 1) filling background polygon without overwriting foreground polygon; 2) left justifying sp.layout text

Lyndon Estes lestes at princeton.edu
Fri Oct 8 16:18:13 CEST 2010


Greetings,

I have two questions 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 to draw the
background polygon):

# Code

# Load shapefiles
con1 <- url("http://sites.google.com/site/ldemisc/spplot-question/demo.mdshp.RData")
con2 <- url("http://sites.google.com/site/ldemisc/spplot-question/demo.sashp.RData")
load(con1); load(con2); close(con1); close(con2)

# Set up plot background shape and additional text for map

# Covers over md.shp
#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")

# sa.shp doesn't plot
#sa.lst = list("sp.polygons", sa.shp, fill = "grey", first = T, col = "black")

# Background polygon works like this, but without fill
sa.lst = list("sp.polygons", sa.shp, col = "black")

# 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
underneath 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