Title: | Post-Process 'ggplot2' Plots with 'TikZ' Code Using Plot Coordinates |
Version: | 0.1.3 |
Description: | Annotation of 'ggplot2' plots with arbitrary 'TikZ' code, using absolute data or relative plot coordinates. |
License: | MIT + file LICENSE |
URL: | https://github.com/osthomas/ggtikz |
BugReports: | https://github.com/osthomas/ggtikz/issues |
Encoding: | UTF-8 |
RoxygenNote: | 7.3.1 |
Imports: | dplyr, ggplot2 (≥ 3.5.0), grid, stringr, tikzDevice |
Suggests: | covr, knitr, magick, rmarkdown, testthat (≥ 3.0.0), tinytex |
Config/testthat/edition: | 3 |
VignetteBuilder: | knitr |
NeedsCompilation: | no |
Packaged: | 2024-06-15 12:32:31 UTC; thomaso |
Author: | Oliver Thomas [aut, cre] |
Maintainer: | Oliver Thomas <ost.dev@posteo.net> |
Repository: | CRAN |
Date/Publication: | 2024-06-16 15:10:02 UTC |
Replace Infinites by discrete values
Description
The replacement values correspond to the edges of the available coordinate space
Usage
discretize(coord_values, xrange, yrange)
Arguments
coord_values |
numeric. The coordinate x and y values, potentially containing Inf or -Inf |
xrange |
Numeric vector of length 2, minimum and maximum values in the x direction |
yrange |
Numeric vector of length 2, minimum and maximum values in the y direction |
Calculate length of padding from plot elements
Description
To prevent overlap with panel borders or axis lines, annotations are clipped to a viewport that is reduced in size by the width of these lines. They depend on the current plot theme.
Usage
get_padding_from_elements(
gg_plot,
elements_t,
elements_r,
elements_b,
elements_l
)
Arguments
gg_plot |
A ggplot2 object. |
elements_t |
character vector with names of elements to consider for padding at the top |
elements_r |
character vector with names of elements to consider for padding on the right |
elements_b |
character vector with names of elements to consider for padding at the bottom |
elements_l |
character vector with names of elements to consider for padding on the left |
Value
A vector grid::unit
s of paddings for t
, r
, b
, l
(in pt)
See Also
uninfinite_coord
for construction of the complete
replaced coordinate.
Convert data coordinates to npc coordinates.
Description
Convert data coordinates to npc coordinates.
Usage
## S3 method for class 'ggtikzCanvas'
gg_to_npc(self, coord, panelx, panely, ...)
Arguments
self |
a |
coord |
A numeric vector of length 2, with the x coordinate to convert
at |
panelx |
X position (column) of the panel holding the data |
panely |
X position (row) of the panel holding the data |
... |
unused |
Value
The input coordinates from coord
converted to npc coordinates in
the form of a numeric vector of length 2. (0,0) corresponds to the lower
left corner of the viewport containing the ggplot
panel specified by
panelx
and panely
, and (1,1) corresponds to the upper right corner.
Create a canvas and add a TikZ annotation.
Description
This is a helper function for quick one-step annotations. It creates a ggtikzCanvas from a ggplot, adds one annotation to it, and optionally draws the plot and the annotations.
Usage
ggtikz(gg_plot, ..., draw = TRUE)
Arguments
gg_plot |
A ggplot object on which annotations should be made. |
... |
Passed to |
draw |
TRUE or FALSE. Should gg_plot and the resulting annotation be drawn immediately? A tikz device needs to be open. |
Details
For finer control, see ggtikzCanvas()
and ggtikzAnnotation()
.
Value
A ggtikzCanvas
object with one
ggtikzAnnotation
(specified in ...
) already added. If
draw = TRUE
, the gg_plot
and the annotations are drawn to the currently
active device. This must be a tikzDevice
, or an error will be raised.
See Also
ggtikzCanvas
for creating a canvas which can store
multiple annotations.
ggtikzAnnotation
for creating an annotation, which can
then be added to a canvas.
Examples
## Not run:
library(ggplot2)
library(tikzDevice)
library(ggtikz)
p <- ggplot(mtcars, aes(disp, mpg)) + geom_point()
out <- tempfile(fileext = ".tikz")
tikz(out)
# Add a red circle in the middle of the plot.
ggtikz(p, "\\fill[red] (0.5,0.5) circle (2mm);", xy="plot")
dev.off()
## End(Not run)
Prepare a TikZ annotation for a ggplot.
Description
ggtikzAnnotation objects are meant to be added to a ggtikzCanvas object.
Usage
ggtikzAnnotation(
tikz_code,
x = c("data", "panel"),
y = c("data", "panel"),
xy = NULL,
panelx = NULL,
panely = NULL,
transform = TRUE,
replace_inf = TRUE,
clip = "on"
)
Arguments
tikz_code |
The tikz code to use for annotation. Backslashes must be escaped! |
x |
Reference frame for the x coordinates. Either "data" or "panel". |
y |
Reference frame for the y coordinates. Either "data" or "panel". |
xy |
Reference frame for both x and y coordinates. Trumps |
panelx |
x position of the panel to use as coordinate reference, starting from the left, 1-based. |
panely |
y position of the panel to use as coordinate reference, starting from the top, 1-based. |
transform |
Should TikZ coordinates be transformed according to the
scale transformation? If |
replace_inf |
Should annotation coordinates containing 'Inf' or '-Inf'
be adjusted so these values correspond to the edge of the available space?
This is analogous to the behavior of ggplot when infinite values are
encountered.
See also |
clip |
Should annotations be clipped to the panel boundaries?
See the |
Details
This function prepares TikZ annotations in a form understandable to a ggtikzCanvas object. An annotation can be added to multiple ggtikzCanvas objects, provided that each underlying ggplot object has the necessary panels to know what to do with this information.
Value
A ggtikzAnnotation object, which can be added to a ggtikzCanvas object.
See Also
grid.tikzAnnotate
for annotation of base graphics
ggtikz
for a helper function for quick one-step annotations.
ggtikzCanvas
for information about initiating the annotation process.
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)
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.
Value
A ggtikzCanvas object, to which annotations can be added.
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
## Not run:
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()
## End(Not run)
Transform TikZ coordinates according to scale transformations
Description
ggtikzTransform extracts coordinates definitions in an annotation's TikZ code and transforms them with the transformer functions stored in the underlying plot's x or y scales, respectively.
Usage
ggtikzTransform(ggtikzCanvas, ggtikzAnnotation)
Arguments
ggtikzCanvas |
A |
ggtikzAnnotation |
A |
Details
This function does not have to called directly. It is automatically called
when annotations are added to a canvas, if transform = TRUE
in the
ggtikzAnnotation
construction call.
Coordinates components with physical lengths are not changed. For a plot with a linear x scale and a log10-transformed y scale,
the TikZ coordinate (10,10) becomes (10,1),
the TikZ coordinate (10cm,10) becomes (10cm,1),
the TikZ coordinate (10,10cm) becomes (10,10cm)
the TikZ coordinate (0,0) will raise an error.
Value
A link{ggtikzAnnotation}
object, with transformations applied
to the coordinates in the TikZ code.
Replace Inf in TikZ coordinates
Description
Infinite values in TiKZ coordinate specifications are replaced by values corresponding to the edge of the available coordinate space. This allows placement of annotations at the very edge of a panel without knowing its precise coordinates. This is useful for annotations which extend to the panel boundaries, but also make use of specific coordinates.
Usage
ggtikzUninfinite(ggtikzCanvas, ggtikzAnnotation)
Arguments
ggtikzCanvas |
A |
ggtikzAnnotation |
A |
Value
A link{ggtikzAnnotation}
object, with Infinites in coordinates
replaced by finite values.
Unclip plots produced by the tikzDevice
.
Description
By default, plots produced with the tikzDevice are clipped to the plot area, which also clips ggtikzAnnotations extending beyond the plot boundaries. This function removes the 'clip' and 'use as bounding box' options in a tikz file.
Usage
set_ggtikz_unclip_hook()
unset_ggtikz_unclip_hook()
Value
Called for side effects - the unclip
knitr hook is set or unset,
respectively.
See Also
unclip
, the hook that is being set.
Split a TikZ coordinate.
Description
Split a TikZ coordinate.
Usage
split_coord(coord)
Arguments
coord |
Coordinate string of the form "(x,y)" |
Value
A character vector of length 2: The x and y components of the coordinate. These may contain spaces.
Construct a regex pattern for possible tikzDevice extensions.
Description
Construct a regex pattern for possible tikzDevice extensions.
Usage
tikz_exts_pattern(options)
Arguments
options |
A list of |
Value
A regex pattern to match file extensions of tikz figures
knitr hook to remove clipping from plots produced with the tikzDevice.
Description
Note that the chunk options unclip = TRUE
and external = FALSE
must be set
for the hook to come into effect!
Usage
unclip(before, options)
Arguments
before |
see |
options |
see |
Value
Called for side effect. The files containing tikz plots are edited and overwritten.
See Also
set_ggtikz_unclip_hook
to set the knitr hook.
unclip_tikz
, the workhorse function for this hook.
Unclip a plot produced by the tikzDevice
.
Description
By default, plots produced with the tikzDevice are clipped to the plot area, which also clips ggtikzAnnotations extending beyond the plot boundaries. This function removes the 'clip' and 'use as bounding box' options in a tikz file.
Usage
unclip_tikz(fpath)
Arguments
fpath |
Path to the tikz file |
Details
This function can be used for manual post-processing, however,
see set_ggtikz_unclip_hook
to set the corresponding knitr hook.
Value
Called for side effect.
The file at fpath
is edited and overwritten.
See Also
set_ggtikz_unclip_hook
to set the knitr hook.
Replace infinite values in TikZ coordinates
Description
Infinite values are replaced with the minimum or maximum value of the padding in the x or y direction, respectively. Additionally, the adjusted coordinate is padded so that it lies just next to the panel borders and axis lines without overlap.
Usage
uninfinite_coord(coord, xrange, yrange)
uninfinite_tikz(tikz_code, xrange, yrange)
Arguments
coord |
TikZ coordinate |
xrange |
Numeric vector of length 2, minimum and maximum values in the x direction |
yrange |
Numeric vector of length 2, minimum and maximum values in the y direction |
tikz_code |
The TikZ code to replace Infinite values in. |
Value
The adjusted TikZ coordinate with padding, as a string.