Commit 24e96c24 authored by houyun's avatar houyun
Browse files

elemant_magic_text

parent ec6361c2
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ S3method(as.data.frame,md_tbl)
S3method(as_md_tbl,corr.test)
S3method(as_tibble,md_tbl)
S3method(c,marker)
S3method(element_grob,element_magic_text)
S3method(get_order,character)
S3method(get_order,dendrogram)
S3method(get_order,dist)
@@ -82,6 +83,7 @@ export(contain_with)
export(correlate)
export(dist_func)
export(draw_key_marker)
export(element_magic_text)
export(extract_diag)
export(extract_lower)
export(extract_nodes)
@@ -170,6 +172,7 @@ importFrom(ggplot2,draw_key_path)
importFrom(ggplot2,draw_key_polygon)
importFrom(ggplot2,draw_key_text)
importFrom(ggplot2,element_blank)
importFrom(ggplot2,element_grob)
importFrom(ggplot2,element_text)
importFrom(ggplot2,expand_limits)
importFrom(ggplot2,facet_wrap)
+76 −0
Original line number Diff line number Diff line
@@ -38,3 +38,79 @@ label_formula <- function (parse = TRUE) {
    x
  }
}

#' @importFrom ggplot2 element_grob
#' @export
element_grob.element_magic_text <- function(element, label = "", ...) {
  if (is.null(label)) {
    return(ggplot2::zeroGrob())
  }

  parse_fun <- element$parse
  if (isTRUE(parse_fun)) {
    parse_fun <- parse_func(output = "expression")
  }
  if (is.function(parse_fun) && !is.expression(label)) {
    label <- parse_fun(label)
    if (!is_richtext(label)) {
      label <- parse(text = label)
    }
  }
  element <- element[setdiff(names(element), "parse")]
  if (is_richtext(label)) {
    class(element) <- c("element_markdown", "element_text", "element")
  } else {
    class(element) <- c("element_text", "element")
  }
  ggplot2::element_grob(element = element, label = label, ...)
}

#' @title Theme element that enables magic text
#' @description theme element that can parse text to expression or richtext.
#' @inheritParams ggplot2::element_text
#' @param parse logical or a parse function. IF TRUE (default), the labels will
#' be parsed into expression.
#' @return a element_magic_text object.
#' @rdname element_magic_text
#' @author Hou Yun
#' @export
element_magic_text <- function(parse = TRUE,
                               family = NULL,
                               face = NULL,
                               size = NULL,
                               colour = NULL,
                               hjust = NULL,
                               vjust = NULL,
                               angle = NULL,
                               lineheight = NULL,
                               margin = NULL,
                               color = NULL,
                               debug = FALSE,
                               inherit.blank = FALSE) {
  if (!is.null(color)) {
    colour <- color
  }

  n <- max(length(family), length(face), length(colour), length(size),
           length(hjust), length(vjust), length(angle), length(lineheight))
  if (n > 1) {
    warning("Vectorized input to `element_text()` is not officially supported.\n",
    "Results may be unexpected or may change in future versions of ggplot2.",
    call. = FALSE)
  }

  structure(list(parse = parse,
                 family = family,
                 face = face,
                 colour = colour,
                 size = size,
                 hjust = hjust,
                 vjust = vjust,
                 angle = angle,
                 lineheight = lineheight,
                 margin = margin,
                 debug = debug,
                 inherit.blank = inherit.blank),
            class = c("element_magic_text", "element_text", "element"))

}
+65 −0
Original line number Diff line number Diff line
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/gg-utils.R
\name{element_magic_text}
\alias{element_magic_text}
\title{Theme element that enables magic text}
\usage{
element_magic_text(
  parse = TRUE,
  family = NULL,
  face = NULL,
  size = NULL,
  colour = NULL,
  hjust = NULL,
  vjust = NULL,
  angle = NULL,
  lineheight = NULL,
  margin = NULL,
  color = NULL,
  debug = FALSE,
  inherit.blank = FALSE
)
}
\arguments{
\item{parse}{logical or a parse function. IF TRUE (default), the labels will
be parsed into expression.}

\item{family}{Font family}

\item{face}{Font face ("plain", "italic", "bold", "bold.italic")}

\item{size}{Line/border size in mm; text size in pts.}

\item{colour, color}{Line/border colour. Color is an alias for colour.}

\item{hjust}{Horizontal justification (in \eqn{[0, 1]})}

\item{vjust}{Vertical justification (in \eqn{[0, 1]})}

\item{angle}{Angle (in \eqn{[0, 360]})}

\item{lineheight}{Line height}

\item{margin}{Margins around the text. See \code{\link[ggplot2:margin]{margin()}} for more
details. When creating a theme, the margins should be placed on the
side of the text facing towards the center of the plot.}

\item{debug}{If \code{TRUE}, aids visual debugging by drawing a solid
rectangle behind the complete text area, and a point where each label
is anchored.}

\item{inherit.blank}{Should this element inherit the existence of an
\code{element_blank} among its parents? If \code{TRUE} the existence of
a blank element among its parents will cause this element to be blank as
well. If \code{FALSE} any blank parent element will be ignored when
calculating final element state.}
}
\value{
a element_magic_text object.
}
\description{
theme element that can parse text to expression or richtext.
}
\author{
Hou Yun
}