Skip to content

Commit

Permalink
add: label_tex as an extention of ggplot2::label_parsed (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
atusy authored Aug 16, 2022
1 parent 33584ca commit cd9a76b
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Suggests:
sessioninfo,
concaveman,
deldir,
latex2exp,
reshape2,
units (>= 0.4-6),
covr
Expand Down Expand Up @@ -74,6 +75,7 @@ Collate:
'ggforce-package.R'
'ggproto-classes.R'
'interpolate.R'
'labeller.R'
'link.R'
'mark_circle.R'
'mark_ellipse.R'
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export(geom_spiro)
export(geom_voronoi_segment)
export(geom_voronoi_tile)
export(interpolateDataFrame)
export(label_tex)
export(linear_trans)
export(n_pages)
export(position_auto)
Expand Down Expand Up @@ -156,6 +157,7 @@ export(trans_reverser)
import(ggplot2)
importFrom(MASS,fractions)
importFrom(Rcpp,sourceCpp)
importFrom(ggplot2,label_parsed)
importFrom(ggplot2,layer)
importFrom(grDevices,chull)
importFrom(grid,addGrob)
Expand Down
33 changes: 33 additions & 0 deletions R/labeller.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#' A labeller function to parse TeX syntax
#'
#' This function formats the strip labels of facet grids and wraps that contains
#' TeX expressions. The latex2exp package must be installed.
#'
#' @seealso [ggplot2::labeller], [latex2exp::TeX()]
#'
#' @inheritParams ggplot2::label_parsed
#' @inheritDotParams ggplot2::label_parsed -labels
#'
#' @examples
#' # requires latex2exp package be installed
#' if (requireNamespace("latex2exp", quietly = TRUE)) {
#' library(ggplot2)
#' d <- data.frame(x = 1, y = 1, facet = "$\\beta$")
#' ggplot(d, aes(x, y)) +
#' geom_point() +
#' facet_wrap(~ facet, labeller = label_tex)
#' }
#' @importFrom ggplot2 label_parsed
#' @export
label_tex <- function(labels, ...) {
if (!requireNamespace("latex2exp", quiet = TRUE)) {
stop("The latex2exp package is needed for this functionality", call. = FALSE)
}
label_parsed(
as.data.frame(
lapply(labels, latex2exp::TeX, output = "character"),
stringsAsFactors = FALSE
),
...
)
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ reference:
- geom_sina
- geom_autopoint
- geom_autodensity
- label_tex
- stat_err
- position_auto
- position_jitternormal
Expand Down
37 changes: 37 additions & 0 deletions man/label_tex.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cd9a76b

Please sign in to comment.