6

Is it possible to save a plot without displaying it at all ?

I made a little ggplot hack to be able to copy graphs to powerpoint easily, it copies the plot to the clipboard, but one can see the device window open and close fast, it's a bit awkward, can I avoid this ?

I'm using windows and rstudio.

reproducible code:

library(ggplot)

`-.gg` <- function(e1,e2){
  assertthat::assert_that(is.numeric(e2),
                          length(e2)<= 2)
  if(identical(e2,0)) return(invisible(NULL))
  W <- 8
  H <- 4.5
  dev.new(width=W * head(e2,1), height=H * tail(e2,1),noRStudioGD =TRUE)
  print(e1)
  savePlot("clipboard", type="wmf")
  dev.off()
  e1
}

ggplot(data.frame(x=1:10,y=1:10),aes(x,y)) + geom_point() - 1 - 0

Edit:

My code, and chosen solution, have issues dealing with semi-transparency.It's ok most of the time, but exceptions will be annoying. Maybe a path to a general solution would be to save it with tempfile then read it into the clipboard, either through an appropriate R function, or with command line using system (maybe something that would open the file invisibly and copy).

0

2 Answers 2

3

This works on Windows: use the win.metafile() device. If you give no filename, it saves to the clipboard. So your function should be

library(ggplot2)

`-.gg` <- function(e1,e2){
  assertthat::assert_that(is.numeric(e2),
                          length(e2)<= 2)
  if(identical(e2,0)) return(invisible(NULL))
  W <- 8
  H <- 4.5
  win.metafile(width=W * head(e2,1), height=H * tail(e2,1))
  print(e1)
  dev.off()
  e1
}

ggplot(data.frame(x=1:10,y=1:10),aes(x,y)) + geom_point() - 1 - 0
Sign up to request clarification or add additional context in comments.

3 Comments

I was too good to be true, ggplot(data.frame(x=1:10,y=1:10),aes(x,y)) + geom_smooth() - 1 - 0 returns semi-transparency is not supported on this device. geom_smooth won't work completely, but it seems the alpha parameter is supported.
better example: ggplot(iris,aes(Sepal.Length,Sepal.Width)) + geom_smooth() - 1 - 0
I get the same warning from your original savePlot code. It's a limitation of the WMF device. Maybe the WMF format supports transparency by now, but the device doesn't know that.
1

On windows and R 3.4.2, using Sys.sleep was able to view the plot instead of blink and miss

`-.gg` <- function(e1,e2){
    assertthat::assert_that(is.numeric(e2),
                      length(e2)<= 2)
    if(identical(e2,0)) return(invisible(NULL))
     W <- 8
     H <- 4.5
    dev.new(width=W * head(e2,1), height=H * tail(e2,1),noRStudioGD =TRUE)
    print(e1)
    savePlot("clipboard", type="wmf")
    Sys.sleep(3) ##
    dev.off()
    e1
 }

ggplot(data.frame(x=1:10,y=1:10),aes(x,y)) + geom_point() - 1 - 0

1 Comment

It's a good idea as it makes it less awkward, but I'm also curious if I can actually not show it at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.