使用 plot(...,add=T) 叠加光栅图会导致最终图的任意错位 [英] Overlay raster plot using plot(...,add=T) leads to arbitrary misalignment of final plot

查看:53
本文介绍了使用 plot(...,add=T) 叠加光栅图会导致最终图的任意错位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现,当我尝试使用 plot(...,add=T) 覆盖多个栅格时,如果我尝试将 3 个以上的栅格重叠在一起,则后续绘图无法正确对齐栅格.

I have found that when I try to overlay multiple rasters using plot(...,add=T) if I try to overlay more than 3 rasters together the subsequent plot does not align the rasters properly.

我的初衷是创建模拟土地覆盖的分类地图,其中代表覆盖类别的颜色的暗度因我们的模型投影的确定性而变化.为此,我创建了一个简单的脚本,该脚本将遍历每个覆盖类并使用从灰色(低确定性森林预测)到完整覆盖颜色(例如,深绿色)的颜色渐变绘制它(例如,森林,地图上的绿色)因为区域被强烈预测).我发现使用这种方法,在将第三个封面添加到绘图后,叠加在绘图上的所有后续栅格都任意错位.我已经颠倒了封面类的绘制顺序,并且表现出相同的行为,这意味着这不是单个封面类栅格的问题.更令人费解的是,在 Rstudio 中,当我使用缩放按钮仔细检查最终绘图时,错位更加严重.

My original intent was to create a categorical map of modeled landcover where the darkness of the color representing a cover class varied wrt the certainty in our model projection. To do this, I created a simple script that would loop through each cover class and plot it (e.g., forest, green color on map) using a color gradient from grey (low certainty forest prediction) to full cover color (e.g., dark green for areas are strongly predicted). What I have found is that using this approach, after the 3rd cover is added to the plot, all subsequent rasters that are overlayed on the plot are arbitrarily misaligned. I have reversed the order of plotting of the cover classes and the same behavior is exhibited meaning it is not an issue with the individual cover class rasters. Even more puzzling in Rstudio, when I use the zoom button to closely inspect the final plot, the misalignment worsens.

您对这种行为存在的原因有什么想法吗?最重要的是,您有任何建议的解决方案或变通方法吗?

Do you have any ideas of why this behavior exists? Most importantly, do you have any suggested solutions or workarounds?

以下链接中的代码和数据捕获了所描述的所有行为.https://dl.dropboxusercontent.com/u/332961/r%20plot%20raster%20add%20issue.zip转动 plot_gradient=F 以查看如果您只是简单地对同一栅格进行子集化并将子集按顺序添加到同一图中,您可以复制该问题.我已经尝试设置绘图设备 plot(..., ext) 的范围,但这没有用.我也检查过,每个覆盖光栅的范围都是一样的.

The code and data on the link below has all of the behaviors described captured. https://dl.dropboxusercontent.com/u/332961/r%20plot%20raster%20add%20issue.zip Turn plot_gradient=F to see how if you just simply subset a same raster and add the subsets sequentially to the same plot you can replicate the issue. I have already tried setting the extent of the plot device plot(..., ext) but that did not work. I have also checked and the extent of each cover raster is the same.

下图是未对齐的封面类别.绘制到 jpeg 设备将产生类似的图像(即,这不是 Rstudio 渲染的问题).奇怪的是,如果我使用 Rstudio 放大图像,错位是不同的为了进行比较,这就是封面在景观中应该如何正确对齐

Below is the figure of the misaligned cover classes. plotting to jpeg device will result in a similar image (i.e., this is not an issue of Rstudio rendering). Strangely enough, if I zoom into the image using Rstudio, the misalignment is different For comparison, this is how the covers should align correctly in the landscape

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() 调用之前.例如:

I had this problem too and solved it by calling the graphical parameters function, par(), with a subset of parameters, and most importantly, put the new=TRUE in the par() call, not the plot() call, before each additional plot() call. For example:

  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)

这篇关于使用 plot(...,add=T) 叠加光栅图会导致最终图的任意错位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆