1

I have the following:

require(ggplot2)

p <- ggplot()   # don't put aes() here, do it separately

df <- data.frame(x=X, y=Y)    # ggplot requires dataframes
p <- p + geom_line(data=df, aes(x=X, y=Y))

# Now we plot the graphs using ggplots and color the segments
p <- p + geom_rect(data=peaks,
                   aes(xmin=xstart, xmax=xend,
                       ymin = -Inf, ymax = Inf,
                       fill = col),
                   alpha=0.4)

# Open the graphical window for displaying the graph
quartz()
print(p)
message("Press Return To Continue")
invisible(readLines("stdin", n=1))

The last line ensures that my graphing window doesn't open and shut as the script terminates. This works fine, except that I can't resize the graphing window when it shows up. My cursor is spinning indefinitely.

Aside from manually specifying the window frame in quartz(), is there any way for me to resize my graphing window?

Chris Watson
  • 1,347
  • 1
  • 9
  • 24
Tinker
  • 4,165
  • 6
  • 33
  • 72

1 Answers1

2

There's a hint to this here.

Instead of using readLines() to keep the R script waiting, use locator() (for base plots) or grid::grid.locator() (for ggplot2). This will not only keep the script running but also keep the plot window active and resizable. On windows, and this works for me:

library(ggplot2)

X11() #quartz() on OSX
ggplot(mtcars, aes(mpg, wt)) + geom_line()
grid::grid.locator()

Or

X11() #quartz() on OSX
plot(mtcars$mpg, mtcars$wt)
locator()

The particular difference between locator() and grid::grid.locator() is that the first will let you select any number of points, and the second just one, which can lead to having the plot closed if you miss click. You can find alternatives to that here, or maybe just use something like replicate(10, grid::grid.locator()).

Edit

Here's a modification which should fix both problems mentioned in the comments.

Instead of readLines() or locator(), we can make the system sleep while the graphics device is active. Since the device should be changed back to null device once the window is closed, we can use that as our condition:

library(ggplot2)

X11() #quartz() on OSX
ggplot(mtcars, aes(mpg, wt)) + geom_line()

while(dev.cur() != 1) Sys.sleep(1)
print("Still running".)

I imagine this will work on any OS, but another (OS specific) option would be something like while(names(dev.cur()) == "windows").

Community
  • 1
  • 1
Molx
  • 6,816
  • 2
  • 31
  • 47
  • Yeah that works, but I just realized that if I close the window I will get `Error in grid.Call(L_locator) : no graphics device is active Calls: replicate ... sapply -> lapply -> FUN -> -> grid.Call`. I don't want one click to close my window neither do I know how many clicks I would like before moving on. – Tinker Jul 08 '15 at 04:23
  • @Sparrowcide Check my addition to the answer. – Molx Jul 08 '15 at 15:57