ggsave
The easiest way to save a plot is to to use the
ggplot2::ggsave()
function. Remember that
shape_plot()
and forest_plot()
both return a
list of the plot and code, so you need to use $plot
to save
just the plot to a file.
my_results <- data.frame(
x = c( 12, 14, 15.5, 18),
est = c(0.05, 0.21, 0.15, 0.32),
se = c(0.05, 0.05, 0.05, 0.05)
)
my_plot <- shape_plot(my_results,
xlims = c(10, 20),
ylims = c(0.75, 2),
exponentiate = TRUE,
quiet = TRUE)
ggsave("myplot.png",
plot = my_plot$plot,
width = 14, height = 14, units = "cm")
Plots created with this package have transparent backgrounds. For a
png file output, you can use the bg
argument to set the
background colour:
ggsave("myplot.png",
plot = my_plot$plot,
width = 14, height = 14, units = "cm",
bg = "white")
The ckbplotr function ggpreview()
can be used in place
of ggsave()
to preview the output.
Save with title and footnote
Use save_figure()
to add a title and footer to a plot
and save to a file. The following code will save the plot (sized to 14
by 14 cm) in an A4 sized PDF file, with title and footer.
save_figure(my_plot$plot,
filename = "Figure 1.pdf",
title = "Figure 1: My example shape plot",
footer = "An example footer text.",
size = unit(c(14, 14), "cm"))
The function has several arguments that allow for customization of
appearance and layout. For example: valign
and
halign
to control the position of the plot (if
size
is set); landscape = TRUE
to create a
landscape page; and set cropped
to be a file name to also
save a plot without additional margins, title or footer.
Preview the output
Set preview = TRUE
to view a preview of the output in
the RStudio Viewer pane. (Instead of creating a file, the figure is
saved to a temporary PNG file and shown.)
save_figure(my_plot$plot,
filename = "Figure 1.pdf",
title = "Figure 1: My example shape plot",
footer = "An example footer text.",
size = unit(c(14, 14), "cm"),
preview = TRUE)