-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
129 lines (104 loc) · 3.79 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
# Process .pdf generated by tikzDevice to .png
pdf2png <- function(path, options) {
if (!grepl(".pdf$", path)) return(path)
outpath <- gsub(".pdf", ".png", path)
texpath <- gsub(".pdf", ".tex", path)
img <- magick::image_read(path, density=300)
magick::image_write(img, outpath, format="png")
unlink(path)
unlink(texpath)
return(outpath)
}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
fig.width = 3,
fig.height = 3,
fig.show = "hold",
out.width="50%",
dev = "tikz",
external = TRUE,
fig.process = pdf2png
)
library(tikzDevice)
library(ggplot2)
library(ggtikz)
options(tikzLatexPackages = c(getOption("tikzLatexPackages"), "\\usetikzlibrary{calc}"))
```
# ggtikz
<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/ggtikz)](https://CRAN.R-project.org/package=ggtikz)
[![codecov](https://codecov.io/gh/osthomas/ggtikz/branch/main/graph/badge.svg?token=0LPNGPFO5Z)](https://app.codecov.io/gh/osthomas/ggtikz)
[![R-CMD-check](https://github.com/osthomas/ggtikz/workflows/R-CMD-check/badge.svg)](https://github.com/osthomas/ggtikz/actions)
<!-- badges: end -->
ggtikz allows you to annotate plots created using
[ggplot2](https://ggplot2.tidyverse.org/)
with arbitrary TikZ code when rendering
them with the [tikzDevice](https://CRAN.R-project.org/package=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](https://github.com/osthomas/ggtikz/blob/devel/doc/examples.pdf).
## Installation
You can install the latest ggtikz release from CRAN with:
``` {r, eval = FALSE}
install.packages("ggtikz")
```
Or get the development version from GitHub:
``` {r, eval = FALSE}
# install.packages("devtools")
devtools::install_github("osthomas/ggtikz", ref = "devel")
```
<!-- ## New in Devel
[![codecov (devel)](https://codecov.io/gh/osthomas/ggtikz/branch/devel/graph/badge.svg?token=0LPNGPFO5Z)](https://codecov.io/gh/osthomas/ggtikz)
[![R-CMD-check (devel)](https://github.com/osthomas/ggtikz/workflows/R-CMD-check/badge.svg?branch=devel)](https://github.com/osthomas/ggtikz/actions)
-->
## Basic Usage
1. Create a ggplot.
2. Add annotations with `ggtikz()`.
```{r basic-usage}
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.
```{r advanced-usage}
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 ...
```