这是我的 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
我已经尝试过this question给出的答案但这没有用。
最佳答案
在绘图之前将 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)
})
关于r - 使用 gWidgets2 显示图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20478142/