Frage

I'm building a GUI using gWidgetsRGtk2 and am having trouble making a ggplot appear on the click of a gbutton. The plotting function works by itself (i.e. when you type "plotData()"), but I can't get it to work with gWidgets. Is there some compatibility issue with gWidgets and ggplot2, or am I not calling the function correctly? I've stripped everything else from the GUI to make the code shorter and simpler. Here's what the plot should look like:

fake isotope data

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
)
War es hilfreich?

Lösung

When using RGui you should change ggplot(...) to print(ggplot(...)) in your plotData function. However, that is not enough when using R Studio and you should also type a blank line, i.e. press 'enter' in the console after clicking the 'Plot data' button.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top