Commit 4b0bda45 authored by houyun's avatar houyun
Browse files

parse diagonal label carefully

parent 05ab3f07
Loading
Loading
Loading
Loading
+31 −6
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
#' @description \code{geom_diag_label} is mainly used with \code{hyplot()} and
#'     \code{qcorrplot()} functions to add diagnoal labels on correct position
#'     base on different type of cor_tbl object.
#' @param parse logical or function generated by \code{parse_func()}.
#' @param geom one of "text", "label" or "richtext" (needs `ggtext` package).
#' @param ... extra parameters.
#' @importFrom ggplot2 aes_ geom_label
@@ -13,9 +14,10 @@
#' @export
#' @examples
#' qcorrplot(correlate(mtcars)) + geom_diag_label()
geom_diag_label <- function(..., geom = NULL)

geom_diag_label <- function(..., parse = FALSE, geom = NULL)
{
  structure(.Data = list(geom = geom, params = list(...)),
  structure(.Data = list(parse = parse, geom = geom, params = list(...)),
            class = "geom_diag_label")
}

@@ -30,12 +32,32 @@ ggplot_add.geom_diag_label <- function(object, plot, object_name) {
    stop("`geom_diag_label()` just support for symmetric matrices.", call. = FALSE)
  }

  if(is.null(object$geom)) {
  label <- col_names
  parse <- object$parse
  need_parse <- isTRUE(parse) || is.function(parse)
  parse_fun <- parse
  if (need_parse) {
    if (isTRUE(parse)) {
      if (is_richtext(col_names)) {
      if(suppressMessages(requireNamespace("ggtext"))) {
        parse_fun <- parse_func(output = "richtext")
      } else {
        parse_fun <- parse_func(output = "character")
      }
    }
  }
  if (is.function(parse_fun)) {
    label <- parse_fun(label)
    if (!is_richtext(label)) {
      label <- parse_safe(label)
    }
  }

  if(is.null(object$geom)) {
    if(is_richtext(label)) {
      if(requireNamespace("ggtext", quietly = TRUE)) {
        object$geom <- "richtext"
      } else {
        message("It looks like the label contains richtext\n",
        message("It looks like the label is richtext\n",
                "you can install the ggtext package to add richtext.\n")
        object$geom <- "text"
      }
@@ -50,7 +72,7 @@ ggplot_add.geom_diag_label <- function(object, plot, object_name) {

  data <- tibble::tibble(.x = seq_along(col_names),
                         .y = rev(seq_along(row_names)),
                         .label = col_names)
                         .label = label)
  params <- object$params
  params$data <- data
  params$mapping <- aes_modify(aes_(x = ~.x, y = ~.y, label = ~.label),
@@ -65,6 +87,9 @@ ggplot_add.geom_diag_label <- function(object, plot, object_name) {
      params$label.colour <- NA
    }
  }
  if (need_parse && !is_richtext(label)) {
    object$parse <- TRUE
  }
  object <- do.call(geom, params)
  ggplot_add(object, plot, object_name)
}
+3 −1
Original line number Diff line number Diff line
@@ -4,11 +4,13 @@
\alias{geom_diag_label}
\title{Add diagonal label on hyplot}
\usage{
geom_diag_label(..., geom = NULL)
geom_diag_label(..., parse = FALSE, geom = NULL)
}
\arguments{
\item{...}{extra parameters.}

\item{parse}{logical or function generated by \code{parse_func()}.}

\item{geom}{one of "text", "label" or "richtext" (needs `ggtext` package).}
}
\description{