Export plot in .png with transparent background
Asked Answered
S

2

25

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?

Spirant answered 25/4, 2017 at 15:23 Comment(2)
try: png("myplot.png", width=600, height=400, bg = "transparent"); plot(x); dev.off()Psychometrics
It worked for me with plot (x, bg = 'transparent'); dev.copy (png,'myplot.png', bg = 'transparent') Rattler
C
35
x = c(1, 2, 3)
par(bg=NA)
plot (x)

dev.copy(png,'myplot.png')
dev.off()
Commissioner answered 25/4, 2017 at 15:28 Comment(2)
and how would one revert the par change?Chian
Creating an object with default parametrs: ´parOrig <- par()´ and using it as an ´par()´ argument: ´par(parOrig)´ ´parOrig <- par(); par(bg=NA, mfrow = c(2, 1)); plot (1:3); par(parOrig); plot (1:3)´Commissioner
D
1

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)
Derangement answered 25/1, 2020 at 20:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.