交互式操作绘图时在 R 中打印高质量绘图

Print high quality plots in R when manipulating plot interactively

我需要将单倍型网络以高质量打印为 png。但是,为了按照我想要的方式获得情节,我需要使用交互功能更改一些内容。当我使用 png() 打印绘图时,我无法进行这些更改,因为它打印到 pdf 而不是绘图 window。有什么方法可以将其更改为打印到 plot window 以及 png 吗?

可重现的情节示例:

install.packages("pegas")
library(pegas)
data(woodmouse)
net <- haploNet(haplotype(woodmouse))
plot(net)
o <- replot() # interactive
## click to rearrange the network at will...
## then do a different plot using the same coordinates:
plot(net, bg = "red", labels = FALSE, show.mutation = 2)
replot(o) # not interactive

我不想使用 RStudio 中的导出按钮,因为它生成的图表质量很差。

您可以使用 recordPlot() 将当前绘图保存到变量,然后将图形设备更改为 png 并使用 replayPlot 重播保存的绘图。例如:

#same data as above
o <- plot(net, bg = "red", labels = FALSE, show.mutation = 2)
replot() # interactive - do your thing

myplot <- recordPlot()
png("recordedPlot.png")
replayPlot(myplot)
dev.off()