简体   繁体   English

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

[英]R- spplot not plotting raster stack in gWidgets GUI

I have been building a small GUI for climate analysis using gWidgets in R. Progress has been slow but steady until I hit a problem trying to display my raster stack of results using spplot() . 我一直在使用g中的gWidgets构建一个用于气候分析的小GUI。进度一直缓慢但稳定,直到我遇到一个问题,试图使用spplot()显示我的栅格堆栈结果。 The issue is that only the first raster in the stack is plotted and the rest are not. 问题是只绘制了堆栈中的第一个栅格,其余的则没有。 This issue occurs regardless if: 无论是否发生此问题:

  1. I produce the plot using a handler within the GUI. 我使用GUI中的处理程序生成绘图。

  2. If the plot is produced using a handler within a addHandlerChanged / addHandlerClicked function. 如果使用addHandlerChanged / addHandlerClicked函数中的处理程序生成绘图。

  3. If the plot is loaded to the GUI directly from the R console. 如果绘图直接从R控制台加载到GUI。

  4. As above but using using levelplot() . 如上所述,但使用levelplot()

If plot() is used, results are displayed correctly but only the first 16 are displayed (I have 24 graphs) and the scales are not merged producing difficulty in interpreting the results. 如果使用plot() ,结果会正确显示,但只显示前16个(我有24个图表),并且未合并比例,难以解释结果。

Here is some example code to illustrate the issue: 以下是一些示例代码来说明问题:

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)


Here is an example of the expected plot (left) and the actual (right) using the above code. 以下是使用上述代码的预期图(左)和实际(右)的示例。
预期和实际结果
If anybody has any ideas how to get around this or any alternative plotting options for raster stacks I would love to hear them. 如果有人有任何想法如何绕过这个或任何替代光栅堆栈的绘图选项,我很乐意听到它们。

(please note that similar results are produced if I open a separate window using windows() within the GUI or if I use levelplot() ) (请注意,如果我在GUI中使用windows()打开一个单独的窗口,或者如果我使用levelplot() ),会产生类似的结果

Cheers 干杯

To those who may be interested. 对那些可能感兴趣的人。 After 3.5 years and a many trials, including recordPlot() , the gridGraphics package and imager::capture.plot() , the only solution that I found was to save the graph as an image and then plot it in the window using rasterImage() 经过3。5年和许多试验,包括recordPlot()gridGraphics包和imager::capture.plot() ,我发现的唯一解决方案是将图形保存为图像,然后使用rasterImage()将其绘制在窗口中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])

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM