save_tikz | R Documentation |
This function saves a ggplot figure as a TikZ file for inclusion in LaTeX documents.
It checks for a valid ggplot object, ensures the file path is specified, and
appends the .tex
extension if not provided. The function also allows sanitization
of special characters in the output file.
save_tikz(fig = NULL, path = NULL, sanitize = TRUE, width = 6.4, height = 3.54)
fig |
A ggplot object. This is the figure to be saved as a TikZ file. |
path |
A character string specifying the file path where the TikZ file will be saved.
If no extension is provided, |
sanitize |
Logical, whether to sanitize special characters in the LaTeX output.
Defaults to |
width |
Numeric, specifying the width of the figure in inches. Defaults to |
height |
Numeric, specifying the height of the figure in inches. Defaults to |
This function uses the tikzDevice
package to generate TikZ code from a ggplot object.
It ensures that the file path provided is valid and appends the .tex
extension if missing.
If the specified directory does not exist, the function will abort with an error message.
Additionally, the function provides useful information to the user, such as warnings
when no extension is provided and confirmation once the file is saved successfully.
The path to the saved TikZ file is returned invisibly.
The tikzDevice
package must be installed to use this function.
## Not run:
library(ggplot2)
# Create a sample plot
p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
theme_minimal()
# Save the plot as a TikZ file
save_tikz(fig = p, path = "my_plot")
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.