[R] [solved!] [lattice] how to overlay a geographical map on a levelplot?
Tom Roche
Tom_Roche at pobox.com
Wed Nov 21 07:22:15 CET 2012
https://stat.ethz.ch/pipermail/r-sig-geo/2012-November/016757.html
>> summary: How to overlay a geographical map on each panel in a lattice
>> (or Trellis), e.g., of levelplot's?
https://stat.ethz.ch/pipermail/r-help/2012-November/329716.html
> you need to launch the library 'latticeExtra'
That's it!
# start example
library(latticeExtra) # for plotting
library(maps) # for ... maps
# Not too many points, for this example
# -130° to -59.5: (130-59.5)/1.5=47
# 23.5° to 58.5°: 58.5-23.5 =35
# define longitudes
west.lon.deg=-130
east.lon.deg=-59.5
step.lon.deg=1.5
lons <- c(seq(west.lon.deg, east.lon.deg, step.lon.deg))
n.lon <- length(lons)
# define latitudes
south.lat.deg=23.5
north.lat.deg=58.5
step.lat.deg=1.0
lats <- c(seq(south.lat.deg, north.lat.deg, step.lat.deg))
n.lat <- length(lats)
# define vertical levels
bot.lev.mb=1000
top.lev.mb=50
n.lev=4
levs <- c(seq(from=bot.lev.mb, to=top.lev.mb, length.out=n.lev))
# define concentrations
grid.3d.len <- n.lon * n.lat * n.lev
concs <- c(1:grid.3d.len)
# define grid
grid.3d.df <- expand.grid(longitude=lons, latitude=lats, level=levs)
# add concentrations as data
data.3d.df <- cbind(grid.3d.df, concentration=concs)
# debugging
head(data.3d.df)
tail(data.3d.df)
# create geographical map: thanks, Pascal Oettli
world.map <- map('world', plot=FALSE,
xlim=c(west.lon.deg,east.lon.deg),
ylim=c(south.lat.deg,north.lat.deg))
world.df <- data.frame(lon=world.map$x, lat=world.map$y)
sigdigs=3 # significant digits for lattice strips
# plot "appropriately" for atmospheric data: use
# * lattice::levelplot
# * one column, since atmospheric levels stack vertically
# package=latticeExtra makes operator='+' work:
# thanks again, Pascal Oettli
levelplot(
concentration ~ longitude * latitude | level,
data=data.3d.df, layout=c(1,n.lev),
levs=as.character(round(data.3d.df[['level']], 1)),
strip=FALSE, # no strip on top; instead ...
strip.left=strip.custom( # ... draw strip to left of panel
factor.levels= # thanks, David Winsemius
as.character(signif(unique(data.3d.df[['level']]), sigdigs)),
strip.levels=TRUE,
horizontal=TRUE,
strip.names=FALSE,
# gotta shrink strip text size to fit strip width:
# more on that separately
par.strip.text=list(cex=0.5)
)
) + xyplot(lat ~ lon, world.df, type='l', lty=1, lwd=1, col='black')
More information about the R-help
mailing list