R-spplot不在gWidgets GUI中绘制栅格堆栈

时间:2014-01-09 21:15:24

标签: r user-interface raster gwidgets

我一直在使用g中的gWidgets构建一个用于气候分析的小GUI。进度缓慢但稳定,直到我尝试使用spplot()显示结果的栅格堆栈时出现问题。 问题是只绘制了堆栈中的第一个栅格,其余的则没有。无论是否出现此问题:

  1. 我使用GUI中的处理程序生成绘图。

  2. 如果使用addHandlerChanged / addHandlerClicked函数中的处理程序生成图表。

  3. 如果绘图直接从R控制台加载到GUI。

  4. 如上所述,但使用levelplot()

  5. 如果使用plot(),结果会正确显示,但只显示前16个(我有24个图表)并且未合并比例,难以解释结果。

    以下是一些示例代码来说明问题:

    require(gWidgets)
    require(raster)
    
    ## create example GUI plot area
    win = gwindow("Graph test")
    nb = gnotebook(container=win,expand=T)
    plots = ggraphicsnotebook(container=nb)
    
    ## create raster stack
    rs=list()
    for(i in 1:24){
      rs1=raster()
      rs1[]=rnorm(3600)
      rs[i]=rs1
    }
    rs=stack(rs)
    
    ## attempt to plot stack
    spplot(rs) ##plot is not produced correctly with only the first raster plotted
    
    ##compare this to plotting in a normal window
    windows()
    spplot(rs)
    


    以下是使用上述代码的预期图(左)和实际(右)的示例。
    Expected and actual results
    如果有人有任何想法如何绕过这个或任何替代光栅堆栈的绘图选项,我很乐意听到它们。

    (请注意,如果我在GUI中使用windows()打开一个单独的窗口,或者我使用levelplot()),则会产生类似的结果
    干杯

1 个答案:

答案 0 :(得分:0)

对可能感兴趣的人。经过3。5年和许多试验,包括recordPlot()gridGraphics包和imager::capture.plot(),我找到的唯一解决方案是将图表保存为图像,然后在窗口中绘制它使用rasterImage()

require(gWidgets)
require(gWidgetsRGtk2)
require(RGtk2)
require(raster)
require(png)

options(guiToolkit="RGtk2") 

## create raster stack
rs=list()
for(i in 1:24){
  rs1=raster(nrow=2,ncol=2)
  rs1[]=rnorm(4)
  rs[i]=rs1
}
rs=stack(rs)

##save plot as png
png("out.png")
spplot(rs)
dev.off()
img = readPNG("out.png")


## create example GUI plot area
win = gwindow("Graph test")
nb = gnotebook(container=win,expand=T)
plots = ggraphicsnotebook(container=nb)

##plot
par(mar=rep(0,4))
plot(1, type="n", axes=F, xlab="", ylab="")
usr = par("usr")    
rasterImage(img, usr[1], usr[3], usr[2], usr[4])

enter image description here