我发现,当我尝试使用 plot(...,add=T) 覆盖多个栅格时,如果我尝试将 3 个以上的栅格重叠在一起,则后续绘图不会正确对齐栅格。
我的初衷是创建一个模型化土地覆盖的分类 map ,其中代表覆盖类别的颜色的深浅会随着我们模型投影的确定性而变化。为此,我创建了一个简单的脚本,该脚本将遍历每个覆盖类并使用从灰色(低确定性森林预测)到完整覆盖颜色(例如,深绿色)的颜色渐变绘制它(例如,森林, map 上的绿色)因为区域被强烈预测)。
我发现使用这种方法,在将第三个封面添加到绘图后,叠加在绘图上的所有后续栅格都任意错位。我已经颠倒了封面类的绘制顺序,并且表现出相同的行为,这意味着这不是单个封面类栅格的问题。在 Rstudio 中更令人费解的是,当我使用缩放按钮仔细检查最终绘图时,错位会恶化。
您对这种行为为何存在有任何想法吗?最重要的是,您有任何建议的解决方案或解决方法吗?
下面链接上的代码和数据捕获了所描述的所有行为。
https://dl.dropboxusercontent.com/u/332961/r%20plot%20raster%20add%20issue.zip
转动 plot_gradient=F 以查看如果您只是简单地对同一栅格进行子集化并将子集按顺序添加到同一图中,您可以复制该问题。我已经尝试设置绘图设备 plot(..., ext) 的范围,但这没有用。我也检查过,每个覆盖光栅的范围都是一样的。
下图是未对齐的封面类图。绘制到 jpeg 设备将产生类似的图像(即,这不是 Rstudio 渲染的问题)。
奇怪的是,如果我使用 Rstudio 放大图像,错位是不同的
为了进行比较,这就是封面在景观中应该如何正确对齐
library(raster)
library(colorRamps)
raster_of_classes=raster("C:/r plot raster add issue/raster_of_classes.tif")
raster_of_certainty_of_classes=raster("C:/r plot raster add issue/raster_of_certainty_of_classes.tif")
endCols=c("darkorchid4", "darkorange3", "red3", "green4", "dodgerblue4") #colors to be used in gradients for each class
classes=unique(raster_of_classes)
minVal=cellStats(raster_of_certainty_of_classes, min)
tmp_i=1
addPlot=F
plot_gradient=F #this is for debug only
#classes=rev(classes) #turn this off and on to see how last 2 classes are mis aligned, regardless of plotting order
for (class in classes){
raster_class=raster_of_classes==class #create mask for individual class
raster_class[raster_class==0]=NA #remove 0s from mask so they to do not get plotted
if (plot_gradient){
raster_of_certainty_of_class=raster_of_certainty_of_classes*raster_class #apply class mask to certainty map
}else{
raster_of_certainty_of_class=raster_class #apply class mask to certainty map
}
endCol=endCols[tmp_i] #pick color for gradient
col5 <- colorRampPalette(c('grey50', endCol))
if (plot_gradient){
plot(raster_of_certainty_of_class,
col=col5(n=49), breaks=seq(minVal,1,length.out=50), #as uncertainty values range from 0 to 1 plot them with fixed range
useRaster=T, axes=FALSE, box=FALSE, add=addPlot, legend=F)
}else{
plot(raster_of_certainty_of_class,
col=endCol,
useRaster=T, axes=FALSE, box=FALSE, add=addPlot, legend=F)
}
tmp_i=tmp_i+1
addPlot=T #after plotting first class, all other classes are added
}
最佳答案
我也遇到了这个问题,通过调用图形参数函数 par() 和参数子集来解决它,最重要的是,把 new=TRUE
在 par() 调用中,而不是在 plot() 调用中,在每个附加的 plot() 调用之前。例如:
png(fullname,
width = 3000,
height= 3000)
# original par() call
par(mfrow=c(1,1), cex=3, mar=c(3,3,3,7), bg=bgcol, col=txtcol)
# first plot
plot(zreate,
maxpixels=ncell(zreate),
col=qcol,
colNA=mapbg,
xaxt='n',
yaxt='n',
ext=map_extent,
breaks=tq,
bty='n',
legend=FALSE)
#second plot and par() call
par(mfrow=c(1,1), cex=3, mar=c(3,3,3,7), bg=bgcol, col=txtcol, new=TRUE)
plot(rt,
maxpixels=ncell(rt),
col=dcol,
legend=FALSE,
xaxt='n',
yaxt='n',
ext=map_extent,
bty='n')
#third plot and par() call
par(mfrow=c(1,1), cex=3, mar=c(3,3,3,7), bg=bgcol, col=txtcol, new=TRUE)
plot(r0,
maxpixels=ncell(r0),
col="#9e9ac8",
xaxt='n',
yaxt='n',
ext=map_extent, #PRENAFILTERING fix
bty='n',
legend=FALSE)
关于r - 使用 plot(...,add=T) 叠加光栅图会导致最终图的任意错位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24213453/