我需要将图表保存为.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.
我需要将图表保存为.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:>
png
设备,却没有关闭它。它似乎已经成功地写入了指定的ps文件,但我不喜欢这个“png”设备漂浮在那里。 - MichaelChiricowhich=dev.list()["png"]
及其类似物来解决了这个问题。对于那些使用多个 png
设备的人来说,这可能需要进一步调整,但这是一个开始。 - MichaelChirico补充flamenco的精彩回答,这里提供一个看起来可以在RStudio简单运行的版本。假设您已经在RStudio中绘制了某些内容,然后想要保存相同的内容。
pdf(file = "xyz.pdf")
dev.set(which = 2)
dev.copy(which = 4)
dev.off()
我尝试了反复使用 dev.off(),这个工作流似乎非常稳定。
dev.copy(png,'myplot.png')
dev.off()
我在寻找类似问题的研究时偶然发现了这篇文章。我在运行Jupyter笔记本中的R时遇到了保存绘图的麻烦。为了完整起见,我发布了我的解决方案。您可以使用pdf("myplot.pdf")
(或png
或jpeg
等)序列,绘图代码和dev.off()
。您必须在Jupyter笔记本的同一单元格中执行所有操作。以下是对我有效的操作(全部在同一单元格中执行)。
pdf("myplot.pdf")
# Plot commands here
dev.off()