Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
levelplot:如何在colorkey和x轴标签之间添加空格_R_Lattice_Levelplot_Color Key - Fatal编程技术网

levelplot:如何在colorkey和x轴标签之间添加空格

levelplot:如何在colorkey和x轴标签之间添加空格,r,lattice,levelplot,color-key,R,Lattice,Levelplot,Color Key,我正在尝试使用levelplot绘制一个简单的数字高程模型(DEM) 这是我的密码: r1 = raster("ned10dem.tif") e = extent(460000,480000,4555000,4567500) rr1 = crop(r1,e) p = levelplot(rr1, scales=list(x=list(at=seq(450000,480000,4000))), margin=F, cuts=200, col.

我正在尝试使用
levelplot
绘制一个简单的数字高程模型(DEM)

这是我的密码:

r1 = raster("ned10dem.tif")
e = extent(460000,480000,4555000,4567500)
rr1 = crop(r1,e)
p = levelplot(rr1, scales=list(x=list(at=seq(450000,480000,4000))),
              margin=F, cuts=200,
              col.regions = terrain.colors(350,alpha=1), 
              colorkey=list(space="bottom"),
              xlab="Easting(m)", ylab="Northing(m)")
plot(p)
情节最终如下所示:


我无法理解的是如何增加colorkey和x轴之间的空间,以使colorkey不覆盖x轴标签

一个快速解决方法是在x标签上添加一行新行,如下所示:
xlab=“Easting(m)\n”
。这将在x标签和图例之间添加一条空行。

添加以下内容:

par.settings = list(layout.heights=list(xlab.key.padding=1))
测试示例:

x <- seq(pi/4, 5*pi, length.out=100)
y <- seq(pi/4, 5*pi, length.out=100)
r <- as.vector(sqrt(outer(x^2, y^2, "+")))
grid <- expand.grid(x=x, y=y)
grid$z <- cos(r^2) * exp(-r/(pi^3))
p <- levelplot(z~x+y, data=grid,
               margin=F, cuts=200,
               par.settings=list(layout.heights=list(xlab.key.padding=1)),
               col.regions=terrain.colors(350, alpha=1), 
               colorkey=list(space="bottom"),
               xlab="Easting(m)", ylab="Northing(m)")
print(p)

x另一个选项是定义栅格视口,并使用
draw.colorkey
手动插入colorkey。基于@rcs建议的解决方案,代码大致如下所示

library(grid)

## breaks and colors
at <- seq(-1.1, 1.1, .01)
cols <- terrain.colors(350)

## create plot
p <- levelplot(z ~ x + y, data = grid, at = at,
               col.regions = cols, colorkey = FALSE,
               xlab = list("Easting (m)", cex = .8), 
               ylab = list("Northing (m)", cex = .8))

## start png device
png("~/plot.png", width = 8, height = 9, units = "cm", res = 150)

## insert plot
grid.newpage()
vp_fig <- viewport(x = 0, y = .1, width = 1, height = .9, 
                   just = c("left", "bottom"))
pushViewport(vp_fig)
print(p, newpage = FALSE)

## insert colorkey
downViewport(trellis.vpname("figure"))
vp_key <- viewport(x = .5, y = -.4)
pushViewport(vp_key)
draw.colorkey(key = list(col = cols, at = at, width = .6, height = .6, 
                         space = "bottom"), draw = TRUE)
dev.off()
库(网格)
##中断和颜色
在