在 R、RStudio 中同时显示和保存绘图 [英] Display and save the plot simultaneously in R, RStudio

查看:111
本文介绍了在 R、RStudio 中同时显示和保存绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将绘图另存为 .png 并同时显示绘图而不复制代码.有没有一种优雅的方法来做到这一点?在 RStudio for MAC 上工作.

I need to save the plot as .png and show the plot at the same time without duplicating the code. Is there an elegant way to do it? Working on RStudio for MAC.

我可以让它按如下方式工作,但我不喜欢它.

I can get it to work as bellow but I don't like it.

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

#Step2: to display the plot
#plot code (again!) to display it in RStudio

干杯,即时通讯

推荐答案

我发现之前的答案不完整.深入了解如何在默认的 RStudio绘图"中显示绘图.窗口并将绘图保存在png"中同时.

I found the previous answers as being incomplete. Drilled down on how to display the plot in the default RStudio "Plots" window and save the plot in "png" at the same time.

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

In case that someday, someone might come across the same question, here is how is being done, line by line:

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:> 

这篇关于在 R、RStudio 中同时显示和保存绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆