[英]Single colorkey for raster and points Levelplot R
Using the sample data below, how can I generate rasters and spatial points plot with the same colorkey as in the "manually" joined plot shown below? 使用下面的示例数据,如何使用与下面显示的“手动”连接的图相同的色键生成栅格和空间点图?
library(rasterVis)
library(raster)
library(colorRamps)
col=colorRampPalette(matlab.like2(255))
s <- stack(replicate(2, raster(matrix(runif(100), 10))))
xy <- data.frame(coordinates(sampleRandom(s, 10, sp=TRUE)),
z1=runif(10), z2=runif(10))
levelplot(s, margin=FALSE, at=seq(0, 1, 0.05),col.regions=col)
x=xy$x;y=xy$y;z=xy$z1
levelplot(z ~ x + y,contour=F, panel = panel.levelplot.points,
margin=FALSE,col.regions=col,
par.settings=list(axis.line=list(lwd=3), strip.border=list(lwd=3)),
cex=1.4, scales=list(x=list(cex=1.7),y=list(cex=1.7)),xlab=list(label="Longitude",cex=2),
ylab=list(label="Latitude",cex=2))
Thanks to @fdestch I was able to generate the following plot using: 感谢@fdestch,我能够使用以下代码生成以下图:
latticeCombineGrid(mget(rep("pp", 24)), layout = c(3, 8))
following my comments on printing multiple plots with the same colorkey. 以下是我关于使用同一色键打印多个图的评论。
An issue that remains to be clarified: 有待澄清的问题:
1) How can one decide on the order of panels? 1)如何确定面板的顺序? That is, which row & column to place a particular plot just as in
levelplot
using index.cond
. 也就是说,使用
index.cond
像在levelplot
一样放置特定图的行和列。
First of all, you should probably make sure that the breaks in the points plot are identical with those defined in the first levelplot
. 首先,您可能应该确保点图中的中断与第一个
levelplot
定义的中断相同。
## raster plot with colorkey disabled
pr <- levelplot(s, margin = FALSE, at = seq(0, 1, 0.05), col.regions = col,
colorkey = FALSE, xlab = list("Longitude", col = "transparent"))
## points plot
pp <- levelplot(z ~ x + y, panel = panel.levelplot.points, cex = 1.4,
contour = FALSE, margin = FALSE, col.regions = col,
colorkey = list(at = seq(0, 1, .05), width = .6, height = .6),
xlab = "Longitude", ylab = "Latitude")
Please note the definition of a transparent xlab
when creating the raster plot. 创建栅格图时,请注意透明
xlab
的定义。 This little workaround comes in quite handy when using downViewport
later on to ensure that the actual plot boundaries of pr
and pp
overlap (feel free to run grid.rect()
right after print(pr, newpage = FALSE)
to see what I mean). 稍后使用
downViewport
来确保pr
和pp
的实际绘图边界重叠时,此小解决方法非常方便grid.rect()
在print(pr, newpage = FALSE)
之后立即运行grid.rect()
print(pr, newpage = FALSE)
即可查看我的意思) 。
The actual plot arrangement can then easily be achieved by using viewports from the grid package. 然后,可以通过使用网格包中的视口轻松地实现实际的绘图布置。
library(grid)
library(lattice)
## initialize new grid device
grid.newpage()
## add raster plot
vp1 <- viewport(x = 0, y = 0, width = .5, height = 1,
just = c("left", "bottom"))
pushViewport(vp1)
print(pr, newpage = FALSE)
## add points plot
downViewport(trellis.vpname("page"))
vp2 <- viewport(x = 1, y = 0, width = .75, height = 1,
just = c("left", "bottom"))
pushViewport(vp2)
print(pp, newpage = FALSE)
Here is my solution using latticeExtra::c.trellis
: 这是我使用
latticeExtra::c.trellis
解决方案:
library(raster)
library(rasterVis)
s <- stack(replicate(2, raster(matrix(runif(100), 10))))
xy <- data.frame(coordinates(sampleRandom(s, 10, sp=TRUE)),
z1=runif(10), z2=runif(10))
## Define theme and breaks
myTheme <- BTCTheme()
my.at <- seq(0, 1, 0.05)
Plot the Raster*
object, using rasterVis::levelplot
: 使用
rasterVis::levelplot
绘制Raster*
对象:
p1 <- levelplot(s, margin=FALSE, at = my.at, par.settings = myTheme)
Plot the points, using lattice::levelplot
: 使用
lattice::levelplot
绘制点:
p2 <- levelplot(z1 ~ x + y, data = xy, at = my.at, panel = panel.levelplot.points, par.settings = myTheme)
Join them with latticeExtra::c.trellis
: 加入他们与
latticeExtra::c.trellis
:
p3 <- c(p1, p2, layout = c(3, 1))
Unfortunately, c.trellis
does not assign the strip labels correctly, so you have to define them directly: 不幸的是,
c.trellis
没有正确分配带状标签,因此您必须直接定义它们:
update(p3, strip = strip.custom(factor.levels = c(names(s), "Points")))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.