Search code examples
rplotpng

Export plot in .png with transparent background


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?


Solution

  • x = c(1, 2, 3)
    par(bg=NA)
    plot (x)
    
    dev.copy(png,'myplot.png')
    dev.off()