4

I cannot seem to make work the png(), bmp(), etc. with ggplot, and ggsave(filename="clipboard") does nothing. Any ideas?

T_O
  • 173
  • 1
  • 8
  • You wouldn't copy the plot to the clipboard. You create a ggplot and either assign it to an object that can then be saved or just run the code directly. The workflow is something like this, where I've separated each new line with a semi-colon (;): `png("plot.png"); ggplot(dat, aes(xvar, yvar)) + geom_line(); dev.off()`. – eipi10 Aug 08 '17 at 22:42
  • `ggsave` will by default save the last ggplot you created (the one displayed in the plot window). So, you could do `ggplot(dat, aes(xvar, yvar)) + geom_line()` then just type `ggsave("my_plot.png")` to save it as a `png` file. – eipi10 Aug 08 '17 at 22:47
  • 1
    So you're saying it's not possible to copy a plot to my clipboard? – T_O Aug 08 '17 at 22:51
  • I don't know. I've never had a reason to want to do that. But maybe [this SO question](https://stackoverflow.com/questions/23295525/copy-r-plot-to-clipboard-with-custom-size) will be helpful. – eipi10 Aug 08 '17 at 22:53
  • Yes I thought it would, but it does not work (and I'm on mac OS) – T_O Aug 08 '17 at 22:55
  • 1
    This sounds ridiculously simple, but in Mac OS, just click on the plot window, hit `Cmd-C` to copy, and then just paste it into whatever document you want to paste it into. – Woodstock Aug 09 '17 at 03:19
  • @Woodstock it doesn't seem to work, whether the plot clicked is in another window or on the 4th pane. If I hit `cmd-c` I hear a system noise which implies that nothing was done – T_O Aug 09 '17 at 05:50
  • one could always `ggsave()` it and then copy that file... or insert the file into a document and copy it from there. Sounds like tedious work if you have lots of them to do, though. – Matt74 Aug 09 '17 at 06:16
  • @T_O - in that case, if nothing else works, hit 'Cmd-Shift-4` --> that will bring up a cross hair. Position that in the upper left, click your mouse and drag the cross-hair to the bottom right, to form a large square/rectangle. When you release, the Mac will have taken a screenshot of that area and saved it to your Desktop folder as a .png file, where you can access it. This is a Mac OS system utility that works in all Mac OS programs. – Woodstock Aug 09 '17 at 15:26
  • @Woodstock Thank you, but the reason why I wanted to avoid screenshots was because of the quality loss (and quality can be important especially with labels). For now I export as "Copy to clipboard" which is a few clicks, but there doesn't seem to be a better solution – T_O Aug 09 '17 at 15:48
  • @T_O at some point, just post same sample data and some sample code of what you are doing, and we can try to get to the bottom of it. – Woodstock Aug 09 '17 at 16:44
  • 1
    `data(mtcars);ggplot(data=mtcars)+geom_point(aes(x=cyl, y=mpg));ggsave(filename="clipboard");` also `png(filename="clipboard");dev.off()` does the same which is nothing.. If you think Cmd+shift+4 has the same effect as copying the image, then I don't know what to say unfortunately – T_O Aug 09 '17 at 16:54

3 Answers3

3

Try this :

dev.new()
my_plot <- ggplot(data.frame(x=1:10,y=1:10),aes(x,y)) + geom_point()
my_plot
savePlot("clipboard")

Or if you don't want to display it at all (Windows) :

win.metafile()
my_plot2 <- ggplot(data.frame(x=1:5,y=1:5),aes(x,y)) + geom_point()
print(my_plot2)
dev.off()

Relevant: Save plot without showing it at all

moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
0

At a guess, are you trying to use ggsave() within a function, loop, or within an external file?

This is a common speedbump with a simple fix. You need to explicitly print the file.

Instead of:

makeplot <- function(){
    p <- gpplot(....some plotting....)
    p 
    ggsave("foo.bmp")  #or other function to save an image
}

You need to:

makeplot <- function(){
    p <- gpplot(....some plotting....)
    print(p )
    ggsave("foo.bmp")  #or other function to save an image
}

See similar question here: ggplot's qplot does not execute on sourcing

Joseph Weaver
  • 91
  • 1
  • 4
  • it's not so much about function wrapping & saving a plot than copying to my clipboard, which is the main topic of my question – T_O Aug 09 '17 at 16:56
0

I think the goal is just to put a plot into the clipboard. I wrote a function to do that by writing the plot to a temporary file. Since the OP is clearly on macOS, as am I, this function is for now only suitable for macOS. Adapt as needed.

plot_clipboard <- function(plot, width = 7, height = 6, device_fn = cairo_pdf) {

  # determine required file extension based on input for device_fn
  file_type <- deparse(substitute(device_fn))
  file_type <- gsub(".*(.{3})$", "\\1", file_type)
  tmpfile <- tempfile(fileext = paste0(".", file_type))

  # save plot to temp file
  device_fn(filename = tmpfile, width = width, height = height)
  print(plot)
  dev.off()

  message("Saved plot to temp file: ", tmpfile)

  # and copy this file into the clipboard
  sysname <- tolower(Sys.info()["sysname"])
  if (sysname == "darwin") {
    # macOS, make use of Applescript
    out <- system(command = paste0("osascript -e 'on run args' -e 'set the clipboard to POSIX file (first item of args)' -e end \"$@\"", tmpfile), intern = TRUE)
  # } else if (sysname == "windows") {
  #   # something for Windows here
  } else {
    message("Sorry, you're on your own!")
  }
}

It's still a bit work in progress. Pasting in Finder works fine, but pasting into a Word document doesn't (while Word correctly says it's about to paste a 'Files' object).

For writing scientific manuscripts, I'm still looking out for a function that writes a PDF file that's immediately paste-able into Word (so no quality loss), enabling me going from R to Word with only doing this:

analysis.1.2.3 <- my_research_data %>%
  ggplot() +
  geom_col() +
  younameit()

analysis.1.2.3 %>% plot_clipboard()

And then Cmd+V while in Word. As I said, work in progress :)

MS Berends
  • 4,489
  • 1
  • 40
  • 53