3

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

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

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

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

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

如果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)


这是使用上述代码的预期图(左)和实际图(右)的示例。
预期和实际结果
如果有人对如何解决这个问题或光栅堆栈的任何替代绘图选项有任何想法,我很想听听他们的意见。(请注意,如果我在 GUI 中

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

4

1 回答 1

0

给那些可能感兴趣的人。经过 3.5 年和许多试验,包括recordPlot()gridGraphicsimager::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])

在此处输入图像描述

于 2017-11-22T04:23:43.200 回答