| ggwrite | R Documentation |
Writes ggplot plots, gtable objects, or lists thereof to PNG or PDF
files with optional stamping (caption with script name, timestamp, and model information).
Supports flexible canvas sizing, multiple output formats, and both single-file and
multi-file output for lists of plots.
ggwrite(
plot,
file,
canvas = NULL,
formats,
onefile = FALSE,
res = 200,
paper = "special",
save = TRUE,
show = !save,
use.names = FALSE,
subdir = NULL,
script,
time,
model,
name.all.pdf,
quiet = FALSE,
path.format = NULL,
method.png = c("ragg", "png"),
stamp.format = NULL,
stamp.funs = NULL,
stamp.gg.style = NULL,
useNames,
fun.path = NULL
)
plot |
A plot object or a list of plot objects. Normally generated with
|
file |
Character string specifying the output file path. Must end in
|
canvas |
Either a list with |
formats |
Character vector specifying file format(s) to write. Must be a
subset of |
onefile |
Logical. Only applicable if |
res |
Numeric. Resolution in pixels per inch for PNG output. Default is 200.
Passed to |
paper |
Character string specifying paper type. Only used with PDF device.
Default is |
save |
Logical. Should the plot be saved to file? Default is |
show |
Logical. Should the plot be displayed on screen? Default is the
opposite of |
use.names |
Logical. If |
subdir |
Character string specifying a subdirectory within the path structure.
This is passed to the path function generated from |
script |
Character string or path to the script file. If provided, a stamp
(caption) will be added to the plot showing the script name, timestamp, and
optionally the model. Requires |
time |
Character string or POSIXct timestamp. By default, a timestamp is
included if |
model |
Character string or model object specifying the model name/identifier.
Used for stamping and for organizing output directories via |
name.all.pdf |
Character string specifying the filename to use when writing
all plots to a single PDF file (when |
quiet |
Logical. If |
path.format |
Character string or function defining the output path.
A template using |
method.png |
Character string, one of |
stamp.format |
A template for the stamp text using the
placeholders |
stamp.funs |
Optional named list of functions (elements |
stamp.gg.style |
A list of formatting options for the stamp text,
passed to |
useNames |
Deprecated. Use |
fun.path |
Deprecated. Use |
The function handles both single plot objects and lists of plots with flexible output options:
Single plots:
Written to file with specified format(s)
Can generate multiple formats simultaneously
Can generate multiple canvas sizes simultaneously
Lists of plots:
If onefile = TRUE: creates a single multi-page PDF
If onefile = FALSE: creates separate files (numbered or named)
Automatic naming based on list element names or indices
Returns NULL invisibly. Files are written as a side effect.
If show = TRUE, plots are also displayed.
Predefined canvas sizes (see canvasSize):
"standard": 7.2 × 5.4 inches
"wide": 9.6 × 5.4 inches
"wide-screen": 18.6 × 9 inches
"square": 5.4 × 5.4 inches
"A4": 5.4 × 7.2 inches
"letter": 6.5 × 9 inches
Custom sizes can be specified as list(width = w, height = h) where
w and h are in inches.
When script is provided, a caption is added to the plot using
ggstamp. The stamp typically includes:
Script file name
Timestamp (unless time = "")
Model identifier (if model is provided)
The stamp is added as a ggplot2 caption and will appear at the bottom of the plot.
If plot has traceit attributes (added via traceit), those
attributes will be automatically extracted and used, unless explicitly overridden
by arguments passed to ggwrite.
ggstamp for adding stamps to plots,
canvasSize for canvas size options,
writer for a unified interface to write various object types,
pathFormat for organizing output directories,
traceit for adding traceit attributes,
png, pdf for graphics devices
Other Plotting:
canvasSize(),
ggstamp(),
ggwater()
library(ggplot2)
writeOutput <- FALSE
data(ChickWeight)
p1 <- ggplot(ChickWeight, aes(Time, weight, group = Chick, colour = factor(Diet))) +
geom_line()
# View plot on screen
ggwrite(p1)
# Save plot with stamping
script <- "analysis.R"
ggwrite(p1, script = script, canvas = "wide",
file = "myplot1.png", save = writeOutput)
# Save in multiple formats
ggwrite(p1, file = "myplot1.png",
formats = c("png", "pdf"),
save = writeOutput)
# Multiple canvas sizes
ggwrite(p1, file = "myplot1.png",
canvas = list(small = "standard", large = "wide"),
save = writeOutput)
# Creates: myplot1_small.png, myplot1_large.png
# List of plots to separate files
p2 <- ggplot(ChickWeight, aes(Time, weight)) + geom_point()
plots <- list(lines = p1, points = p2)
ggwrite(plots, file = "chickweight.png",
use.names = TRUE, save = writeOutput)
# Creates: chickweight_lines.png, chickweight_points.png
# List of plots to single PDF
ggwrite(plots, file = "chickweight.pdf",
onefile = TRUE, save = writeOutput)
# Creates: chickweight.pdf (2 pages)
# Organize output using a %script-based directory structure
ggwrite(p1, file = "chickweight_lines.png",
script = script, path.format = "%script/%name",
save = writeOutput)
# Creates: analysis/chickweight_lines.png
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.