Written and maintained by CASRAI Editorial Board
Published · Last updated
On this page
To save an R plot as a PNG file, call png() from the grDevices package with a file name, run the plotting code, then close the device with dev.off(). The file is only complete after dev.off(). Because the default size is 480 × 480 pixels and not inches, set width, height, units and res explicitly whenever the figure is destined for a manuscript, poster or slide.
What png() is used for in research
Researchers use png() to write figures from an analysis script to disk so that the same script regenerates every figure on demand. The R documentation describes the PNG format as lossless and best suited to line diagrams and blocks of colour, whereas JPEG is lossy and suited to continuous-tone images. Scatter plots, bar charts, box plots and histograms fall in the first group, so PNG is the usual raster choice for statistical graphics. If a journal asks for vector artwork, a raster PNG is not the right format; see scientific figure rules for journal submission before choosing.
png() opens a graphics device: it returns nothing useful itself, and every plotting call after it draws into the file until the device is closed. That device model explains most of the questions people ask about saving plots in R, including why a file is empty or why two plots end up in one file name.
How to install or access it
png() is part of the grDevices package, which ships with base R, so there is nothing extra to install. Obtain R itself from the Comprehensive R Archive Network (CRAN) at cran.r-project.org; the CRAN manual An Introduction to R covers starting a session. An editor such as RStudio is optional (see the RStudio cheat sheet), because png() works the same in a plain R console or in a script run with Rscript.
The one optional extra is the ragg package, which provides agg_png(), an alternative PNG device covered below. It is installed from CRAN in the usual way with install.packages("ragg").
Step by step: save a scatter plot as a PNG
This worked example uses mtcars, a data set that is built into R, so it runs without any file of your own.
-
Open the device and give it the file name, a physical size and a resolution:
png("mtcars_scatter.png", width = 6, height = 4, units = "in", res = 300) -
Run the plotting code. Nothing appears on screen, because the output is going to the file:
plot(mtcars$wt, mtcars$mpg, xlab = "Weight (1000 lb)", ylab = "Miles per gallon", main = "Fuel economy against weight") -
Close the device so R finishes writing the file:
dev.off() -
Open
mtcars_scatter.pngfrom the working directory. Usegetwd()to see where that is, or give a full path in the first argument. R expands a leading tilde in the file name.
With the settings above, the picture is 6 inches by 4 inches at a nominal 300 pixels per inch, so the file is 1,800 × 1,200 pixels. That follows from the documented rule that res is used to convert units other than pixels into pixels. Leave out units and res and the same width = 6, height = 4 would mean a 6 × 4 pixel image, because the defaults are pixels. R warns when both dimensions are below 20.
How width, height, units and res interact
The documentation gives the arguments and defaults; the table below works out what common combinations produce. Pixel sizes are the product of the size in the stated unit and res where the unit is inches; they are arithmetic from the documented conversion rule, not separate measurements.
| Call | Interpretation | Resulting pixel size |
|---|---|---|
png("a.png") |
All defaults: 480 × 480, units px | 480 × 480 |
png("a.png", width = 800, height = 600) |
Sizes in pixels, no resolution recorded | 800 × 600 |
png("a.png", width = 6, height = 4, units = "in", res = 300) |
6 in × 4 in at 300 ppi | 1,800 × 1,200 |
png("a.png", width = 3.5, height = 3, units = "in", res = 600) |
3.5 in × 3 in at 600 ppi | 2,100 × 1,800 |
The units argument accepts px (the default), in, cm and mm. The res value is the nominal resolution recorded in the file when it is a positive integer. If you do not set it, no resolution is recorded and viewers often assume 72 pixels per inch, which is why a figure sized in inches can open at a surprising physical size. Check the target journal’s stated resolution and size requirements first, then choose width, height and res to match.
Output and how to read it
The result of the example is a single PNG file on disk; png() itself prints nothing. Judge the file by its pixel dimensions and by whether text and lines look sharp at the size it will be printed or shown. A figure that is blocky or tiny almost always means the size was left in pixels, or res was not set. Setting pointsize (default 12) changes the nominal text size in points, and res is also used to convert points to pixels, so text scales with resolution.
Choosing the type argument
The type argument selects the graphics library, and its default comes from getOption("bitmapType"). The documentation states that cairo is best for antialiased fonts and semi-transparent colours. It also describes how the two Cairo-based values differ in what they write to the file:
| Setting | Colour storage in the PNG file | Documented note |
|---|---|---|
type = "cairo" |
Palette if the plot has fewer than 256 colours, otherwise 24-bit RGB | Best for antialiased fonts and semi-transparent colours |
type = "cairo-png" |
Never uses a palette; normally 32-bit ARGB | Files are normally larger |
Transparency is supported: bg = "transparent" gives a transparent background, although not every viewer displays it as transparent.
Writing several plots with one call
The file name is treated as a C-style integer format, so png("fig%03d.png") writes fig001.png, fig002.png and so on, one file per page plotted. The default file name, Rplot%03d.png, already does this. If you draw several pages and the name has no %d pattern, the file holds only the last page plotted, so earlier plots are lost.
png("fig%03d.png", width = 6, height = 4, units = "in", res = 300)
hist(mtcars$mpg)
boxplot(mpg ~ cyl, data = mtcars)
dev.off()
For the plot types themselves, see the guides to histograms in R and box plots in R, and to line width in base graphics, lwd in R.
Troubleshooting
The PNG file is empty, missing or incomplete
The usual cause is a missing dev.off(). Only one device is active at a time, and dev.off() shuts down the current one, which finishes the file. If an earlier script failed halfway, a device may still be open from before: dev.list() shows the open devices, dev.cur() shows the active one, and graphics.off() closes all of them. Devices are also closed when the R session ends normally.
The image is far smaller or blurrier than expected
The defaults are 480 × 480 pixels with no recorded resolution. Set units and res as in the example. A pair of small numbers such as width = 6, height = 4 with default units is read as pixels; R warns if both are under 20.
Only the last plot was saved
Add the %03d pattern to the file name, or open a separate png() and dev.off() pair for each figure.
Text looks jagged or transparency looks wrong
Try type = "cairo", which the documentation recommends for antialiased fonts and semi-transparent colours, or switch to ragg::agg_png() below.
Running from ggplot2
The png() device also receives ggplot2 output, provided the plot object is printed while the device is open. For a ggplot object you can alternatively use ggplot2’s own saving function; see the guide to ggplot in R for the plotting side.
Free alternatives
All three routes below are free and open source. Base R’s png() is enough for most work; agg_png() is worth considering when you need identical bytes from identical code.
| Route | Where it comes from | Documented difference |
|---|---|---|
grDevices::png() |
Base R (grDevices) | Lossless PNG; writes the date and time into the file; default res = NA |
ragg::agg_png() |
The ragg package from CRAN | Supports transparency and 8 and 16 bit colour; does not write date and time, so identical code gives identical files; default res = 72 |
jpeg() |
Base R (grDevices) | Lossy format, better suited to continuous-tone images than to line diagrams |
The reproducibility point matters for version control. Because agg_png() does not write a timestamp, regenerating a figure from unchanged code gives a byte-identical file, so a repository diff shows a change only when the figure actually changed. Its signature mirrors png(): agg_png(filename = "Rplot%03d.png", width = 480, height = 480, units = "px", pointsize = 12, background = "white", res = 72, scaling = 1, ...). Note that the background argument is called background there, and that the default resolution is 72 rather than unset. The data table below compares the two sets of defaults.
Frequently asked questions
What is the default size of a PNG saved with png() in R?
480 × 480 pixels. The width and height defaults are 480 and units defaults to "px", according to the grDevices documentation, and no resolution is recorded unless you set res.
Why does png() produce an empty file?
Most often because dev.off() was not called, so the device is still open. Run dev.off(), or graphics.off() to close every open device.
How do I set the resolution of a PNG in R?
Pass res in pixels per inch and give the size in physical units, for example width = 6, height = 4, units = "in", res = 300 for a 1,800 × 1,200 pixel file.
Is PNG or JPEG better for plots in R?
The R documentation calls PNG lossless and best for line diagrams and blocks of colour, and JPEG lossy and better for continuous-tone images, so PNG suits most statistical graphics.
How do I get a transparent background?
Set bg = "transparent". Not all viewers show the transparency, so check the file in the program where it will be used.
Sources
- R Core Team, grDevices: BMP, JPEG, PNG and TIFF graphics devices (png help page), R documentation; read 2026-11-14.
- R Core Team, grDevices: Control Multiple Devices (dev.off, dev.list, graphics.off), R documentation; read 2026-11-14.
- ragg package, agg_png reference (version 1.5.2 shown); read 2026-11-14.
- CRAN, An Introduction to R; read 2026-11-14.
- W3C, Portable Network Graphics (PNG) Specification, the specification cited by the png help page.
Data: Default arguments of grDevices::png() and ragg::agg_png() in R
Side-by-side defaults of the base R png() device and the ragg agg_png() device as stated in their reference documentation, compiled to show which defaults differ (resolution, background argument name, scaling).
Period covered: 2026.
| Argument | grDevices::png() default | ragg::agg_png() default |
|---|---|---|
| filename | Rplot%03d.png | Rplot%03d.png |
| width | 480 | 480 |
| height | 480 | 480 |
| units | px | px |
| pointsize | 12 | 12 |
| background colour | bg = "white" | background = "white" |
| res | NA (no resolution recorded) | 72 |
| scaling | not an argument | 1 |
Sources
- grDevices png help page (accessed 2026-11-14)
- ragg agg_png reference (accessed 2026-11-14)
Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.







