| Title: | Consistent Plot Rendering and Saving Across Interactive Sessions and Reports |
|---|---|
| Description: | Renders plots to a temporary image using the ragg graphics device and returns knitr::include_graphics() output. Optionally saves the image to a specified path. This helps ensure consistent appearance across interactive sessions, saved files, and knitted documents. For more details see Pedersen and Shemanarev (2025) <doi:10.32614/CRAN.package.ragg>. |
| Authors: | Tom Naber [aut, cre] |
| Maintainer: | Tom Naber <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.1 |
| Built: | 2026-06-03 07:17:53 UTC |
| Source: | https://github.com/tomnaber/sameplot |
sameplot() renders plot to a temporary PNG using a ragg device and returns
a knitr image include pointing to that PNG. This makes the displayed output
consistent across interactive use and knitted documents. Optionally, when
save = TRUE, the plot is also saved to file using a ragg device inferred
from the file extension.
sameplot( plot, file = NULL, width = 6.4, height = 4.8, units = "in", background = "white", scaling = 1, bitsize = 8, res = 300, save = FALSE )sameplot( plot, file = NULL, width = 6.4, height = 4.8, units = "in", background = "white", scaling = 1, bitsize = 8, res = 300, save = FALSE )
plot |
A plot object. Typically a ggplot, but any object whose |
file |
Output filename (relative or absolute). Must include an extension
|
width, height
|
Plot size. |
units |
Units for |
background |
Background color passed to ragg devices. |
scaling |
Increase to make plot bigger within the same physical size. |
bitsize |
Record color as 8 bit or 16 bit. 16 bit may be useful for smoother gradients. |
res |
Resolution in pixels per inch passed to ragg devices. |
save |
Logical; if |
When knitting to HTML (e.g., rmarkdown::html_document()), sameplot()
requires the document to be self-contained so that the generated images are
embedded in the output. Add this to your YAML:
output:
html_document:
self_contained: true
An object returned by knitr::include_graphics()
(rendered by knitr in HTML/PDF output).
p <- ggplot2::ggplot(mtcars, ggplot2::aes(wt, mpg)) + ggplot2::geom_point() # Display via temporary PNG sameplot(p) # Save a PNG sameplot(p, file = file.path(tempdir(), "mtcars.png"), save = TRUE ) # Save a TIFF with custom parameters sameplot( p, file = file.path(tempdir(), "mtcars.tiff"), width = 4, height = 6, background = "transparent", res = 600, save = TRUE )p <- ggplot2::ggplot(mtcars, ggplot2::aes(wt, mpg)) + ggplot2::geom_point() # Display via temporary PNG sameplot(p) # Save a PNG sameplot(p, file = file.path(tempdir(), "mtcars.png"), save = TRUE ) # Save a TIFF with custom parameters sameplot( p, file = file.path(tempdir(), "mtcars.tiff"), width = 4, height = 6, background = "transparent", res = 600, save = TRUE )