2

这是我的 GUI“标题”:

library(gWidgets2RGtk2)
library(cairoDevice)
library(ggplot2)
WINGRAPH0 <- gwindow("")
WINGRAPH <- gvbox(container=WINGRAPH0)

以下代码不起作用:

gnb <- gnotebook(container=WINGRAPH)
ggraph <- ggraphics(container=gnb)
ggplot(cars, aes(x=speed, y=dist)) + geom_point()

它给:

Error in UseMethod("depth") : 
  no applicable method for 'depth' applied to an object of class "NULL"

但是,如果我首先在图形笔记本中显示图像文件,则效果很好:

gnb <- gnotebook(container=WINGRAPH)
gimage("plot1.png", container=gnb)
ggraph <- ggraphics(container=gnb)
ggplot(cars, aes(x=speed, y=dist)) + geom_point()

在第一个代码中,如果我使用经典图而不是 ggplot(例如plot(0,0)),我会得到:

Error in plot.new() : figure margins too large

我已经尝试了这个问题的答案,但没有奏效。

4

1 回答 1

2

在绘图前设置visible为 FALSE:

library(gWidgets2RGtk2)
library(cairoDevice)

w <- gwindow("notebook example")
nb <- gnotebook(cont=w)
gg <- ggraphics(cont=nb, label='1',visible=FALSE)
library(ggplot2)
ggplot(cars, aes(x=speed, y=dist)) + geom_point()
visible(gg) <- TRUE

在此处输入图像描述

EIDT

w <- gwindow("notebook example")
nb <- gnotebook(cont=w)
devs <- lapply(1:2, function(i) 
    ggraphics(cont=nb,visible=FALSE, label=as.character(i)))

addHandlerChanged(nb, handler=function(h,...) {
    gg <- h$obj[h$page.no]
    visible(gg) <- TRUE
    if(h$page.no =="1")
        print(ggplot(cars, aes(x=speed, y=dist)) + geom_point())
    else    plot(0)
})
于 2013-12-09T19:30:22.087 回答