在R、RStudio中同时显示和保存绘图

在R、RStudio中同时显示和保存绘图,r,rstudio,R,Rstudio,我需要将绘图保存为.png,同时显示绘图,而不复制代码。 有没有优雅的方法? 正在为MAC开发RStudio 我可以让它像下面那样工作,但我不喜欢它 #Step1: save the plot png("myplot.png") #plot code dev.off() #Step2: to display the plot #plot code (again!) to display it in RStudio 干杯, I.M.我发现前面的答案不完整。深入了解如何在默认RStudio“Pl

我需要将绘图保存为.png,同时显示绘图,而不复制代码。 有没有优雅的方法? 正在为MAC开发RStudio

我可以让它像下面那样工作,但我不喜欢它

#Step1: save the plot
png("myplot.png")
#plot code
dev.off()

#Step2: to display the plot
#plot code (again!) to display it in RStudio
干杯,
I.M.

我发现前面的答案不完整。深入了解如何在默认RStudio“Plots”窗口中显示绘图,同时将绘图保存在“png”中

如果有一天,有人可能会遇到同样的问题,这里是如何做到的,一行一行:

R:> 
R:> dev.list()                  # fresh start. no graphical devices available at this point
NULL
R:> dev.cur()                   # no current device at this point
null device 
      1 
R:> dev.new()                   # open graphical devices
NULL
R:> dev.list()                  # list them
     RStudioGD quartz_off_screen 
        2                 3 
R:> png("plot50.png")           # open an offscreen device as png. New device should be number 4
R:> dev.list()                  # the list of all graphical devices includes the newly created device, number 4
    RStudioGD quartz_off_screen quartz_off_screen 
          2          3               4 
R:> dev.cur()                   # NOTE: the new created device(number 4) becomes "current" automatically,
quartz_off_screen               # as soon as it has been created
            4 
R:> dev.set(which = 2)          # switch back to device 2 used to display the plot in the default RStudio 
                                # "Plots" window
RStudioGD 
    2 
R:> dev.cur()                   # indeed, RstudioGD becomes the current device after the switch step from above
RStudioGD 
    2 
R:> dev.list()                  # just a check on all available devices. device 4 still in the list after 
                                # the switch
    RStudioGD quartz_off_screen quartz_off_screen 
            2           3                 4 
R:> plot(c(1:100))              # plot an example. It will be displayed in "Plots" window of RStudio
R:> dev.list()                  # just a check on all the available devices
   RStudioGD quartz_off_screen quartz_off_screen 
      2                 3                 4 
R:> dev.copy(which = 4)         # copies from current device(RStudioGD) to device 4. It automatically sets
   quartz_off_screen            # device 4 as current
         4 
R:> dev.cur()                   # indeed , device 4 is the current device
  quartz_off_screen 
         4 
R:> dev.off()                   # close device 4. IMPORTANT: AT THIS POINT the plot is saved as
    RStudioGD                   # png("plot50.png") in the current working directory.
                                # Three actions takes place at this point, all at once:
                                # 1. closes device 4
                                # 2. save the plot as "plot50.png"
                                # 3. sets the dev.next() (which is RStudioGD) as the current device
       2 
R:> dev.cur()                   # RStudioGD becomes current as soon as device 4 has been closed down.
   RStudioGD 
       2 
R:> 

为了补充弗拉门戈的伟大答案,这里有一个简单的版本,它似乎是用RStudio设计的。假设您在RStudio中绘制了一些内容,然后希望保存相同的内容

pdf(file = "xyz.pdf")
dev.set(which = 2)
dev.copy(which = 4)
dev.off()
我反复尝试使用dev.off(),此工作流似乎相当稳定。

我找到了一个答案:

如果按照上一节中的过程进行操作,首先必须在屏幕上绘制一个绘图,然后重新输入命令以将绘图保存到文件中。R还提供dev.copy命令,将图形窗口的内容复制到文件中,而无需重新输入命令。对于大多数情节来说,一切都会很好,但有时将屏幕上的内容转换成不同的格式看起来并不像应该的那么好

要使用这种方法,首先以通常的方式生成图形。什么时候 如果您对它的外观感到满意,请调用dev.copy,并将其传递给 要使用的驱动程序、存储该驱动程序的文件名以及任何其他 适用于驱动程序的参数

他们使用
dev.copy
给出的示例如下:

dev.copy(png,'myplot.png')
dev.off()

我在研究类似问题时偶然发现了这篇文章。我在保存Jupyter笔记本中运行的R中的一个情节时遇到困难。我张贴我的完整性解决方案。使用
pdf(“myplot.pdf”)
(或
png
jpeg
,…)、绘图代码和
dev.off()。您必须在Jupyter笔记本的同一个单元格中完成所有这些操作。这是对我有用的东西(都在同一个牢房里)


这可能会有所帮助--即,先打印以显示,然后复制到png。设备3发生了什么?我试图创建一个postscript,R正在打开一个
png
devic,但没有关闭它。它似乎已正确写入指定的ps文件,但我不喜欢此
png
设备四处浮动。我不确定是否理解您的问题。您想在打开新设备后立即关闭设备3吗?如果是这样的话,答案是你必须手动关闭之前打开的设备。问题基本上是按编号引用设备可能会导致问题——跟踪哪个设备处于哪个编号并不总是容易的。通过使用
which=dev.list()。对于那些使用多个
png
设备的人来说,这可能需要进一步调整,但这只是一个开始。
pdf("myplot.pdf")
# Plot commands here 
dev.off()