Learn R Programming

ggtikz

ggtikz allows you to annotate plots created using ggplot2 with arbitrary TikZ code when rendering them with the tikzDevice. The annotations can be made using data coordinates, or with coordinates relative to a specified panel or the whole plot.

Plots with multiple panels (via facet_grid() or facet_wrap()) are supported.

For a few examples, see the examples vignette.

Installation

You can install the latest ggtikz release from CRAN with:

install.packages("ggtikz")

Or get the development version from GitHub:

# install.packages("devtools")
devtools::install_github("osthomas/ggtikz", ref = "devel")

Basic Usage

  1. Create a ggplot.
  2. Add annotations with ggtikz().
library(ggplot2)
library(ggtikz)

p <- ggplot(mtcars, aes(disp, mpg)) + geom_point() # 1.
## tikz("plot.tikz")
ggtikz(p,
    "\\fill[red] (0.5,0.5) circle (5mm);",
    xy = "panel", panelx = 1, panely = 1)
## dev.off()
## Render with LaTeX ...

Advanced Usage

  1. Create a ggplot.
  2. Create a ggtikz canvas from the plot with ggtikzCanvas().
  3. Create ggtikz annotations with ggtikzAnnotation().
  4. Add the annotations to the canvas.
  5. Draw the plot and the annotations using tikzDevice.
library(ggplot2)
library(ggtikz)

p <- ggplot(mtcars, aes(disp, mpg)) + geom_point()  # 1.
canvas <- ggtikzCanvas(p)                           # 2.
annot1 <- ggtikzAnnotation(                         # 3.
    "
    \\draw (0,0) -- (1,1);
    \\draw (0,1) -- (1,0);
    \\fill[red] (0.5,0.5) circle (5mm);
    ",
    xy = "panel", panelx = 1, panely = 1
)
annot2 <- ggtikzAnnotation(                          # 3.
    "\\draw[<-] (400,20) -- ++(0,3) node[at end, anchor=south] {(400,20)};",
    xy = "data", panelx = 1, panely = 1
)

## tikz("plot.tikz")
p                                                   # 4. + 5.
canvas + annot1 + annot2
## dev.off()
## Render with LaTeX ...

Copy Link

Version

Install

install.packages('ggtikz')

Monthly Downloads

201

Version

0.1.5

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Oliver Thomas

Last Published

November 6th, 2025

Functions in ggtikz (0.1.5)

set_ggtikz_unclip_hook

Unclip plots produced by the tikzDevice.
tikz_exts_pattern

Construct a regex pattern for possible tikzDevice extensions.
unclip

knitr hook to remove clipping from plots produced with the tikzDevice.
split_coord

Split a TikZ coordinate.
ggtikzTransform

Transform TikZ coordinates according to scale transformations
ggtikz

Create a canvas and add a TikZ annotation.
dense_rank

Return dense rank of a sequence
ggtikzAnnotation

Prepare a TikZ annotation for a ggplot.
ggtikz-package

ggtikz: Post-Process 'ggplot2' Plots with 'TikZ' Code Using Plot Coordinates
ggtikzUninfinite

Replace Inf in TikZ coordinates
get_padding_from_elements

Calculate length of padding from plot elements
gg_to_npc.ggtikzCanvas

Convert data coordinates to npc coordinates.
uninfinite_coord

Replace infinite values in TikZ coordinates
unclip_tikz

Unclip a plot produced by the tikzDevice.
discretize

Replace Infinites by discrete values
ggtikzCanvas

Create a canvas to store TikZ annotations to a ggplot.