Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在gWidgetsRGtk2中使用ggplot2_R_Ggplot2_Gtk2_Gwidgets - Fatal编程技术网

在gWidgetsRGtk2中使用ggplot2

在gWidgetsRGtk2中使用ggplot2,r,ggplot2,gtk2,gwidgets,R,Ggplot2,Gtk2,Gwidgets,我正在使用gWidgetsRGtk2构建一个GUI,但在单击gbutton时无法显示ggplot。plotting函数本身可以工作(即键入“plotData()”),但我无法让它与gWidget一起工作。gWidgets和ggplot2是否存在兼容性问题,或者我是否没有正确调用该函数?我已经从GUI中剥离了所有其他内容,以使代码更短、更简单。下面是情节应该是什么样的: library(ggplot2) library(gWidgets) library(gWidgetsRGtk2) requi

我正在使用gWidgetsRGtk2构建一个GUI,但在单击gbutton时无法显示ggplot。plotting函数本身可以工作(即键入“plotData()”),但我无法让它与gWidget一起工作。gWidgets和ggplot2是否存在兼容性问题,或者我是否没有正确调用该函数?我已经从GUI中剥离了所有其他内容,以使代码更短、更简单。下面是情节应该是什么样的:

library(ggplot2)
library(gWidgets)
library(gWidgetsRGtk2)

require(gWidgets)
options("guiToolkit"="RGtk2")

X<-cbind(runif(120,min=-22,max=-17),runif(120,min=6,max=12))
MU<-matrix(c(-18.96,-15.86,-24.67,4.04,13.57,9.69),nrow=3,ncol=2)
SIG<-matrix(c(0.6,0.77,0.85,0.85,0.55,0.90),nrow=3,ncol=2)

plotData = function(h,...)
{
C_err <- 0.3
N_err <- 1.0
df <- data.frame(x = X[,1],
    y = X[,2],
    ymin = X[,2] - N_err,
    ymax = X[,2] + N_err,
    xmin = X[,1] - C_err,
    xmax = X[,1] + C_err)

df_MU <- data.frame(x=MU[,1], y=MU[,2], 
    ymin = MU[,2] - SIG[,2],
    ymax = MU[,2] + SIG[,2],
    xmin = MU[,1] - SIG[,1],
    xmax = MU[,1] + SIG[,1])

ggplot(data = df,aes(x = x,y = y)) + 
  geom_point() + 
  geom_errorbar(aes(ymin = ymin,ymax = ymax)) + 
  geom_errorbarh(aes(xmin = xmin,xmax = xmax)) +
  geom_pointrange(data=df_MU,aes(ymin=ymin,ymax=ymax),colour=c('red','blue','green'),size=1) +
  geom_errorbarh(data=df_MU,aes(xmin=xmin,xmax=xmax),colour=c('red','blue','green'),size=1,height=0) +
  ggtitle("Isotope Data") +
  ylab("d15N (%)") +
  xlab("d13C (%)") +
  theme_bw()
}

win<-gwindow()
grp_all <- ggroup(container=win, horizontal=F)
plot_button <- gbutton(
  text = "Plot data",
  container = grp_all,
  expand = TRUE,
  handler = plotData
)

库(ggplot2)
库(gWidgets)
库(gWidgetsRGtk2)
require(gWidgets)
选项(“guiToolkit”=“RGtk2”)

使用RGui时,应在
plotData
函数中将
ggplot(…)
更改为
print(ggplot(…)
。但是,在使用R Studio时,这还不够,您还应键入一个空行,即在单击“绘图数据”按钮后在控制台中按“回车”。

R Studio存在一些问题。在将
ggplot(…)
更改为
print(ggplot(…)
后,我可以使用RGui,但不能使用R Studio。谢谢,我现在可以使用了!我是从记事本上复制粘贴的,不是用R StudioYes。Julius的建议奏效了——将plotData函数中的ggplot(…)调用更改为print(ggplot(…)。也许它应该作为一个答案而不是评论发布。在回答之前,我只想为R Studio找到一个解决方案。。尽管这个一点也不方便。