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.
You can install the latest ggtikz release from CRAN with:
install.packages("ggtikz")
Or get the development version from GitHub:
# install.packages("devtools")
::install_github("osthomas/ggtikz", ref = "devel") devtools
ggtikz()
.library(ggplot2)
library(ggtikz)
<- ggplot(mtcars, aes(disp, mpg)) + geom_point() # 1.
p ## tikz("plot.tikz")
ggtikz(p,
"\\fill[red] (0.5,0.5) circle (5mm);",
xy = "panel", panelx = 1, panely = 1)
## dev.off()
## Render with LaTeX ...
ggtikzCanvas()
.ggtikzAnnotation()
.library(ggplot2)
library(ggtikz)
<- ggplot(mtcars, aes(disp, mpg)) + geom_point() # 1.
p <- ggtikzCanvas(p) # 2.
canvas <- ggtikzAnnotation( # 3.
annot1 "
\\draw (0,0) -- (1,1);
\\draw (0,1) -- (1,0);
\\fill[red] (0.5,0.5) circle (5mm);
",
xy = "panel", panelx = 1, panely = 1
)<- ggtikzAnnotation( # 3.
annot2 "\\draw[<-] (400,20) -- ++(0,3) node[at end, anchor=south] {(400,20)};",
xy = "data", panelx = 1, panely = 1
)
## tikz("plot.tikz")
# 4. + 5.
p + annot1 + annot2
canvas ## dev.off()
## Render with LaTeX ...