如何修改R中的多边形spplot的框架或填充?
问题描述:
我正在使用spplot
来绘制一幅等值线图。我的问题是绘图框架在地图上默认绘制(请参见下面的示例代码)。我想完全移除框架,但我不能使用选项trellis.par.set(axis.line=list(col=NA))
,因为它也会遗漏图例的轴线。为了清晰地读取比例尺,需要图例线条,并且我的调色板还包括需要加框的白色。如何修改R中的多边形spplot的框架或填充?
作为速战速决,我尝试添加在spplot框架(白色)多边形,但它尽管使用选项under=F
框架下拉伸:
#Plotting dummy polygons
library(sp)
p1 <- Polygons(list(Polygon(cbind(c(13,15,20,20), c(8,13,14,8)))), "1")
p2 <- Polygons(list(Polygon(cbind(c(20,20,28,30), c(8,15,12,7)))), "2")
p3 <- Polygons(list(Polygon(cbind(c(15,20,25,22,18), c(8,8,7.5,0,2)))), "3")
polys <- SpatialPolygons(list(p1,p2,p3))
spdf <- SpatialPolygonsDataFrame(polys, data.frame(var1=c(1,4,8)))
spplot(spdf, col.regions=topo.colors(16))
#Adding an extra polygon to try to cover the frame
frame <- extent(spdf)
spplot(spdf, col.regions=topo.colors(16)) +
layer(sp.polygons(as(frame,'SpatialPolygons'), lwd=3, col=3, fill=NA), under=F)
#(using green here to actually see the box)
第二个最好的解决办法是将绘制远离地图的框架。不幸的是,我只找到如何修改外边距的说明,例如trellis.par.set(layout.heights=list(top.padding=3,bottom.padding=3))
,但我不知道如何控制边距(如果spplot
图中有这样的事情)。
任何帮助修改框架或spplot
内边距将不胜感激!
答
溶液1:设定情节面板的轮廓透明,同时保持颜色键的轮廓黑色
spplot(spdf, col.regions=topo.colors(16),
par.settings = list(axis.line = list(col = "transparent")),
colorkey = list(axis.line = list(col = "black")))
溶液2:填充的xlim
& ylim
spplot(spdf, col.regions=topo.colors(16),
xlim = c([email protected]["x", "min"] - 3,
[email protected]["x", "max"] + 3),
ylim = c([email protected]["y", "min"] - 3,
[email protected]["y", "max"] + 3))
范围
非常好,两种解决方案都像魅力一样工作!谢谢! –