country-labels on spplot()

2019-06-28 00:55发布

问题:

I'd like to add name-labels for regions on an spplot().

Example:

load(url('http://gadm.org/data/rda/FRA_adm0.RData'))
FR <- gadm
FR <- spChFIDs(FR, paste("FR", rownames(FR), sep = "_"))
load(url('http://gadm.org/data/rda/CHE_adm0.RData'))
SW <- gadm
SW <- spChFIDs(SW, paste("SW", rownames(SW), sep = "_"))
load(url('http://gadm.org/data/rda/DEU_adm0.RData'))
GE <- gadm
GE <- spChFIDs(GE, paste("GE", rownames(GE), sep = "_"))

df <- rbind(FR, SW, GE)

## working
plot(df)
text(getSpPPolygonsLabptSlots(df), labels = c("FR", "SW", "GE"))


## not working
spplot(df[1-2,])
text((getSpPPolygonsLabptSlots(df), labels = c("FR", "SW"))

The second one probably doesn't work because of lattice!? However, I need the spplot-functionality. How would I get the labels on the plot?

回答1:

Standard way of adding some text is using the function ltext of lattice, but the coordinates given there are always absolute. In essence, you can't really rescale the figure after adding the text. Eg :

data(meuse.grid)
gridded(meuse.grid)=~x+y
meuse.grid$g = factor(sample(letters[1:5], 3103, replace=TRUE),levels=letters[1:10])
meuse.grid$f = factor(sample(letters[6:10], 3103, replace=TRUE),levels=letters[1:10])

spplot(meuse.grid, c("f","g"))
ltext(100,200,"Horror")

Produces these figures (before and after scaling)

You can use a custom panel function, using the coordinates within each panel :

myPanel <- function(x,y,xx,yy,labels,...){
    panel.xyplot(x,y,...)
    ltext(xx,yy,labels)
}

xyplot(1:10 ~ 1:10,data=quakes,panel=myPanel,
        xx=(1:5),yy=(1:5)+0.5,labels=letters[1:5])

(run it for yourself to see how it looks)

This trick you can use within the spplot function as well, although you really have to check whatever plotting function you use. In the help files on spplot you find the possible options (polygonsplot, gridplot and pointsplot), so you have to check whether any of them is doing what you want. Continuing with the gridplot above, this becomes :

myPanel <- function(x,y,z,subscripts,xx,yy,labels,...){
    panel.gridplot(x,y,z,subscripts,...)
    ltext(xx,yy,labels)
}
# I just chose some coordinates
spplot(meuse.grid, c("f","g"),panel=myPanel,xx=180000,yy=331000,label="Hooray")

which gives a rescalable result, where the text is added in each panel :



回答2:

Thank you, Gavin Simpson!

I finally found a way.

In the hope it helps others in the future, I post my solution:

sp.label <- function(x, label) {
    list("sp.text", coordinates(x), label)
}

ISO.sp.label <- function(x) {
    sp.label(x, row.names(x["ISO"]))
}

make.ISO.sp.label <- function(x) {
    do.call("list", ISO.sp.label(x))
}

spplot(df['ISO'], sp.layout = make.ISO.sp.label(df))