我需要将不同地理范围的地图导出为不同大小的 png 文件(用于网页显示、打印等)。这要求我的地图绘制功能具有高度的灵活性,而我只是无法让所有参数都对齐。几年前就开始用lattice写函数了,已经很精细了,目前还没有使用ggplot2的经验。所以我更愿意暂时留在格子里。
我的问题是:
我希望我设法使问题变得清晰(代码示例可能会有所帮助),并且非常感谢各位专家的任何提示。谢谢。
干杯, 约翰
# Argument
width <- 600 # PNG width in px
require(lattice)
# Load field
field <- volcano
west <- 1
east <- ncol(field)
south <- 1
north <- nrow(field)
# Color key
breaks <- pretty(field)
cols <- terrain.colors(length(breaks) - 1)
colorkey <- list(space="bottom",
col=cols)
# Layout settings
par.settings <- list(layout.heights=list(top.padding=0,
main.key.padding=0,
key.axis.padding=0,
xlab.key.padding=0,
key.sub.padding=0,
bottom.padding=3),
layout.widths=list(left.padding=0,
key.ylab.padding=0,
ylab.axis.padding=0,
axis.key.padding=0,
right.padding=0))
# Panel function
panel <- function(...){
panel.levelplot(...)
panel.xyplot(x=c(10,20,30),y=c(10,20,30),pch=1)
panel.text(east,south,labels="\u00a9 by me",adj=c(1,0))
}
# Adaptive resolution?
res <- round(width / 9)
# Adaptive pointsize?
# PNG height wrt PNG width plus space for colorkey?
aspect <- nrow(field) / ncol(field)
height <- round(width * aspect)
# Open PNG device
png(filename="test.png",width=width,height=height,units="px",res=res,
type="cairo")
# Draw map
print(levelplot(x=field,
row.values=seq(south,north),
column.values=seq(west,east),
at=breaks,
col.regions=cols,
scales=list(draw=F),
colorkey=colorkey,
xlab="",
xlim=c(west,east),
ylab="",
ylim=c(south,north),
panel=panel,
par.settings=par.settings))
# Close png device
dev.off()