petit_plot | R Documentation |
Save a plot to a file and automatically shrink it with tinify()
.
petit_plot( filename = "plot", path = NULL, device = "png", ragg = FALSE, keep_large = FALSE, suffix, quiet, return_path, key = NULL, ... ) petit_ggplot( filename = "plot", path = NULL, plot = ggplot2::last_plot(), device = "png", keep_large = FALSE, suffix, quiet, return_path, key = NULL, ... )
filename |
String, required. The name to give the output image file. Do not include a file extension. |
path |
String, optional. If |
device |
String, optional. Defaults to |
ragg |
Boolean, optional. Defaults to |
keep_large |
Boolean, optional. Defaults to |
suffix |
String, optional. If |
quiet |
Boolean, optional. If set to |
return_path |
String or |
key |
String, optional. A string containing your TinyPNG API key. Not
required if the API key is set using |
... |
Additional plot options, passed directly to either |
plot |
Object, optional. The plot object to export. Defaults to the last plot
modified or created (using |
These are convenience functions to wrap saving either a base R plot with
png()
or jpeg()
devices, or a ggplot object with ggplot2::ggsave()
, before passing the resulting
image file directly to tinify()
. Can also make use of the ragg
package for base plots if installed.
If return_path = "proj"
, return_path = "rel"
, or return_path = "abs"
, a string with the project, relative, or absolute path to the newly
tinified image file. If no project can be identified for return_path = "proj"
, returns NA
. If return_path = "all"
, a named list with all file
paths included as $project
, $relative
, and $absolute
respectively. If
return_path = NULL
, no return value.
To save and shrink a base R plot, print the plot and call
petit_plot()
immediately after. Under the hood, this uses recordPlot()
to capture
and replay the last plot created within the chosen device with the applied options:
plot(mtcars$mpg, mtcars$drat) petit_plot(filename = "mtcars")
To save and shrink a ggplot, either create, modify, or show
the plot and call petit_plot()
immediately after, in a similar process to base R plots:
ggplot(data = palmerpenguins::penguins, aes(flipper_length_mm, body_mass_g)) + geom_point(aes(color = species)) petit_plot(filename = "penguins")
Or use petit_ggplot()
to capture specifically the last ggplot created or modified:
ggplot(data = palmerpenguins::penguins, aes(flipper_length_mm, body_mass_g)) + geom_point(aes(color = species)) petit_ggplot(filename = "penguins")
Or provide the plot object explicitly to petit_ggplot()
with plot
:
p <- ggplot(data = palmerpenguins::penguins, aes(flipper_length_mm, body_mass_g)) + geom_point(aes(color = species)) petit_ggplot(filename = "penguins", plot = p)
tinify()
tinify_key()
to set an API key globally
tinify_defaults()
to set default arguments that will be used if not
provided explicitly
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.