### Function to plot a scatterplot with zoom gui ### ScatterZoom <- function(x,y,...){ # Load 'rpanel': if (!require("rpanel")) stop("Package 'rpanel' is required!") # Open new plot window: if (any(grepl("RStudio", .libPaths(), ignore.case=TRUE))) { if (grepl("win",Sys.info()["sysname"],ignore.case=TRUE)) { windows() } else X11() } else { dev.new() } ### 'rpanel' functions ### # Function to plot: DrawFun <- function(panel) { do.call(plot,c(panel[c("x","y")],list(xlim=c(panel$Xmin,panel$Xmax),ylim=c(panel$Ymin,panel$Ymax)),panel$args)) return(panel) } # Function to set up parameters without plotting (unless on the fly is enabled): SetupFun <- function(panel) { if (isTRUE(panel$OnTheFly)) DrawFun(panel) return(panel) } # Function to save plot with same dimensions: SaveFun <- function(panel) { pdf(width=par("din")[1],height=par("din")[2]) DrawFun(panel) dev.off() return(panel) } ### Create panel ### panel <- rp.control(x=x,y=y,args=list(...),aschar=FALSE) ### Small fix to fix xlab and ylab: if (is.null(panel$args$xlab)) panel$args$xlab <- deparse(substitute(x)) if (is.null(panel$args$ylab)) panel$args$ylab <- deparse(substitute(y)) ### Create GUI parts ### # On the fly plotting checkbox: rp.checkbox(panel, OnTheFly , SetupFun, title="Plot on the fly", pos = list(column=0,row=0), initval=FALSE) # Minimum of X slider: rp.slider(panel, Xmin, min(x), max(x) , SetupFun, "Minimum X", initval = min(x), showvalue = TRUE, pos = list(column=0,row=1)) # Maximum of X slider: rp.slider(panel, Xmax, min(x), max(x) , SetupFun, "Maximum X", initval = max(x), showvalue = TRUE, pos = list(column=1,row=1)) # Minimum of Y slider: rp.slider(panel, Ymin, min(y), max(y) , SetupFun, "Minimum Y", initval = min(y), showvalue = TRUE, pos = list(column=0,row=2)) # Maximum of Y slider: rp.slider(panel, Ymax, min(y), max(y) , SetupFun, "Maximum Y", initval = max(y), showvalue = TRUE, pos = list(column=1,row=2)) # Plot button: rp.button(panel, action = DrawFun, title = "Plot", ,pos = list(column=0,row=3)) # Save as PDF button: rp.button(panel, action = SaveFun, title = "Save", pos = list(column=1,row=3)) }