Akos
Akos

Reputation: 870

Print high quality plots in R when manipulating plot interactively

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?

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.

Upvotes: 1

Views: 195

Answers (1)

Molx
Molx

Reputation: 6931

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:

#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()

Upvotes: 2

Related Questions