ggsave
Exports plot to a file. Supported formats: SVG, HTML, PNG, JPEG, and TIFF. Note: in some configurations raster formats might not be supported.
If path is not specified, the output file will be saved in ${user.dir}/lets-plot-images.
Notes
Large plot dimensions without units require explicit unit specification. When w or h value exceeds 20 without specifying units (e.g., ggsave(p, 300, 400)), we ask to specify units explicitly: ggsave(p, 300, 400, unit="px") or ggsave(p, 3, 4, unit="in").
The output format is inferred from the file extension.
For PNG, JPEG, and TIFF:
If
w,h,unit, anddpiare all specified:The plot's pixel size (default or set via ggsize()) is ignored.
The output size is computed from the given dimensions and DPI.
The plot is resized to fit the specified
wxharea, which may affect the layout.If only
dpiis specified:The plot's pixel size (default or set via ggsize()) is converted to inches assuming the standard display PPI of 96 PPI.
The output size is computed from this size and
dpi.The plot maintains its aspect ratio, preserving layout, tick labels, and other visual elements.
Useful for printing - the plot will appear nearly the same size as on screen.
If
wandhare not specified:The
scaleparameter is used to determine the output size.The plot maintains its aspect ratio, preserving layout, tick labels, and other visual elements.
Useful for generating high-resolution images suitable for publication.
For SVG:
If
w,h, andunitare specified:The plot's pixel size (default or set via ggsize()) is ignored.
The output size is set from the given values.
For HTML:
If
wandhare specified:The plot's pixel size (default or set via ggsize()) is ignored.
The output size is determined directly from the specified
wandh, which are treated as pixel values.
Examples
Return
Absolute pathname of the created file.
Parameters
Plot to export.
Name of the file. Must include an extension matching one of the supported formats: .svg, .html/.htm, .png, .jpeg/.jpg, or .tiff/.tif
Scaling factor (only for raster formats). Default: 2.0
Width of the output image in units.
Height of the output image in units.
Unit of the output image. One of: "in", "cm", "mm", "px". Only applicable when exporting to SVG, PNG, JPG, or TIFF. For HTML, the unit is always "px" (pixels). Default: "px" (pixels) for SVG, "in" (inches) for the raster formats (PNG, JPG, and TIFF).
Resolution in dots per inch. Only applicable when exporting to the raster formats: PNG, JPG, or TIFF. The default value depends on the unit:
for
"px"it is96(output image will have the same pixel size aswandhvalues)for physical units (
"in","cm","mm") it is300.
Path to a directory to save image files in. Default: ${user.dir}/lets-plot-images