Export plot in .png with transparent background

2020-07-02 10:15发布

问题:

I am trying to export a simple plot in .png with transparent background. I am able to export it, but the background stays white.

Mock example

x = c(1, 2, 3)

I've tried this

plot (x)

dev.copy (png,'myplot.png', bg = 'transparent')
dev.off()

And this

plot (x, bg = 'transparent')

dev.copy (png,'myplot.png')
dev.off()

But neither work.

Can someone help?

回答1:

x = c(1, 2, 3)
par(bg=NA)
plot (x)

dev.copy(png,'myplot.png')
dev.off()


回答2:

Instead of saving all parameters, it is better to only save the old value of the parameter that was changed in a call to ´par´ by saving result of ´par´ as in the modified example:

x = c(1, 2, 3)
old.par <- par(bg=NA)
plot (x)

dev.copy(png,'myplot.png')
dev.off()
par(old.par)


标签: r plot png