Learn R Programming

ggtikz (version 0.1.5)

ggtikzCanvas: Create a canvas to store TikZ annotations to a ggplot.

Description

Annotations can be made relative to the whole plot, to a panel, or to data coordinates (of individual panels).

Usage

ggtikzCanvas(gg_plot)

Value

A ggtikzCanvas object, to which annotations can be added.

Arguments

gg_plot

A ggplot object on which annotations should be made.

Details

This function provides a canvas for TikZ annotations, and does not draw anything by itself. Its purpose is to provide information about the underlying ggplot object for coordinate calculations.

See Also

grid.tikzAnnotate for annotation of base graphics.

ggtikz for a helper function for quick one-step annotations.

ggtikzAnnotation for more information about creating and adding ggtikz annotations.

Examples

Run this code
if (FALSE) {
library(ggplot2)
library(tikzDevice)
library(ggtikz)
p <- ggplot(mtcars, aes(disp, mpg)) + geom_point()

# Create a TikZ canvas on the plot
canvas <- ggtikzCanvas(p)

# Create annotations to add to the canvas

# Circle in the center of the plot
annotation1 <- ggtikzAnnotation(
   "\\fill[red] (0.5,0.5) circle (2mm);",
   xy = "plot")

# Arrow to data coordinate (400,20)
annotation2 <- ggtikzAnnotation(
   "\\draw[<-] (400,20) -- ++(0,2.5);",
   xy = "data", panelx = 1, panely = 1)

out <- tempfile(fileext = ".tikz")
tikz(out)
# First, draw the original plot
p
# Then, add the annotations to the canvas and draw it
canvas + annotation1 + annotation2
dev.off()
}

Run the code above in your browser using DataLab