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

时间:2023-02-07 07:12:18

I need to print a haplotype network to png in high quality. However, to get the plot the way I want it, I need to change some things using an interactive function. When I use png() to print the plot, I'm not able to make these changes, because it prints to the pdf rather than to the plot window. Is there any way to change it to print to plot window as well as png?

我需要打印单倍型网络以高质量打印。然而,为了按照我想要的方式获得情节,我需要使用交互式功能来改变一些事情。当我使用png()打印绘图时,我无法进行这些更改,因为它打印到pdf而不是绘图窗口。有没有办法改变它打印到绘图窗口以及png?

A reproducible example of the plot:

一个可重现的情节示例:

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

I don't want to use the export button in RStudio because the graphs it produces are of low quality.

我不想在RStudio中使用导出按钮,因为它产生的图形质量很低。

1 个解决方案

#1


1  

You can save the current plot to a variable using recordPlot(), then change the graphic device to png and replay the saved plot using replayPlot. For example:

您可以使用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()

#1


1  

You can save the current plot to a variable using recordPlot(), then change the graphic device to png and replay the saved plot using replayPlot. For example:

您可以使用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()