Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export plot in .png with transparent background

Tags:

plot

r

png

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?

like image 376
francoiskroll Avatar asked Apr 25 '17 15:04

francoiskroll


People also ask

How do I save a PNG with a transparent background?

Save As A Transparent PNG ImageClick “File” -> “Save As”. Select “PNG (*. PNG) as the file format. Note that though a transparent background looks checkered in Photoshop, it will actually be transparent in the final PNG file.

Can PNG files have transparent background?

These features make PNG an ideal file type to use for logos, icons, and digital art. Most importantly, PNG is a crowd favorite because it supports transparent backgrounds. Colors (including white) fill all the pixels in a regular image.

How do you make a plot transparent?

If you want to make the graph plot more transparent, then you can make alpha less than 1, such as 0.5 or 0.25. If you want to make the graph plot less transparent, then you can make alpha greater than 1.


2 Answers

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

dev.copy(png,'myplot.png')
dev.off()
like image 129
gonzalez.ivan90 Avatar answered Sep 18 '22 15:09

gonzalez.ivan90


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)
like image 31
Diana Šimić Avatar answered Sep 17 '22 15:09

Diana Šimić