grattan_save | R Documentation |
Save ggplot2 object as an image in the correct size and resolution for Grattan charts. Wrapper around ggsave(). 'grattan_save_all()' is a wrapper around 'grattan_save()' with 'type = "all"', 'save_pptx = TRUE', 'save_data = TRUE'.
grattan_save(
filename,
object = ggplot2::last_plot(),
type = "normal",
height = NULL,
save_pptx = FALSE,
save_data = FALSE,
force_labs = FALSE,
watermark = NULL,
latex = FALSE,
dpi = "retina",
ignore_long_title = FALSE,
...
)
grattan_save_all(filename, object = ggplot2::last_plot(), ...)
filename |
Required. The filename (including path
where necessary) where you want to save your image. The filename
should usually end in ".pdf" or ".png" (see |
object |
The ggplot2 graph object to be saved. Defaults to
|
type |
Sets height and width to Grattan defaults. The following chart types are available:
Set type = "all" to save your chart in all available sizes or use 'grattan_save_all()'. |
height |
Numeric, optional. NULL by default. Controls the height (in cm) of the image you wish to save. If specified, 'height' will override the default height for your chosen chart type. |
save_pptx |
'FALSE' by default. If 'TRUE', a Powerpoint presentation containing your graph will be created. Note that Powerpoint templates are not available for all chart types. If 'type = "all"', Powerpoint presentations will be created for each type for which a Powerpoint template exists. |
save_data |
Logical. Default is FALSE. If set to TRUE, a properly-formatted .xlsx file will be created containing the dataframe you passed to ggplot(). The filename and path will be the same as your image, but with a .xlsx extension. |
force_labs |
Logical. By default, 'grattan_save()' will remove your title, subtitle, and caption (if present) from your graph before saving it, unless 'type' = "fullslide". By setting 'force_labs' to TRUE, your title/subtitle/caption will be retained regardless of 'type'. |
watermark |
Character. NULL by default. If a string, like 'DRAFT', is supplied, this string will be added to your plot as a watermark. See '?watermark' for options - to use these, call 'watermark()' directly before saving your plot. |
latex |
Logical. FALSE by default. If TRUE, exports figure environment LaTeX code to clipboard and console. |
dpi |
Plot resolution. Default is "retina". |
ignore_long_title |
Default is FALSE. If TRUE, the check on a long title won't be performed. This is useful if using ggtext syntax within titles. |
... |
arguments passed to 'ggsave()'. For example, use 'device = cairo_pdf' to use the Cairo PDF rendering engine. For 'grattan_save_all()', the '...' are passed to 'grattan_save()'. |
library(ggplot2)
p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
theme_grattan()
## Not run: grattan_save("p.png", p)
# If you don't assign your chart to an object name, that's OK, it will still
# save if it was the last plot displayed.
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
theme_grattan()
## Not run: grattan_save("p.png")
# Want to make a 'self-contained' chart that includes a
# title/subtitle/caption, eg. to go on the Grattan Blog?
# If so, just add them - they'll be properly
# left-aligned when you save them with grattan_save(), like this:
ggplot(mtcars, aes(x = wt, y = mpg, col = factor(cyl))) +
geom_point() +
scale_y_continuous_grattan(limits = c(10, NA)) +
scale_colour_manual(values = grattan_pal(n = 3)) +
theme_grattan() +
labs(title = "Title goes here",
subtitle = "Subtitle goes here",
caption = "Notes: Notes go here\nSource: Source goes here")
# The plot above won't look right in RStudio's viewer - the text is
# aligned to the left of the plot area, not the image. Once you save it,
# the file should have properly-aligned text:
## Not run: grattan_save("your_file.png")
# Want to make a full Powerpoint slide? Just use type = "fullslide"
# in grattan_save(), like this.
# If you include 'notes' and 'source' as below, grattan_save() will
# automatically
# split them onto separate rows. It will also wrap your title and subtitle
# automatically over up to 2 rows; and wrap your caption over as many rows
# as necessary.
ggplot(mtcars, aes(x = wt, y = mpg, col = factor(cyl))) +
geom_point() +
scale_y_continuous_grattan(limits = c(10, NA)) +
scale_colour_manual(values = grattan_pal(n = 3)) +
theme_grattan() +
labs(title = "Title goes here",
subtitle = "Subtitle goes here",
caption = "Notes: Notes go here. Source: Source goes here")
## Not run: grattan_save("your_file.pdf", type = "normal")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.