Skip to content

RomanKyrychenko/ggplot2latex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

57 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ggplot2latex

Codecov test coverage R-CMD-check

ggplot2latex is an R package designed to efficiently save ggplot2 plots in LaTeX TikZ format. The package provides functions to optimize the resulting .tex files by rounding coordinates, removing duplicated elements, and reducing the file size. This ensures that the LaTeX documents are both lightweight and maintain high-quality visualizations.

Features

  • Save ggplot2 plots as LaTeX (TikZ) files.
  • Optimize .tex files by rounding coordinates.
  • Remove duplicated elements to reduce file size.
  • Utilities to handle special characters in LaTeX.

Installation

To install the package, use the following command in R:

# Install from CRAN (if available)
install.packages("ggplot2latex")

# Or install from GitHub
devtools::install_github("RomanKyrychenko/ggplot2latex")

Usage

Here is an example of how to use the save_tex function to save a ggplot2 plot as a LaTeX (TikZ) file:

library(ggplot2)
library(ggplot2latex)

# Create a ggplot2 plot
ggplot_object <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()

# Save the plot as a LaTeX (TikZ) file
save_tex(ggplot_object, "path/to/file.tex")

Why This Saves Memory

A traditional TikZ export with save_tikz() writes the full raw device output. save_tex() uses the same TikZ/vector workflow, then post-processes the generated .tex by:

  • rounding floating-point coordinates
  • removing duplicated \path[...] commands
  • dropping clip and bounding-box instructions that are not needed in the final figure
  • simplifying color definitions

That means the visual remains vector-based and LaTeX-native, but the TeX source is smaller and easier for LaTeX to process.

On five representative ggplot2 figures run locally, including a map and a network graph:

Plot Raw bytes Optimized bytes Byte reduction Raw lines Optimized lines Line reduction
Scatter 11,300 7,990 29.3% 269 134 50.2%
Scatter + smooth 10,106 7,929 21.5% 279 193 30.8%
Faceted scatter 20,750 15,014 27.6% 545 301 44.8%
World map 2,116,053 403,827 80.9% 102,620 21,999 78.6%
Network graph 45,299 19,835 56.2% 2,057 1,136 44.8%

Average reduction across these examples:

  • 43.1% smaller .tex files
  • 49.8% fewer lines of TeX

These statistics describe source size reduction, not raster compression: the figure is still emitted as TikZ, so there is no bitmap quality loss introduced by the package.

License

This package is licensed under the MIT License.

About

R package designed to efficiently save ggplot2 plots in LaTeX TikZ format

Topics

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages