Ale*_*ald 12 plot r ggplot2 lattice
有一些数据,我想添加"点画",以显示它"重要",就像他们在IPCC图中所做的那样

目前我正在努力尝试在R中这样做.
如果我编制一些测试数据并绘制它:
data <- array(runif(12*6), dim=c(12,6) )
over <- ifelse(data > 0.5, 1, 0 )
image(1:12, 1:6, data)
Run Code Online (Sandbox Code Playgroud)
我最终要做的是根据当前图像顶部的"over"数组过度绘制一些点.
有什么建议!??
这应该有所帮助 - 我以前做过类似的事情,并写了一个我在这里发布的函数.
#required function from www.menugget.blogspot.com
matrix.poly <- function(x, y, z=mat, n=NULL){
if(missing(z)) stop("Must define matrix 'z'")
if(missing(n)) stop("Must define at least 1 grid location 'n'")
if(missing(x)) x <- seq(0,1,,dim(z)[1])
if(missing(y)) y <- seq(0,1,,dim(z)[2])
poly <- vector(mode="list", length(n))
for(i in seq(length(n))){
ROW <- ((n[i]-1) %% dim(z)[1]) +1
COL <- ((n[i]-1) %/% dim(z)[1]) +1
dist.left <- (x[ROW]-x[ROW-1])/2
dist.right <- (x[ROW+1]-x[ROW])/2
if(ROW==1) dist.left <- dist.right
if(ROW==dim(z)[1]) dist.right <- dist.left
dist.down <- (y[COL]-y[COL-1])/2
dist.up <- (y[COL+1]-y[COL])/2
if(COL==1) dist.down <- dist.up
if(COL==dim(z)[2]) dist.up <- dist.down
xs <- c(x[ROW]-dist.left, x[ROW]-dist.left, x[ROW]+dist.right, x[ROW]+dist.right)
ys <- c(y[COL]-dist.down, y[COL]+dist.up, y[COL]+dist.up, y[COL]-dist.down)
poly[[i]] <- data.frame(x=xs, y=ys)
}
return(poly)
}
#make vector of grids for hatching
incl <- which(over==1)
#make polygons for each grid for hatching
polys <- matrix.poly(1:12, 1:6, z=over, n=incl)
#plot
png("hatched_image.png")
image(1:12, 1:6, data)
for(i in seq(polys)){
polygon(polys[[i]], density=10, angle=45, border=NA)
polygon(polys[[i]], density=10, angle=-45, border=NA)
}
box()
dev.off()
Run Code Online (Sandbox Code Playgroud)

或者,替代"点画":
png("hatched_image2.png")
image(1:12, 1:6, data)
for(i in seq(polys)){
xran <- range(polys[[i]]$x)
yran <- range(polys[[i]]$y)
xs <- seq(xran[1], xran[2],,5)
ys <- seq(yran[1], yran[2],,5)
grd <- expand.grid(xs,ys)
points(grd, pch=19, cex=0.5)
}
box()
dev.off()
Run Code Online (Sandbox Code Playgroud)

在(很晚)对Paul Hiemstra的评论的回应中,这里是另外两个具有更高分辨率矩阵的例子.孵化保持了一个很好的规则模式,但是在分解时看起来并不好看.斑点的例子更好:
n <- 100
x <- 1:n
y <- 1:n
M <- list(x=x, y=y, z=outer(x, y, FUN = function(x,y){x^2 * y * rlnorm(n^2,0,0.2)}))
image(M)
range(M$z)
incl <- which(M$z>5e5)
polys <- matrix.poly(M$x, M$y, z=M$z, n=incl)
png("hatched_image.png", height=5, width=5, units="in", res=400)
op <- par(mar=c(3,3,1,1))
image(M)
for(i in seq(polys)){
polygon(polys[[i]], density=10, angle=45, border=NA, lwd=0.5)
polygon(polys[[i]], density=10, angle=-45, border=NA, lwd=0.5)
}
box()
par(op)
dev.off()
Run Code Online (Sandbox Code Playgroud)
png("stippled_image.png", height=5, width=5, units="in", res=400)
op <- par(mar=c(3,3,1,1))
image(M)
grd <- expand.grid(x=x, y=y)
points(grd$x[incl], grd$y[incl], pch=".", cex=1.5)
box()
par(op)
dev.off()
Run Code Online (Sandbox Code Playgroud)
使用[1]的坐标定位机制来做到这一点?image。
data(volcano)
m <- volcano
dimx <- nrow(m)
dimy <- ncol(m)
d1 <- list(x = seq(0, 1, length = dimx), y = seq(0, 1, length = dimy), z = m)
Run Code Online (Sandbox Code Playgroud)
通过以这种方式构建的“图像”,您可以保持对象的结构及其坐标完好无损。您可以将多个矩阵收集到 3D 数组中或收集为多个元素,但您需要进行扩充image()才能处理该问题,因此我在这里将它们分开。
复制数据以指定感兴趣的区域。
d2 <- d1
d2$z <- d2$z > 155
Run Code Online (Sandbox Code Playgroud)
使用坐标来指定哪些单元格是有趣的。如果你有一个非常大的光栅,这会很昂贵,但它非常容易做到。
pts <- expand.grid(x = d2$x, y = d2$y)
pts$over <- as.vector(d2$z)
Run Code Online (Sandbox Code Playgroud)
设置情节。
op <- par(mfcol = c(2, 1))
image(d1)
image(d1)
points(pts$x[pts$over], pts$y[pts$over], cex = 0.7)
par(op)
Run Code Online (Sandbox Code Playgroud)
不要忘记修改点的绘制以获得不同的效果,特别是具有大量点的非常密集的网格将需要很长时间才能绘制所有这些小圆圈。pch = "."是一个不错的选择。
现在,您有一些真实数据可以绘制在那个漂亮的投影上吗?有关某些选项,请参阅此处的示例:http://spatial-analyst.net/wiki/index.php ?title=Global_datasets
[1] R 具有用于更复杂地处理栅格数据的类,请参阅包 sp 和 raster 了解两种不同的方法。