【问题标题】:How to copy R ggplot to clipboard?如何将 R ggplot 复制到剪贴板?
【发布时间】:2017-08-08 22:29:49
【问题描述】:

我似乎无法使png()bmp() 等与ggplot 一起工作,而ggsave(filename="clipboard") 什么也不做。 有什么想法吗?

【问题讨论】:

  • 您不会将绘图复制到剪贴板。您创建一个 ggplot 并将其分配给一个可以保存的对象,或者直接运行代码。工作流程是这样的,我用分号 (;) 分隔每一行:png("plot.png"); ggplot(dat, aes(xvar, yvar)) + geom_line(); dev.off()
  • ggsave 默认情况下会保存您创建的最后一个 ggplot(显示在绘图窗口中的那个)。因此,您可以使用 ggplot(dat, aes(xvar, yvar)) + geom_line() 然后输入 ggsave("my_plot.png") 将其保存为 png 文件。
  • 所以你是说不能将绘图复制到我的剪贴板?
  • 这听起来很简单,但在 Mac OS 中,只需单击绘图窗口,点击Cmd-C 进行复制,然后将其粘贴到您想要粘贴到的任何文档中。
  • data(mtcars);ggplot(data=mtcars)+geom_point(aes(x=cyl, y=mpg));ggsave(filename="clipboard");png(filename="clipboard");dev.off() 做同样的事情没什么。。如果你认为 Cmd+shift+4 和复制图像的效果一样,那么我不知道该说什么很遗憾

标签: r ggplot2


【解决方案1】:

试试这个:

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

或者如果您根本不想显示它(Windows):

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

相关:Save plot without showing it at all

【讨论】:

  • 此解决方案是否仅适用于 Windows?
  • @Denis 是的
【解决方案2】:

猜测一下,您是想在函数、循环还是外部文件中使用 ggsave()?

这是一个常见的减速带,有一个简单的修复。您需要明确地print 文件。

代替:

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

你需要:

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

在此处查看类似问题:ggplot's qplot does not execute on sourcing

【讨论】:

  • 与其说是函数包装和保存绘图,不如说是复制到我的剪贴板,这是我的问题的主题
【解决方案3】:

我认为目标只是将绘图放入剪贴板。我通过将绘图写入临时文件来编写一个函数来做到这一点。由于 OP 和我一样显然是在 macOS 上,所以这个功能现在只适用于 macOS。根据需要进行调整。

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!")
  }
}

还有一些工作正在进行中。在 Finder 中粘贴可以正常工作,但粘贴到 Word 文档中则不行(虽然 Word 正确地表示将要粘贴“文件”对象)。

对于撰写科学手稿,我仍在寻找一种功能,该功能可以编写可立即粘贴到 Word 中的 PDF 文件(因此不会造成质量损失),只需执行以下操作即可从 R 转到 Word:

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

analysis.1.2.3 %>% plot_clipboard()

然后在 Word 中使用 Cmd+V。正如我所说,工作正在进行中:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-06
    • 1970-01-01
    • 2011-04-08
    • 2013-11-21
    • 2010-10-09
    • 1970-01-01
    • 2019-10-30
    • 1970-01-01
    相关资源
    最近更新 更多