Commit c02d159a authored by houyun's avatar houyun
Browse files

axis filter helper funtion

parent 5ce7d9b7
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -26,8 +26,11 @@ S3method(ggplot_add,magic_text)
S3method(ggplot_add,secondary_axis)
S3method(ggplot_build,gggplot)
S3method(guide_gengrob,guide_child)
S3method(guide_gengrob,guide_filter)
S3method(guide_train,guide_child)
S3method(guide_train,guide_filter)
S3method(guide_transform,guide_child)
S3method(guide_transform,guide_filter)
S3method(length,marker)
S3method(makeContent,annotateGrob)
S3method(makeContent,markerGrob)
@@ -99,6 +102,7 @@ export(geom_square)
export(get_order)
export(grid.doughnut)
export(guide_axis_child)
export(guide_axis_filter)
export(hyplot)
export(is_grouped_matrix_data)
export(is_matrix_data)
+67 −3
Original line number Diff line number Diff line
#' @title Secondary and Nested Axis
#' @title Axis helper function
#' @description functions to add secondary and nested axis.
#' @param ... other parameters passing to \code{ggh4x::guide_axis_manual()} or
#' \code{ggh4x::guide_axis_nested()}.
#' @param ... other parameters passing to:
#' \itemize{
#'      \item{\code{secondary_axis*()}: ggh4x::guide_axis_manual().}
#'      \item{\code{guide_axis_filter()}: parameters for filtering.}
#'      }
#' @param position where this guide should be drawn: one of top, bottom, left, or right.
#' @param position_aes one of "x" or "y".
#' @inheritParams ggplot2::guide_axis
#' @return a guide object.
#' @rdname axis
#' @importFrom ggplot2 guides
@@ -286,6 +290,66 @@ guide_gengrob.guide_child <- function(guide, theme) {
        cl = "absoluteGrob")
}

#' @rdname axis
#' @export
guide_axis_filter <- function(...,
                              title = waiver(),
                              check.overlap = FALSE,
                              angle = NULL,
                              n.dodge = 1,
                              order = 0,
                              position = waiver()) {

  structure(
    list(params = rlang::enquos(...),
         title = title,
         check.overlap = check.overlap,
         angle = angle,
         n.dodge = n.dodge,
         order = order,
         position = position,
         available_aes = c("x", "y"),
         name = "axis"),
    class = c("guide", "guide_filter", "axis")
  )
}

#' @method guide_train guide_filter
#' @importFrom ggplot2 guide_train
#' @export
guide_train.guide_filter <- function(guide, scale, aesthetic = NULL) {
  NextMethod()
}

#' @method guide_transform guide_filter
#' @importFrom ggplot2 guide_transform
#' @export
guide_transform.guide_filter <- function(guide, coord, panel_params) {

  guide$key <- filter2(guide$key, guide$params)
  NextMethod()
}

#' @method guide_gengrob guide_filter
#' @importFrom ggplot2 guide_gengrob
#' @export
guide_gengrob.guide_filter <- function(guide, theme) {
  NextMethod()
}

#' @noRd
filter2 <- function(data, params) {
  if (length(params) < 1) {
    return(data)
  }

  id <- TRUE
  for (ii in params) {
    id <- id & rlang::eval_tidy(ii, data)
  }
  data[id, , drop = FALSE]
}

#' @importFrom grid grobHeight
#' @noRd
grobHeight.absoluteGrob <- function(x) {
+36 −3
Original line number Diff line number Diff line
@@ -4,21 +4,54 @@
\alias{secondary_x_axis}
\alias{secondary_y_axis}
\alias{set_secondary_axis}
\title{Secondary and Nested Axis}
\alias{guide_axis_filter}
\title{Axis helper function}
\usage{
secondary_x_axis(...)

secondary_y_axis(...)

set_secondary_axis(..., position = waiver(), position_aes = "x")

guide_axis_filter(
  ...,
  title = waiver(),
  check.overlap = FALSE,
  angle = NULL,
  n.dodge = 1,
  order = 0,
  position = waiver()
)
}
\arguments{
\item{...}{other parameters passing to \code{ggh4x::guide_axis_manual()} or
\code{ggh4x::guide_axis_nested()}.}
\item{...}{other parameters passing to:
\itemize{
     \item{\code{secondary_axis*()}: ggh4x::guide_axis_manual().}
     \item{\code{guide_axis_filter()}: parameters for filtering.}
     }}

\item{position}{where this guide should be drawn: one of top, bottom, left, or right.}

\item{position_aes}{one of "x" or "y".}

\item{title}{A character string or expression indicating a title of guide.
If \code{NULL}, the title is not shown. By default
(\code{\link[ggplot2:waiver]{waiver()}}), the name of the scale object or the name
specified in \code{\link[ggplot2:labs]{labs()}} is used for the title.}

\item{check.overlap}{silently remove overlapping labels,
(recursively) prioritizing the first, last, and middle labels.}

\item{angle}{Compared to setting the angle in \code{\link[ggplot2:theme]{theme()}} / \code{\link[ggplot2:element]{element_text()}},
this also uses some heuristics to automatically pick the \code{hjust} and \code{vjust} that
you probably want.}

\item{n.dodge}{The number of rows (for vertical axes) or columns (for
horizontal axes) that should be used to render the labels. This is
useful for displaying labels that would otherwise overlap.}

\item{order}{Used to determine the order of the guides (left-to-right,
top-to-bottom), if more than one  guide must be drawn at the same location.}
}
\value{
a guide object.